Class: K::Replace::Statement

Inherits:
Object
  • Object
show all
Includes:
DynamicAccessors, Keys
Defined in:
lib/k/replace/statement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DynamicAccessors

#create_accessors

Methods included from Keys

#chain_properties, #chain_property, #find_all

Constructor Details

#initialize(source) ⇒ Statement

Returns a new instance of Statement.



11
12
13
14
15
16
17
18
# File 'lib/k/replace/statement.rb', line 11

def initialize(source)
  @keys = []
  @source = source

  find_all(@source) do |key|
    @keys << key
  end
end

Instance Attribute Details

#keysObject (readonly)

Returns the value of attribute keys.



9
10
11
# File 'lib/k/replace/statement.rb', line 9

def keys
  @keys
end

#sourceObject (readonly)

Returns the value of attribute source.



9
10
11
# File 'lib/k/replace/statement.rb', line 9

def source
  @source
end

Instance Method Details

#replace(hash) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/k/replace/statement.rb', line 20

def replace(hash)
  @source.gsub(kreplace.key_pattern) do |key|
    replaced = ''
    find_all(key) do |match|
      chained = chain_property(match)
      chained.each{|k, v| replaced = resolve_property(k, v, hash[k])}
    end
    replaced
  end
end