module MarkMapper::Plugins::Keys
Constants
- IS_RUBY_1_9
Public Class Methods
new(attrs={}) { |self| ... }
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 266 def initialize(attrs={}) @_new = true init_ivars initialize_default_values(attrs) self.attributes = attrs yield self if block_given? end
Public Instance Methods
[](key_name)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 375 def [](key_name); read_key(key_name); end
[]=(name, value)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 378 def []=(name, value) write_key(name, value) end
attribute(key_name)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 376 def attribute(key_name); read_key(key_name); end
attributes()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 319 def attributes to_marklogic(false).with_indifferent_access end
attributes=(attrs)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 286 def attributes=(attrs) return if attrs == nil || attrs.blank? attrs.each_pair do |key, value| if respond_to?(:"#{key}=") self.send(:"#{key}=", value) else self[key] = value end end end
embedded_keys()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 390 def embedded_keys @embedded_keys ||= keys.values.select(&:embeddable?) end
id()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 342 def id self[:_id] end
id=(value)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 346 def id=(value) if self.class.using_object_id? value = ObjectId.to_marklogic(value) end self[:_id] = value end
initialize_from_database(attrs={}, with_cast = false)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 274 def initialize_from_database(attrs={}, with_cast = false) @_new = false init_ivars initialize_default_values(attrs) load_from_database(attrs, with_cast) self end
key_names()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 382 def key_names @key_names ||= keys.keys end
keys()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 354 def keys self.class.keys end
non_embedded_keys()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 386 def non_embedded_keys @non_embedded_keys ||= keys.values.select { |key| !key.embeddable? } end
persisted?()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 282 def persisted? !new? && !destroyed? end
read_key(key_name)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 358 def read_key(key_name) key_name_sym = key_name.to_sym if @_dynamic_attributes && @_dynamic_attributes.key?(key_name_sym) @_dynamic_attributes[key_name_sym] elsif key = keys[key_name.to_s] if key.ivar && instance_variable_defined?(key.ivar) value = instance_variable_get(key.ivar) else if key.ivar instance_variable_set key.ivar, key.get(nil) else @_dynamic_attributes[key_name_sym] = key.get(nil) end end end end
to_marklogic(include_abbreviatons = true)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 298 def to_marklogic(include_abbreviatons = true) Hash.new.tap do |attrs| self.class.unaliased_keys.each do |name, key| value = self.read_key(key.name) if key.type == ObjectId || !value.nil? attrs[include_abbreviatons && key.persisted_name || name] = key.set(value) end end embedded_associations.each do |association| if documents = instance_variable_get(association.ivar) if association.is_a?(Associations::OneAssociation) attrs[association.name] = documents.to_marklogic else attrs[association.name] = documents.map(&:to_marklogic) end end end end end
update(attrs={})
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 323 def update(attrs={}) self.attributes = attrs save end
Also aliased as: update_attributes
update!(attrs={})
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 330 def update!(attrs={}) self.attributes = attrs save! end
Also aliased as: update_attributes!
update_attribute(name, value)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 337 def update_attribute(name, value) self.send(:"#{name}=", value) save(:validate => false) end
Protected Instance Methods
unalias_key(name)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 396 def unalias_key(name) name = name.to_s if key = keys[name] key.name else name end end
Private Instance Methods
dynamic_key(name)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 455 def dynamic_key(name) self.class.key(name, :__dynamic => true) end
init_ivars()
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 407 def init_ivars @__mm_keys = self.class.keys # Not dumpable @__mm_default_keys = @__mm_keys.values.select(&:default?) # Not dumpable @_dynamic_attributes = {} # Dumpable end
initialize_default_values(except = {})
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 459 def initialize_default_values(except = {}) @__mm_default_keys.each do |key| if !(except && except.key?(key.name)) internal_write_key key.name, key.default_value, false end end end
internal_write_key(name, value, cast = true)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 438 def internal_write_key(name, value, cast = true) key = @__mm_keys[name] || dynamic_key(name) as_marklogic = cast ? key.set(value) : value as_typecast = key.get(as_marklogic) if key.ivar if key.embeddable? set_parent_document(key, value) set_parent_document(key, as_typecast) end instance_variable_set key.ivar, as_typecast else @_dynamic_attributes[key.name.to_sym] = as_typecast end @attributes = nil value end
load_from_database(attrs, with_cast = false)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 413 def load_from_database(attrs, with_cast = false) return if attrs == nil || attrs.blank? attrs.each do |key, value| if !@__mm_keys.key?(key) && respond_to?(:"#{key}=") self.send(:"#{key}=", value) else internal_write_key key, value, with_cast end end end
set_parent_document(key, value)
click to toggle source
# File lib/mark_mapper/plugins/keys.rb, line 425 def set_parent_document(key, value) if key.type and value.instance_of?(key.type) && key.embeddable? && value.respond_to?(:_parent_document) value._parent_document = self end end
write_key(name, value)
click to toggle source
This exists to be patched over by plugins, while letting us still get to the undecorated version of the method.
# File lib/mark_mapper/plugins/keys.rb, line 433 def write_key(name, value) init_ivars unless @__mm_keys internal_write_key(name.to_s, value) end