Module: PresenterRails
- Defined in:
- lib/presenter_rails.rb,
lib/presenter_rails/version.rb
Constant Summary collapse
- VERSION =
"3.0.0"
Class Method Summary collapse
-
.ivar_for(name) ⇒ Object
Internal: Name of the instance variable used to memoize a presenter method.
-
.method_name_for(name) ⇒ Object
Internal: Name of the presenter method as defined in the controller.
Instance Method Summary collapse
-
#present(name, &block) ⇒ Object
Public: Defines a method and makes it available to the view context under the specified name as a memoized variable.
Class Method Details
.ivar_for(name) ⇒ Object
Internal: Name of the instance variable used to memoize a presenter method.
35 36 37 |
# File 'lib/presenter_rails.rb', line 35 def self.ivar_for(name) "@_presenter_#{ name.to_s.gsub("?", "_query").gsub("!", "_bang") }" end |
.method_name_for(name) ⇒ Object
Internal: Name of the presenter method as defined in the controller.
30 31 32 |
# File 'lib/presenter_rails.rb', line 30 def self.method_name_for(name) "#{ name }_presenter" end |
Instance Method Details
#present(name, &block) ⇒ Object
Public: Defines a method and makes it available to the view context under the specified name as a memoized variable.
name - The name of the method as called from the view context. block - Executed once if (and only if) the method is called.
Returns nothing.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/presenter_rails.rb', line 12 def present(name, &block) presenter_method = PresenterRails.method_name_for(name) ivar = PresenterRails.ivar_for(name) private define_method(presenter_method) { if instance_variable_defined?(ivar) instance_variable_get(ivar) else instance_variable_set(ivar, instance_exec(&block)) end } helper Module.new { define_method(name) { controller.send(presenter_method) } } end |