Class: Hash

Inherits:
Object
  • Object
show all
Includes:
ToApiFilter, ToApiInstanceMethods
Defined in:
lib/to_api.rb

Instance Method Summary collapse

Methods included from ToApiInstanceMethods

#build_to_api_include_hash

Methods included from ToApiFilter

#add_to_api_filter, #to_api_filters, #to_api_filters=

Instance Method Details

#to_api(*includes) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/to_api.rb', line 151

def to_api(*includes)
  values = {}

  include_hash = build_to_api_include_hash(*includes)

  include_hash.keys.each do |inc|
    if to_api_filters.has_key?(inc)
      child_includes = include_hash[inc]
      values[inc] = to_api_filters[inc].call self, child_includes
    end
  end

  (keys-values.keys).each do |k|
    val = self[k]
    val.to_api_filters = to_api_filters if to_api_filters.any? && val.respond_to?(:to_api_filters)
    child_includes = include_hash[k] || []
    values[k] = val.to_api(*[child_includes].flatten.compact)
  end
  values
end