Class: Build::Text::Substitutions

Inherits:
Object
  • Object
show all
Defined in:
lib/build/text/substitutions.rb

Defined Under Namespace

Classes: NestedSubstitution, SymbolicSubstitution

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ordered = []) ⇒ Substitutions

Returns a new instance of Substitutions.



26
27
28
# File 'lib/build/text/substitutions.rb', line 26

def initialize(ordered = [])
  @ordered = ordered
end

Instance Attribute Details

#orderedObject (readonly)

Returns the value of attribute ordered.



53
54
55
# File 'lib/build/text/substitutions.rb', line 53

def ordered
  @ordered
end

Instance Method Details

#+(other) ⇒ Object



49
50
51
# File 'lib/build/text/substitutions.rb', line 49

def + other
  Substitutions.new(@ordered + other.ordered)
end

#<<(substitution) ⇒ Object



45
46
47
# File 'lib/build/text/substitutions.rb', line 45

def << substitution
  @ordered << substition
end

#[]=(keyword, value) ⇒ Object



36
37
38
39
40
41
42
43
# File 'lib/build/text/substitutions.rb', line 36

def []= keyword, value
  if Array === value
    open, close = *value.each_slice(value.length / 2)
    @ordered << NestedSubstitution.new(keyword, open, close)
  else
    @ordered << SymbolicSubstitution.new('$' + keyword, value.to_s)
  end
end

#apply(text) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/build/text/substitutions.rb', line 59

def apply(text)
  return text unless @ordered.count > 0
  
  grouped = [[@ordered.first]]
  
  @ordered.drop(1).each do |substitution|
    if grouped.last[0].class == substitution.class
      grouped.last << substitution
    else
      grouped << [substitution]
    end
  end
  
  grouped.each do |group|
    text = group.first.class.apply(text, group)
  end
  
  return text
end

#call(text) ⇒ Object



55
56
57
# File 'lib/build/text/substitutions.rb', line 55

def call(text)
  apply(text)
end

#freezeObject



30
31
32
33
34
# File 'lib/build/text/substitutions.rb', line 30

def freeze
  @ordered.freeze
  
  super
end