Class: Htmless::StringsInjector

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

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (StringsInjector) initialize(&block)

A new instance of StringsInjector



6
7
8
9
10
# File 'lib/htmless/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

- (Object) objects_to_update (readonly)

Returns the value of attribute objects_to_update



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

def objects_to_update
  @objects_to_update
end

- (Object) strings (readonly)

Returns the value of attribute strings



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

def strings
  @strings
end

Instance Method Details

- (Object) [](name)



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

def [](name)
  strings[name]
end

- (Object) add(name, value)



16
17
18
19
20
# File 'lib/htmless/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

- (Object) inject_to(obj)



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

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

- (Object) replace(name, value)



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

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

- (Object) update_object(obj, name) (private)



39
40
41
# File 'lib/htmless/strings_injector.rb', line 39

def update_object(obj, name)
  obj.instance_variable_set(:@_str_#{name}", self[name])
end

- (Object) update_objects(name) (private)



35
36
37
# File 'lib/htmless/strings_injector.rb', line 35

def update_objects(name)
  objects_to_update.each { |obj| update_object obj, name }
end