Class: K::Replace::Statement

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Keys

#chain_properties, #chain_property, #find_all

Constructor Details

#initialize(source) ⇒ Statement

Returns a new instance of Statement.



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

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(*objects) ⇒ Object



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

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