Class: Artisanal::Model::Attribute

Inherits:
Module
  • Object
show all
Defined in:
lib/artisanal/model/attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, coercer = nil, **options) ⇒ Attribute

Returns a new instance of Attribute.

Raises:

  • (ArgumentError)


5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/artisanal/model/attribute.rb', line 5

def initialize(name, coercer=nil, **options)
  @name, @options = name, options
  @type = coercer || options[:type]

  # Convert :from option to :as
  if options.has_key? :from
    @name, options[:as] = options[:from], name
  end

  # Add default values for certain types
  unless options.has_key?(:default)
    options.merge!(default: type_default)
  end

  raise ArgumentError.new("type missing for attribute #{name}") if type.nil?
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/artisanal/model/attribute.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/artisanal/model/attribute.rb', line 3

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/artisanal/model/attribute.rb', line 3

def type
  @type
end

Instance Method Details

#included(base) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/artisanal/model/attribute.rb', line 22

def included(base)
  # Create dry-initializer option
  base.option(name, **options.merge(type: type_builder))

  # Create writer method
  define_writer(base, name) if options[:writer]
end