Class: Fortitude::Support::StaticizedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/fortitude/support/staticized_method.rb

Instance Method Summary collapse

Constructor Details

#initialize(widget_class, method_name, options = { }) ⇒ StaticizedMethod

Returns a new instance of StaticizedMethod.



4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/fortitude/support/staticized_method.rb', line 4

def initialize(widget_class, method_name, options = { })
  @widget_class = widget_class
  @method_name = method_name

  @output_by_locale = { }
  @has_yield = false

  @options = options
  @options.assert_valid_keys(:locale_support)

  set_constant!
end

Instance Method Details

#create_method!Object



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/fortitude/support/staticized_method.rb', line 30

def create_method!
  unless widget_class.instance_methods.map(&:to_s).include?(dynamic_method_name.to_s)
    widget_class.send(:alias_method, dynamic_method_name, method_name)
  end

  widget_class.class_eval <<-EOS
    def #{method_name}
#{constant_name}.run!(self) { yield }
    end
  EOS
end

#run!(widget) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/fortitude/support/staticized_method.rb', line 17

def run!(widget)
  locale = locale_support? ? widget.widget_locale : nil
  output = (@output_by_locale[locale] ||= generate_content!(widget))

  if output.kind_of?(Array)
    widget.rawtext(output[0])
    yield
    widget.rawtext(output[1])
  else
    widget.rawtext(output)
  end
end