Class: ApiView::Base
- Inherits:
-
Hash
- Object
- Hash
- ApiView::Base
- Defined in:
- lib/api_view/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#object ⇒ Object
(also: #obj)
readonly
Returns the value of attribute object.
Class Method Summary collapse
-
.attributes(*attrs) ⇒ Object
(also: attrs)
defines the basic (flat) fields that will be copied from the main object.
- .for_model(model) ⇒ Object
- .main_object(main_object_name) ⇒ Object
- .parent_attributes ⇒ Object
- .render(obj, scope = {}, options = {}) ⇒ Object
Instance Method Summary collapse
- #collect_attributes ⇒ Object
- #convert ⇒ Object
-
#field(fieldname, field_object, opts = {}) ⇒ Object
hides the details for serialization implementation.
-
#initialize(object) ⇒ Base
constructor
A new instance of Base.
-
#instance_convert ⇒ Object
this is the method that is supposed to be overriden in the subclass.
Constructor Details
#initialize(object) ⇒ Base
Returns a new instance of Base.
57 58 59 60 |
# File 'lib/api_view/base.rb', line 57 def initialize(object) super(nil) @object = object end |
Instance Attribute Details
#object ⇒ Object (readonly) Also known as: obj
Returns the value of attribute object.
54 55 56 |
# File 'lib/api_view/base.rb', line 54 def object @object end |
Class Method Details
.attributes(*attrs) ⇒ Object Also known as: attrs
defines the basic (flat) fields that will be copied from the main object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/api_view/base.rb', line 28 def attributes(*attrs) @attributes ||= [] @attributes = (@attributes + attrs).flatten parent_attributes.reverse.each do |a| @attributes.unshift(a) if not @attributes.include? a end # create a method which reads each attribute from the model object and # copies it into the hash, then returns the hash itself # e.g., # def collect_attributes # self.store(:foo, @object.foo) # ... # self # end code = ["def collect_attributes()"] @attributes.each do |a| code << "self.store(:#{a}, @object.#{a})" end code << "end" class_eval(code.join("\n")) end |
.for_model(model) ⇒ Object
7 8 9 |
# File 'lib/api_view/base.rb', line 7 def for_model(model) ApiView::Registry.add_model(model, self) end |
.main_object(main_object_name) ⇒ Object
23 24 25 |
# File 'lib/api_view/base.rb', line 23 def main_object(main_object_name) alias_method main_object_name, :object end |
.parent_attributes ⇒ Object
16 17 18 19 20 |
# File 'lib/api_view/base.rb', line 16 def parent_attributes parent = self.superclass return [] if parent.name == "ApiView::Base" return parent.instance_variable_get(:@attributes) end |
Instance Method Details
#collect_attributes ⇒ Object
62 63 64 |
# File 'lib/api_view/base.rb', line 62 def collect_attributes # no-op by default end |
#convert ⇒ Object
71 72 73 74 75 |
# File 'lib/api_view/base.rb', line 71 def convert collect_attributes() instance_convert self end |
#field(fieldname, field_object, opts = {}) ⇒ Object
hides the details for serialization implementation
78 79 80 81 82 83 84 85 86 |
# File 'lib/api_view/base.rb', line 78 def field(fieldname, field_object, opts={}) serializer = opts[:via] value = if serializer serializer.new(field_object).convert else ApiView::Engine.convert(field_object) end store fieldname, value end |
#instance_convert ⇒ Object
this is the method that is supposed to be overriden in the subclass
67 68 69 |
# File 'lib/api_view/base.rb', line 67 def instance_convert # no-op by default, override in you subclass end |