class Praxis::MediaType::FieldResolver

Attributes

history[R]

Public Class Methods

new() click to toggle source
# File lib/praxis/media_type.rb, line 94
def initialize
  @history = Hash.new do |hash,key|
    hash[key] = Hash.new
  end
end
resolve(type,fields) click to toggle source
# File lib/praxis/media_type.rb, line 88
def self.resolve(type,fields)
  self.new.resolve(type,fields)
end

Public Instance Methods

deep_merge(target, source) click to toggle source

perform a deep recursive *in place* merge form all values in source onto target

note: can not use ActiveSupport's Hash#deep_merge! because it does not properly do a recursive `deep_merge!`, but instead does `deep_merge`, which destroys the self-referential behavior of field hashes.

note: unlike Hash#merge, doesn't take a block.

# File lib/praxis/media_type.rb, line 153
def deep_merge(target, source)
  source.each do |current_key, source_value|
    target_value = target[current_key]

    target[current_key] = if target_value.is_a?(Hash) && source_value.is_a?(Hash)
      deep_merge(target_value, source_value)
    else
      source_value
    end
  end
  target
end
resolve(type,fields) click to toggle source
# File lib/praxis/media_type.rb, line 100
def resolve(type,fields)
  history_key = fields
  history_type = type
  if fields.kind_of?(Array)
    loop do
      type = type.member_attribute.type
      fields = fields.first
      break unless fields.kind_of?(Array)
    end
  end

  return true if fields == true

  if history[history_type].include? history_key
    return history[history_type][history_key]
  end

  result = history[history_type][history_key] = {}


  fields.each do |name, sub_fields|
    # skip links and do them below
    next if name == :links && defined?(type::Links)

    new_type = type.attributes[name].type
    result[name] = resolve(new_type, sub_fields)
  end

  # now to tackle whatever links there may be
  if defined?(type::Links) &&(links_fields = fields[:links])
    resolved_links = resolve_links(type::Links, links_fields)
    self.deep_merge(result, resolved_links)
  end

  result
end