Class: Constructable::Attribute

Inherits:
Object
  • Object
show all
Defined in:
lib/constructable/attribute.rb

Constant Summary collapse

ATTRIBUTES =
[:group, :writable, :readable, :accessible, :required, :default, :converter]

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ Attribute

Returns a new instance of Attribute.



6
7
8
9
10
11
# File 'lib/constructable/attribute.rb', line 6

def initialize(name, options = {})
  @name = name
  ATTRIBUTES.each do |attribute|
    self.send(:"#{attribute}=", options[attribute])
  end
end

Instance Method Details

#accessible=(boolean) ⇒ Object



13
14
15
16
17
18
# File 'lib/constructable/attribute.rb', line 13

def accessible=(boolean)
  if boolean
    self.readable = true
    self.writable = true
  end
end

#attr_writer_symbolObject



24
25
26
# File 'lib/constructable/attribute.rb', line 24

def attr_writer_symbol
  (self.name.to_s + '=').to_sym
end

#ivar_symbolObject



20
21
22
# File 'lib/constructable/attribute.rb', line 20

def ivar_symbol
  ('@' + self.name.to_s).to_sym
end

#process(value) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/constructable/attribute.rb', line 28

def process(value)
  unless value.nil?
    self.converter ? converter.(value) : value
  else
    raise AttributeError, ":#{self.name} is a required attribute" if self.required
    self.default.call if self.default
  end
end