Class: ActsAsApi::ApiTemplate

Inherits:
Hash
  • Object
show all
Defined in:
lib/acts_as_api/api_template.rb

Overview

Represents an api template for a model. This class should not be initiated by yourself, api templates are created by defining them in the model by calling: api_accessible.

The api template is configured in the block passed to api_accessible.

Please note that ApiTemplate inherits from Hash so you can use all kind of Hash and Enumerable methods to manipulate the template.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_template) ⇒ ApiTemplate

Returns a new instance of ApiTemplate.


18
19
20
21
# File 'lib/acts_as_api/api_template.rb', line 18

def initialize(api_template)
  self.api_template = api_template
  @options ||= {}
end

Instance Attribute Details

#api_templateObject

The name of the api template as a Symbol.


14
15
16
# File 'lib/acts_as_api/api_template.rb', line 14

def api_template
  @api_template
end

#optionsObject (readonly)

Returns the value of attribute options


16
17
18
# File 'lib/acts_as_api/api_template.rb', line 16

def options
  @options
end

Instance Method Details

#add(val, options = {}) ⇒ Object

Adds a field to the api template

The value passed can be one of the following:

* Symbol - the method with the same name will be called on the model when rendering.
* String - must be in the form "method1.method2.method3", will call this method chain.
* Hash - will be added as a sub hash and all its items will be resolved the way described above.

Possible options to pass:

* :template - Determine the template that should be used to render the item if it is
  +api_accessible+ itself.

38
39
40
41
42
43
44
# File 'lib/acts_as_api/api_template.rb', line 38

def add(val, options = {})
  item_key = (options[:as] || val).to_sym

  self[item_key] = val

  @options[item_key] = options
end

#allowed_to_render?(fieldset, field, model, options) ⇒ Boolean

Decides if the passed item should be added to the response based on the conditional options passed.

Returns:

  • (Boolean)

69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/acts_as_api/api_template.rb', line 69

def allowed_to_render?(fieldset, field, model, options)
  return true unless fieldset.is_a? ActsAsApi::ApiTemplate

  fieldset_options = fieldset.options_for(field)

  if fieldset_options[:unless]
    !condition_fulfilled?(model, fieldset_options[:unless], options)
  elsif fieldset_options[:if]
    condition_fulfilled?(model, fieldset_options[:if], options)
  else
    true
  end
end

#api_template_for(fieldset, field) ⇒ Object

If a special template name for the passed item is specified it will be returned, if not the original api template.


63
64
65
# File 'lib/acts_as_api/api_template.rb', line 63

def api_template_for(fieldset, field)
  fieldset.option_for(field, :template) || api_template
end

#condition_fulfilled?(model, condition, options) ⇒ Boolean

Checks if a condition is fulfilled (result is not nil or false)

Returns:

  • (Boolean)

85
86
87
88
89
90
91
92
93
# File 'lib/acts_as_api/api_template.rb', line 85

def condition_fulfilled?(model, condition, options)
  case condition
  when Symbol
    result = model.send(condition)
  when Proc
    result = call_proc(condition, model, options)
  end
  !!result
end

#merge!(other_hash, &block) ⇒ Object


23
24
25
26
# File 'lib/acts_as_api/api_template.rb', line 23

def merge!(other_hash, &block)
  super
  options.merge!(other_hash.options) if other_hash.respond_to?(:options)
end

#option_for(field, option) ⇒ Object

Returns the passed option of a field in the api template


57
58
59
# File 'lib/acts_as_api/api_template.rb', line 57

def option_for(field, option)
  @options[field][option] if @options[field]
end

#options_for(field) ⇒ Object

Returns the options of a field in the api template


52
53
54
# File 'lib/acts_as_api/api_template.rb', line 52

def options_for(field)
  @options[field]
end

#remove(field) ⇒ Object

Removes a field from the template


47
48
49
# File 'lib/acts_as_api/api_template.rb', line 47

def remove(field)
  delete(field)
end

#to_response_hash(model, fieldset = self, options = {}) ⇒ Object

Generates a hash that represents the api response based on this template for the passed model instance.


97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_api/api_template.rb', line 97

def to_response_hash(model, fieldset = self, options = {})
  api_output = {}

  fieldset.each do |field, value|
    next unless allowed_to_render?(fieldset, field, model, options)

    out = process_value(model, value, options)

    if out.respond_to?(:as_api_response)
      sub_template = api_template_for(fieldset, field)
      out = out.as_api_response(sub_template, options)
    end

    api_output[field] = out
  end

  api_output
end