Class: MongoMapper::Plugins::Keys::Key
- Defined in:
- lib/mongo_mapper/plugins/keys/key.rb
Constant Summary collapse
- RESERVED_KEYS =
%w( id class object_id attributes )
- ID_STR =
'_id'
Instance Attribute Summary collapse
-
#abbr ⇒ Object
Returns the value of attribute abbr.
-
#accessors ⇒ Object
Returns the value of attribute accessors.
-
#default ⇒ Object
Returns the value of attribute default.
-
#ivar ⇒ Object
Returns the value of attribute ivar.
-
#name ⇒ Object
Returns the value of attribute name.
-
#options ⇒ Object
Returns the value of attribute options.
-
#type ⇒ Object
Returns the value of attribute type.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #any_accessor?(arr_opt = []) ⇒ Boolean
- #default? ⇒ Boolean
- #default_value ⇒ Object
- #dynamic? ⇒ Boolean
- #embeddable? ⇒ Boolean
- #get(value) ⇒ Object
-
#initialize(*args) ⇒ Key
constructor
A new instance of Key.
- #number? ⇒ Boolean
- #persisted_name ⇒ Object
- #predicate_accessor? ⇒ Boolean
- #read_accessor? ⇒ Boolean
- #reserved_name? ⇒ Boolean
- #set(value) ⇒ Object
- #valid_ruby_name? ⇒ Boolean
- #write_accessor? ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ Key
Returns a new instance of Key.
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 11 def initialize(*args) = args. @name, @type = args.shift.to_s, args.shift self. = ( || {}).symbolize_keys @dynamic = !![:__dynamic] @embeddable = type.respond_to?(:embeddable?) ? type. : false @is_id = @name == ID_STR @typecast = @options[:typecast] @accessors = Array(@options[:accessors]).compact.map &:to_s @has_default = !!.key?(:default) self.default = self.[:default] if default? if abbr = @options[:abbr] || @options[:alias] || @options[:field_name] @abbr = abbr.to_s elsif @name.match(/^[A-Z]/) and !dynamic? @abbr = @name @name = @name.gsub(/^([A-Z])/) {|m| m.downcase } Kernel.warn "Key names may not start with uppercase letters. If your field starts " + "with an uppercase letter, use :field_name to specify the real field name. " + "Accessors called `#{@name}` have been created instead." end @ivar = :"@#{name}" if valid_ruby_name? validate_key_name! unless dynamic? or !any_accessor? end |
Instance Attribute Details
#abbr ⇒ Object
Returns the value of attribute abbr.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def abbr @abbr end |
#accessors ⇒ Object
Returns the value of attribute accessors.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def accessors @accessors end |
#default ⇒ Object
Returns the value of attribute default.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def default @default end |
#ivar ⇒ Object
Returns the value of attribute ivar.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def ivar @ivar end |
#name ⇒ Object
Returns the value of attribute name.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def name @name end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def @options end |
#type ⇒ Object
Returns the value of attribute type.
9 10 11 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 9 def type @type end |
Instance Method Details
#==(other) ⇒ Object
40 41 42 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 40 def ==(other) @name == other.name && @type == other.type && @abbr == other.abbr end |
#any_accessor?(arr_opt = []) ⇒ Boolean
121 122 123 124 125 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 121 def any_accessor?(arr_opt = []) return true if @accessors.empty? return false unless (@accessors & ["skip", "none"]).empty? return !(@accessors & arr_opt).empty? end |
#default? ⇒ Boolean
52 53 54 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 52 def default? @has_default end |
#default_value ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 89 def default_value return unless default? if default.instance_of? Proc default.call else # Using Marshal is easiest way to get a copy of mutable objects # without getting an error on immutable objects Marshal.load(Marshal.dump(default)) end end |
#dynamic? ⇒ Boolean
56 57 58 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 56 def dynamic? @dynamic end |
#embeddable? ⇒ Boolean
44 45 46 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 44 def @embeddable end |
#get(value) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 60 def get(value) # Special Case: Generate default _id on access value = default_value if @is_id and !value value = type ? type.from_mongo(value) : value if @typecast klass = typecast_class # Don't make this lookup on every call # typecast assumes array-ish object. value = value.map { |v| klass.from_mongo(v) } # recast it in the original type value = type.from_mongo(value) end value end |
#number? ⇒ Boolean
48 49 50 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 48 def number? type == Integer || type == Float end |
#persisted_name ⇒ Object
36 37 38 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 36 def persisted_name @abbr || @name end |
#predicate_accessor? ⇒ Boolean
117 118 119 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 117 def predicate_accessor? any_accessor? ["present", "predicate", "boolean"] end |
#read_accessor? ⇒ Boolean
109 110 111 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 109 def read_accessor? any_accessor? ["read"] end |
#reserved_name? ⇒ Boolean
105 106 107 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 105 def reserved_name? RESERVED_KEYS.include?(@name) end |
#set(value) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 77 def set(value) # Avoid tap here so we don't have to create a block binding. value = type ? type.to_mongo(value) : value.to_mongo if @typecast klass = typecast_class # Don't make this lookup on every call value.map { |v| klass.to_mongo(v) } else value end end |
#valid_ruby_name? ⇒ Boolean
101 102 103 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 101 def valid_ruby_name? !!@name.match(/\A[a-z_][a-z0-9_]*\z/i) end |
#write_accessor? ⇒ Boolean
113 114 115 |
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 113 def write_accessor? any_accessor? ["write"] end |