class Grape::Roar::Extensions::Relations::Mapper
Attributes
config[R]
entity[R]
model_klass[R]
Public Class Methods
new(entity)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 10 def initialize(entity) @entity = entity @config = {} end
Public Instance Methods
adapter()
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 15 def adapter @adapter ||= Adapters.for(model_klass) end
decorate(klass)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 19 def decorate(klass) @model_klass = klass config.each_pair do |relation, opts| representer_for(relation.to_s, opts) unless opts.key(:extend) map_relation(relation, opts) end end
Private Instance Methods
find_representer(base, target_name, const)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 34 def find_representer(base, target_name, const) const = base.const_get(const) return false if const.nil? || !const.is_a?(Module) (const < ::Roar::JSON::HAL) && const.name .downcase .include?(target_name.singularize) end
map_collection(relation, opts)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 42 def map_collection(relation, opts) return entity.link_relation(relation, true) unless opts.fetch(:embedded, false) entity.collection(relation, opts) end
map_relation(relation, opts)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 47 def map_relation(relation, opts) map = if adapter.collection_methods.include?(opts[:relation_kind]) :map_collection elsif adapter.single_entity_methods .include?(opts[:relation_kind]) || opts[:relation_kind] == :self :map_single_entity else raise Exceptions::UnsupportedRelationError, 'No such relation supported' end send(map, relation, opts) validate_relation(relation, opts[:relation_kind]) end
map_single_entity(relation, opts)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 61 def map_single_entity(relation, opts) return entity.map_self_url if opts[:relation_kind] == :self return entity.link_relation(relation) unless opts.fetch(:embedded, false) entity.property(relation, opts) end
representer_for(relation, opts)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 67 def representer_for(relation, opts) base_path = entity.name.deconstantize base_path = base_path.empty? ? Object : base_path.safe_constantize return if base_path.nil? to_extend = base_path.constants .find(&method(:find_representer).curry[ base_path, relation ]) opts.merge!(extend: "#{base_path}::#{to_extend}".safe_constantize) end
validate_relation(relation, kind)
click to toggle source
# File lib/grape/roar/extensions/relations/mapper.rb, line 80 def validate_relation(relation, kind) validator_method = "#{kind}_valid?" return true unless adapter.respond_to?(validator_method) adapter.send(validator_method, relation.to_s) end