Module: Praxis::Mapper::SequelCompat

Extended by:
ActiveSupport::Concern
Defined in:
lib/praxis-mapper/sequel_compat.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_load_associated_objects(opts, dynamic_opts = OPTS) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/praxis-mapper/sequel_compat.rb', line 52

def _load_associated_objects(opts, dynamic_opts=OPTS)
  return super if self.identity_map.nil?
  target = opts.associated_class
  key = opts[:key]

  case opts[:type]
  when :many_to_one
    val = if key.kind_of?(Array)
      @values.values_at(*key)
    else
      @values[key]
    end
    return nil if val.nil?
    self.identity_map.get(target, target.primary_key => val)
  when :one_to_many
    self.identity_map.all(target, key => [pk] )
  when :many_to_many
    # OPTIMIZE: cache this result
    join_model = opts[:join_model].constantize

    left_key = opts[:left_key]
    right_key = opts[:right_key]

    right_values = self.identity_map.
      all(join_model, left_key => Array(values[primary_key])).
      collect(&right_key)

    self.identity_map.all(target, target.primary_key => right_values )
  else
    raise "#{opts[:type]} is not currently supported"
  end
end

#identitiesObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/praxis-mapper/sequel_compat.rb', line 86

def identities
  self.class.identities.each_with_object(Hash.new) do |identity, hash|
    case identity
    when Symbol
      hash[identity] = values[identity].freeze
    else
      hash[identity] = values.values_at(*identity).collect(&:freeze)
    end
  end
end