Class: MongoMapper::Plugins::Keys::Key

Inherits:
Object
  • Object
show all
Defined in:
lib/mongo_mapper/plugins/keys/key.rb

Constant Summary

ID_STR =
'_id'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Key



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 10

def initialize(*args)
  options_from_args = args.extract_options!
  @name, @type = args.shift.to_s, args.shift
  self.options = (options_from_args || {}).symbolize_keys
  @dynamic     = !!options[:__dynamic]
  @embeddable  = type.respond_to?(:embeddable?) ? type.embeddable? : false
  @is_id       = @name == ID_STR
  @typecast    = @options[:typecast]
  @accessors   = Array(@options[:accessors]).compact.map &:to_s
  @has_default  = !!options.key?(:default)
  self.default = self.options[: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

#abbrObject

Returns the value of attribute abbr



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def abbr
  @abbr
end

#accessorsObject

Returns the value of attribute accessors



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def accessors
  @accessors
end

#defaultObject

Returns the value of attribute default



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def default
  @default
end

#ivarObject

Returns the value of attribute ivar



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def ivar
  @ivar
end

#nameObject

Returns the value of attribute name



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def name
  @name
end

#optionsObject

Returns the value of attribute options



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def options
  @options
end

#typeObject

Returns the value of attribute type



6
7
8
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 6

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 39

def ==(other)
  @name == other.name && @type == other.type && @abbr == other.abbr
end

#any_accessor?(arr_opt = []) ⇒ Boolean



108
109
110
111
112
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 108

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



51
52
53
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 51

def default?
  @has_default
end

#default_valueObject



81
82
83
84
85
86
87
88
89
90
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 81

def default_value
  return unless default?
  if default.instance_of? Proc
    type.to_mongo default.call
  else
    # Using Marshal is easiest way to get a copy of mutable objects
    # without getting an error on immutable objects
    type.to_mongo Marshal.load(Marshal.dump(default))
  end
end

#dynamic?Boolean



55
56
57
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 55

def dynamic?
  @dynamic
end

#embeddable?Boolean



43
44
45
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 43

def embeddable?
  @embeddable
end

#get(value) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 59

def get(value)
  # Special Case: Generate default _id on access
  value = default_value if @is_id and !value

  if @typecast
    klass = typecast_class  # Don't make this lookup on every call
    type.from_mongo(value).map { |v| klass.from_mongo(v) }
  else
    type.from_mongo(value)
  end
end

#number?Boolean



47
48
49
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 47

def number?
  type == Integer || type == Float
end

#persisted_nameObject



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

def persisted_name
  @abbr || @name
end

#predicate_accessor?Boolean



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

def predicate_accessor?
  any_accessor? ["present", "predicate", "boolean"]
end

#read_accessor?Boolean



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

def read_accessor?
  any_accessor? ["read"]
end

#set(value) ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'lib/mongo_mapper/plugins/keys/key.rb', line 71

def set(value)
  # Avoid tap here so we don't have to create a block binding.
  values = type.to_mongo(value)
  if @typecast
    values.map { |v| typecast_class.to_mongo(v) }
  else
    values
  end
end

#valid_ruby_name?Boolean



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

def valid_ruby_name?
  !!@name.match(/\A[a-z_][a-z0-9_]*\z/i)
end

#write_accessor?Boolean



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

def write_accessor?
  any_accessor? ["write"]
end