class ApiView::Base
Attributes
obj[R]
object[R]
Public Class Methods
attributes(*attrs)
click to toggle source
defines the basic (flat) fields that will be copied from the main object
# 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
Also aliased as: attrs
for_model(model)
click to toggle source
# File lib/api_view/base.rb, line 7 def for_model(model) ApiView::Registry.add_model(model, self) end
main_object(main_object_name)
click to toggle source
# File lib/api_view/base.rb, line 23 def main_object(main_object_name) alias_method main_object_name, :object end
new(object)
click to toggle source
Calls superclass method
# File lib/api_view/base.rb, line 57 def initialize(object) super(nil) @object = object end
parent_attributes()
click to toggle source
# 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
render(obj, scope={}, options={})
click to toggle source
# File lib/api_view/base.rb, line 11 def render(obj, scope={}, options={}) options[:use] = self ApiView::Engine.render(obj, scope, options) end
Public Instance Methods
collect_attributes()
click to toggle source
# File lib/api_view/base.rb, line 62 def collect_attributes # no-op by default end
convert()
click to toggle source
# File lib/api_view/base.rb, line 71 def convert collect_attributes() instance_convert self end
field(fieldname, field_object, opts={})
click to toggle source
hides the details for serialization implementation
# 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()
click to toggle source
this is the method that is supposed to be overriden in the subclass
# File lib/api_view/base.rb, line 67 def instance_convert # no-op by default, override in you subclass end