Module: MarkMapper::Plugins::Keys::ClassMethods

Defined in:
lib/mark_mapper/plugins/keys.rb

Instance Method Summary collapse

Instance Method Details

#dealias_key(name) ⇒ Object


49
50
51
52
# File 'lib/mark_mapper/plugins/keys.rb', line 49

def dealias_key(name)
  key = keys[name.to_s]
  key && key.abbr || k
end

#dealias_keys(hash) ⇒ Object Also known as: dealias, unalias


39
40
41
42
43
44
45
46
47
# File 'lib/mark_mapper/plugins/keys.rb', line 39

def dealias_keys(hash)
  out = {}
  hash.each do |k, v|
    key = keys[k.to_s]
    name = key && key.abbr || k
    out[name] = k.to_s.match(/^\$/) && v.is_a?(Hash) ? dealias_keys(v) : v
  end
  out
end

#defined_keysObject


31
32
33
# File 'lib/mark_mapper/plugins/keys.rb', line 31

def defined_keys
  @defined_keys ||= Hash[*unaliased_keys.select {|k, v| !v.dynamic? }.flatten(1)]
end

#dynamic_keysObject


27
28
29
# File 'lib/mark_mapper/plugins/keys.rb', line 27

def dynamic_keys
  @dynamic_keys ||= Hash[*unaliased_keys.select {|k, v| v.dynamic? }.flatten(1)]
end

#from_marklogic(value) ⇒ Object


112
113
114
# File 'lib/mark_mapper/plugins/keys.rb', line 112

def from_marklogic(value)
  value && (value.instance_of?(self) ? value : load(value))
end

#inherited(descendant) ⇒ Object


18
19
20
21
# File 'lib/mark_mapper/plugins/keys.rb', line 18

def inherited(descendant)
  descendant.instance_variable_set(:@keys, keys.dup)
  super
end

#key(*args) ⇒ Object


57
58
59
60
61
62
63
64
65
66
67
# File 'lib/mark_mapper/plugins/keys.rb', line 57

def key(*args)
  Key.new(*args).tap do |key|
    keys[key.name] = key
    keys[key.abbr] = key if key.abbr
    create_accessors_for(key) if key.valid_ruby_name? && !key.reserved_name?
    create_key_in_descendants(*args)
    create_indexes_for(key)
    create_validations_for(key)
    @dynamic_keys = @defined_keys = @unaliased_keys = @object_id_keys = nil
  end
end

#key?(key) ⇒ Boolean

Returns:


92
93
94
# File 'lib/mark_mapper/plugins/keys.rb', line 92

def key?(key)
  keys.key? key.to_s
end

#keysObject


23
24
25
# File 'lib/mark_mapper/plugins/keys.rb', line 23

def keys
  @keys ||= {}
end

#load(attrs, with_cast = false) ⇒ Object

load is overridden in identity map to ensure same objects are loaded


117
118
119
120
121
122
123
124
# File 'lib/mark_mapper/plugins/keys.rb', line 117

def load(attrs, with_cast = false)
  return nil if attrs.nil?
  begin
    attrs['_type'] ? attrs['_type'].constantize : self
  rescue NameError
    self
  end.allocate.initialize_from_database(attrs, with_cast)
end

#object_id_key?(name) ⇒ Boolean

Returns:


104
105
106
# File 'lib/mark_mapper/plugins/keys.rb', line 104

def object_id_key?(name)
  object_id_keys.include?(name.to_sym)
end

#object_id_keysObject


100
101
102
# File 'lib/mark_mapper/plugins/keys.rb', line 100

def object_id_keys
  @object_id_keys ||= unaliased_keys.keys.select { |key| keys[key].type == ObjectId }.map(&:to_sym)
end

#persisted_name(name) ⇒ Object Also known as: abbr


83
84
85
86
87
88
89
# File 'lib/mark_mapper/plugins/keys.rb', line 83

def persisted_name(name)
  if key = keys[name.to_s]
    key.persisted_name
  else
    name
  end
end

#remove_key(name) ⇒ Object


69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mark_mapper/plugins/keys.rb', line 69

def remove_key(name)
  if key = keys[name.to_s]
    keys.delete key.name
    keys.delete key.abbr
    remove_method key.name if respond_to? "#{key.name}"
    remove_method "#{key.name}=" if respond_to? "#{key.name}="
    remove_method "#{key.name}?" if respond_to? "#{key.name}?"
    remove_method "#{key.name}_before_type_cast" if respond_to? "#{key.name}_before_type_cast"
    remove_key_in_descendants key.name
    remove_validations_for key.name
    @dynamic_keys = @defined_keys = @unaliased_keys = @object_id_keys = nil
  end
end

#to_marklogic(instance) ⇒ Object


108
109
110
# File 'lib/mark_mapper/plugins/keys.rb', line 108

def to_marklogic(instance)
  instance && instance.to_marklogic
end

#unaliased_keysObject


35
36
37
# File 'lib/mark_mapper/plugins/keys.rb', line 35

def unaliased_keys
  @unaliased_keys ||= Hash[*keys.select {|k, v| k == v.name }.flatten(1)]
end

#using_object_id?Boolean

Returns:


96
97
98
# File 'lib/mark_mapper/plugins/keys.rb', line 96

def using_object_id?
  object_id_key?(:_id)
end