Class: MongoModel::Properties::Property

Inherits:
Object
  • Object
show all
Defined in:
lib/mongomodel/concerns/properties.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options = {}) ⇒ Property

Returns a new instance of Property.



48
49
50
# File 'lib/mongomodel/concerns/properties.rb', line 48

def initialize(name, type, options={})
  @name, @type, @options = name.to_sym, type, options
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



46
47
48
# File 'lib/mongomodel/concerns/properties.rb', line 46

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



46
47
48
# File 'lib/mongomodel/concerns/properties.rb', line 46

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



46
47
48
# File 'lib/mongomodel/concerns/properties.rb', line 46

def type
  @type
end

Instance Method Details

#==(other) ⇒ Object



73
74
75
# File 'lib/mongomodel/concerns/properties.rb', line 73

def ==(other)
  other.is_a?(self.class) && name == other.name && type == other.type && options == other.options
end

#asObject



52
53
54
# File 'lib/mongomodel/concerns/properties.rb', line 52

def as
  options[:as] || name.to_s
end

#default(instance) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mongomodel/concerns/properties.rb', line 56

def default(instance)
  if options.key?(:default)
    default = options[:default]

    if default.is_a?(Proc)
      case default.arity
      when 0 then instance.instance_exec(&default)
      else        instance.instance_exec(instance, &default)
      end
    else
      default.duplicable? ? default.dup : default
    end
  elsif type.respond_to?(:mongomodel_default)
    type.mongomodel_default(instance)
  end
end

#embeddable?Boolean

Returns:



77
78
79
# File 'lib/mongomodel/concerns/properties.rb', line 77

def embeddable?
  type.ancestors.include?(EmbeddedDocument)
end

#internal?Boolean

Returns:



81
82
83
# File 'lib/mongomodel/concerns/properties.rb', line 81

def internal?
  as =~ /^_/ || options[:internal]
end

#validate?Boolean

Returns:



85
86
87
# File 'lib/mongomodel/concerns/properties.rb', line 85

def validate?
  options[:validate] != false
end