Class: Prospecto::PresenterView

Inherits:
Object
  • Object
show all
Defined in:
lib/prospecto/presenter_view.rb

Direct Known Subclasses

ApplicationPresenter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ PresenterView

Returns a new instance of PresenterView.



5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/prospecto/presenter_view.rb', line 5

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



18
19
20
21
22
23
24
25
26
27
# File 'lib/prospecto/presenter_view.rb', line 18

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)
  elsif delegate_obj = __delegates.find{|d| d.respond_to? name}
    delegate_obj.send(name)
  else
    super
  end
end

Class Method Details

.__delegatesObject



42
43
44
# File 'lib/prospecto/presenter_view.rb', line 42

def __delegates
  @__delegates ||= Set.new
end

.__propertiesObject



38
39
40
# File 'lib/prospecto/presenter_view.rb', line 38

def __properties
  @__properties ||= Set.new
end

.accepts(*args) ⇒ Object

Accepts means the view uses the member internally, but it is not available outside the class.



48
49
50
51
52
53
54
55
# File 'lib/prospecto/presenter_view.rb', line 48

def accepts(*args)
  args.each do |name|
    define_method name do
      instance_variable_get("@#{name}")
    end
    protected name
  end
end

.decorates(*args) ⇒ Object

Decorates means that properties of the object will be available directly on the presenter.



67
68
69
70
71
# File 'lib/prospecto/presenter_view.rb', line 67

def decorates(*args)
  args.each do |name|
    __delegates << name
  end
end

.proxies(*args) ⇒ Object

Proxies means that the properties of the object can be accessed directly from this object when prefixed with the object name. (ie: @view.user_full_name)



59
60
61
62
63
64
# File 'lib/prospecto/presenter_view.rb', line 59

def proxies(*args)
  args.each do |name|
    __properties << name
  end
  accepts(*args)
end

Instance Method Details

#__delegatesObject



29
30
31
# File 'lib/prospecto/presenter_view.rb', line 29

def __delegates
  @__delegates ||= Set.new
end