Class: Prospecto::PresenterView
- Inherits:
-
Object
- Object
- Prospecto::PresenterView
- Defined in:
- lib/prospecto/presenter_view.rb
Overview
Optional base class for convenience when working with rails presenters.
Direct Known Subclasses
Class Method Summary collapse
- .__delegates ⇒ Object
- .__properties ⇒ Object
-
.accepts(*args) ⇒ Object
Instructs the class to accept the value internally but not publish it.
-
.decorates(*args) ⇒ Object
Instructs the class to proxy directly for this object like a true decorator.
-
.presents(*args) ⇒ Object
Instructs the class to create a publicly visible version of the value.
-
.proxies(*args) ⇒ Object
Instructs the class that the properties of the object can be accessed directly from this object when prefixed with the object name.
Instance Method Summary collapse
-
#initialize(args = {}) ⇒ PresenterView
constructor
Initialize a new PresenterView instance accepting the provided arguments.
- #method_missing(name, *args, &block) ⇒ Object
Constructor Details
#initialize(args = {}) ⇒ PresenterView
Initialize a new PresenterView instance accepting the provided arguments.
10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/prospecto/presenter_view.rb', line 10 def initialize(args={}) args.each do |name, value| if respond_to? name instance_variable_set("@#{name}", value) elsif self.class.__delegates.include?(name) __delegates << value else # Stop everything there is a design problem. raise ArgumentError.new("Unknown property '#{name}' for class '#{self.class.name}'.") end end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/prospecto/presenter_view.rb', line 24 def method_missing(name, *args, &block) if property_name = self.class.__properties.find{|m| name.to_s.start_with? "#{m}_"} field_name = name.to_s.sub("#{property_name}_", "") self.send(property_name).send(field_name, *args, &block) elsif delegate_obj = __delegates.find{|d| d.respond_to? name} delegate_obj.send(name, *args, &block) else super end end |
Class Method Details
.__delegates ⇒ Object
90 91 92 |
# File 'lib/prospecto/presenter_view.rb', line 90 def __delegates @__delegates ||= Set.new end |
.__properties ⇒ Object
85 86 87 |
# File 'lib/prospecto/presenter_view.rb', line 85 def __properties @__properties ||= Set.new end |
.accepts(*args) ⇒ Object
Instructs the class to accept the value internally but not publish it.
54 55 56 57 58 59 60 61 |
# File 'lib/prospecto/presenter_view.rb', line 54 def accepts(*args) args.each do |name| define_method name do instance_variable_get("@#{name}") end protected name end end |
.decorates(*args) ⇒ Object
Instructs the class to proxy directly for this object like a true decorator. This is very similar to how something like [draper](github.com/jcasimir/draper) works.
78 79 80 81 82 |
# File 'lib/prospecto/presenter_view.rb', line 78 def decorates(*args) args.each do |name| __delegates << name end end |
.presents(*args) ⇒ Object
Instructs the class to create a publicly visible version of the value.
46 47 48 |
# File 'lib/prospecto/presenter_view.rb', line 46 def presents(*args) attr_reader(*args) end |
.proxies(*args) ⇒ Object
Instructs the class that the properties of the object can be accessed directly from this object when prefixed with the object name. (ie: ‘@view.user_full_name`)
67 68 69 70 71 72 |
# File 'lib/prospecto/presenter_view.rb', line 67 def proxies(*args) args.each do |name| __properties << name end accepts(*args) end |