Class: ApiView::Base

Inherits:
Hash
  • Object
show all
Defined in:
lib/api_view/base.rb

Direct Known Subclasses

Default

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#objectObject (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_attributesObject



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

.render(obj, scope = {}, options = {}) ⇒ Object



11
12
13
14
# File 'lib/api_view/base.rb', line 11

def render(obj, scope={}, options={})
  options[:use] = self
  ApiView::Engine.render(obj, scope, options)
end

Instance Method Details

#collect_attributesObject



62
63
64
# File 'lib/api_view/base.rb', line 62

def collect_attributes
  # no-op by default
end

#convertObject



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_convertObject

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