Class: Depict::Presenter
- Inherits:
-
Object
show all
- Defined in:
- lib/depict/presenter.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(object) ⇒ Presenter
Returns a new instance of Presenter.
24
25
26
|
# File 'lib/depict/presenter.rb', line 24
def initialize(object)
@object = object
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args) ⇒ Object
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/depict/presenter.rb', line 44
def method_missing(method, *args)
mapping = self.class.mappings.detect {|x| x.target_name == method }
if mapping
attrs = {}
mapping.serialize(@object, attrs)
attrs[method]
else
super(method, *args)
end
end
|
Class Method Details
.define(&block) ⇒ Object
5
6
7
8
9
|
# File 'lib/depict/presenter.rb', line 5
def define(&block)
klass = Class.new(self)
klass.instance_eval(&block)
klass
end
|
.mappings ⇒ Object
11
12
13
|
# File 'lib/depict/presenter.rb', line 11
def mappings
@mappings ||= superclass == Object ? [] : superclass.mappings.dup
end
|
.maps(name, options = {}) ⇒ Object
19
20
21
|
# File 'lib/depict/presenter.rb', line 19
def maps(name, options={})
mappings.push Mapping.new(name, options)
end
|
.target_attribute_names ⇒ Object
15
16
17
|
# File 'lib/depict/presenter.rb', line 15
def target_attribute_names
mappings.map(&:target_name)
end
|
Instance Method Details
#attributes=(attributes) ⇒ Object
32
33
34
|
# File 'lib/depict/presenter.rb', line 32
def attributes=(attributes)
self.class.mappings.each {|mapping| mapping.deserialize(@object, attributes) }
end
|
#respond_to?(method, include_private = false) ⇒ Boolean
36
37
38
39
40
41
42
|
# File 'lib/depict/presenter.rb', line 36
def respond_to?(method, include_private=false)
if self.class.target_attribute_names.include? method
return true
else
super(method, include_private)
end
end
|
#to_hash ⇒ Object
28
29
30
|
# File 'lib/depict/presenter.rb', line 28
def to_hash
self.class.mappings.inject({}) {|attrs, mapping| mapping.serialize(@object, attrs); attrs }
end
|