Class: ActsAsApi::ApiTemplate
- Inherits:
-
Hash
- Object
- Hash
- ActsAsApi::ApiTemplate
- 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
-
#api_template ⇒ Object
The name of the api template as a Symbol.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
-
#add(val, options = {}) ⇒ Object
Adds a field to the api template.
-
#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.
-
#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.
-
#condition_fulfilled?(model, condition, options) ⇒ Boolean
Checks if a condition is fulfilled (result is not nil or false).
-
#initialize(api_template) ⇒ ApiTemplate
constructor
A new instance of ApiTemplate.
- #merge!(other_hash, &block) ⇒ Object
-
#option_for(field, option) ⇒ Object
Returns the passed option of a field in the api template.
-
#options_for(field) ⇒ Object
Returns the options of a field in the api template.
-
#remove(field) ⇒ Object
Removes a field from the template.
-
#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.
Constructor Details
#initialize(api_template) ⇒ ApiTemplate
Returns a new instance of ApiTemplate.
17 18 19 20 |
# File 'lib/acts_as_api/api_template.rb', line 17 def initialize(api_template) self.api_template = api_template @options ||= {} end |
Instance Attribute Details
#api_template ⇒ Object
The name of the api template as a Symbol.
13 14 15 |
# File 'lib/acts_as_api/api_template.rb', line 13 def api_template @api_template end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/acts_as_api/api_template.rb', line 15 def @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.
37 38 39 40 41 42 43 |
# File 'lib/acts_as_api/api_template.rb', line 37 def add(val, = {}) item_key = ([:as] || val).to_sym self[item_key] = val @options[item_key] = 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.
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/acts_as_api/api_template.rb', line 68 def allowed_to_render?(fieldset, field, model, ) return true unless fieldset.is_a? ActsAsApi::ApiTemplate = fieldset.(field) if [:unless] !(condition_fulfilled?(model, [:unless], )) elsif [:if] condition_fulfilled?(model, [:if], ) 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.
62 63 64 |
# File 'lib/acts_as_api/api_template.rb', line 62 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)
84 85 86 87 88 89 90 91 92 |
# File 'lib/acts_as_api/api_template.rb', line 84 def condition_fulfilled?(model, condition, ) case condition when Symbol result = model.send(condition) when Proc result = call_proc(condition, model, ) end !!result end |
#merge!(other_hash, &block) ⇒ Object
22 23 24 25 |
# File 'lib/acts_as_api/api_template.rb', line 22 def merge!(other_hash, &block) super self..merge!(other_hash.) if other_hash.respond_to?(:options) end |
#option_for(field, option) ⇒ Object
Returns the passed option of a field in the api template
56 57 58 |
# File 'lib/acts_as_api/api_template.rb', line 56 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
51 52 53 |
# File 'lib/acts_as_api/api_template.rb', line 51 def (field) @options[field] end |
#remove(field) ⇒ Object
Removes a field from the template
46 47 48 |
# File 'lib/acts_as_api/api_template.rb', line 46 def remove(field) self.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.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/acts_as_api/api_template.rb', line 96 def to_response_hash(model, fieldset = self, = {}) api_output = {} fieldset.each do |field, value| next unless allowed_to_render?(fieldset, field, model, ) out = process_value(model, value, ) if out.respond_to?(:as_api_response) sub_template = api_template_for(fieldset, field) out = out.as_api_response(sub_template, ) end api_output[field] = out end api_output end |