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
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/fortitude/support/staticized_method.rb', line 30

def create_method!
  begin
    widget_class.instance_method(method_name)
  rescue NameError => ne
    raise NameError, %{You declared the method #{method_name.inspect} in class
#{widget_class.name.inspect} to be a Fortitude static content method,
but there is no method declared on this class with that name.

(It's possible you simply tried to declare the method static before
it was defined on that class -- the call to 'static' must come
_after_ the definition of the method in the class's source code.)}
  end

  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