Class: Extant::AttributeDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/extant/attribute_definition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, name, opts = {}) ⇒ AttributeDefinition

Returns a new instance of AttributeDefinition.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/extant/attribute_definition.rb', line 6

def initialize(model, name, opts={})
  self.name = name
  self.type = opts[:type]
  self.has_default = opts.key?(:default)
  self.default_value = opts[:default]
  self.allow_nil = opts.fetch(:allow_nil, true)

  if type
    self.coercer_class = Extant::Coercers.find(type)
  end

  model.instance_eval do
    define_method name do
      extant_attributes[name].value
    end

    define_method "#{name}=" do |val|
      extant_attributes[name].value = val
    end
  end
end

Instance Attribute Details

#allow_nilObject

Returns the value of attribute allow_nil.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def allow_nil
  @allow_nil
end

#coercer_classObject

Returns the value of attribute coercer_class.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def coercer_class
  @coercer_class
end

#default_valueObject

Returns the value of attribute default_value.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def default_value
  @default_value
end

#has_defaultObject

Returns the value of attribute has_default.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def has_default
  @has_default
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def name
  @name
end

#typeObject

Returns the value of attribute type.



3
4
5
# File 'lib/extant/attribute_definition.rb', line 3

def type
  @type
end