Class: HammerBuilder::StringsInjector

Inherits:
Object
  • Object
show all
Defined in:
lib/hammer_builder/strings_injector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ StringsInjector

Returns a new instance of StringsInjector.



6
7
8
9
10
# File 'lib/hammer_builder/strings_injector.rb', line 6

def initialize(&block)
  @strings           = Hash.new {|hash, key| raise ArgumentError "missing key #{key}" }
  @objects_to_update = []
  instance_eval &block
end

Instance Attribute Details

#objects_to_updateObject (readonly)

Returns the value of attribute objects_to_update.



4
5
6
# File 'lib/hammer_builder/strings_injector.rb', line 4

def objects_to_update
  @objects_to_update
end

#stringsObject (readonly)

Returns the value of attribute strings.



4
5
6
# File 'lib/hammer_builder/strings_injector.rb', line 4

def strings
  @strings
end

Instance Method Details

#[](name) ⇒ Object



12
13
14
# File 'lib/hammer_builder/strings_injector.rb', line 12

def [](name)
  strings[name]
end

#add(name, value) ⇒ Object



16
17
18
19
20
# File 'lib/hammer_builder/strings_injector.rb', line 16

def add(name, value)
  name = name.to_sym
  raise "string #{name} is already set to #{value}" if strings.has_key?(name) && self[name] != value
  replace name, value
end

#inject_to(obj) ⇒ Object



28
29
30
31
# File 'lib/hammer_builder/strings_injector.rb', line 28

def inject_to(obj)
  @objects_to_update << obj
  strings.keys.each { |name| update_object obj, name }
end

#replace(name, value) ⇒ Object



22
23
24
25
26
# File 'lib/hammer_builder/strings_injector.rb', line 22

def replace(name, value)
  name          = name.to_sym
  strings[name] = value
  update_objects name
end