Class: Attrio::Attribute

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, options) ⇒ Attribute

Returns a new instance of Attribute.



7
8
9
# File 'lib/attrio/attribute.rb', line 7

def initialize(name, type, options)
  @name = name; @type = type; @options = Helpers.symbolize_hash_keys(options)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/attrio/attribute.rb', line 5

def name
  @name
end

#objectObject (readonly)

Returns the value of attribute object.



5
6
7
# File 'lib/attrio/attribute.rb', line 5

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



5
6
7
# File 'lib/attrio/attribute.rb', line 5

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



5
6
7
# File 'lib/attrio/attribute.rb', line 5

def type
  @type
end

Instance Method Details

#default?Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)


45
46
47
48
49
50
# File 'lib/attrio/attribute.rb', line 45

def default?
  raise ArgumentError if self.object.nil?

  value = self.default_value.is_a?(Attrio::DefaultValue::Base) ? self.default_value.call(self.object) : self.default_value
  self.object.send(self.reader_method_name) == value
end

#default_valueObject



31
32
33
34
35
36
# File 'lib/attrio/attribute.rb', line 31

def default_value
  if !defined?(@default_value)
    @default_value = Attrio::DefaultValue.new(self.name, self.options[:default])
  end
  @default_value
end

#define_reader(klass) ⇒ Object



63
64
65
66
67
68
69
70
71
72
# File 'lib/attrio/attribute.rb', line 63

def define_reader(klass)
  Attrio::Builders::ReaderBuilder.define(klass, self.type,
    self.options.merge({
      :method_name => self.reader_method_name,
      :method_visibility => self.reader_visibility,
      :instance_variable_name => self.instance_variable_name
    })
  )
  self
end

#define_writer(klass) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/attrio/attribute.rb', line 52

def define_writer(klass)
  Attrio::Builders::WriterBuilder.define(klass, self.type,
    self.options.merge({
      :method_name => self.writer_method_name,
      :method_visibility => self.writer_visibility,
      :instance_variable_name => self.instance_variable_name
    })
  )
  self
end

#instance_variable_nameObject



27
28
29
# File 'lib/attrio/attribute.rb', line 27

def instance_variable_name
  @instance_variable_name ||= self.options[:instance_variable_name] || "@#{self.name}"
end

#reader_method_nameObject



11
12
13
# File 'lib/attrio/attribute.rb', line 11

def reader_method_name
  @reader_method_name ||= self.accessor_name_from_options(:reader) || self.name
end

#reader_visibilityObject



19
20
21
# File 'lib/attrio/attribute.rb', line 19

def reader_visibility
  @reader_visibility ||= self.accessor_visibility_from_options(:reader) || :public
end

#reset!Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/attrio/attribute.rb', line 38

def reset!
  raise ArgumentError if self.object.nil?

  value = self.default_value.is_a?(Attrio::DefaultValue::Base) ? self.default_value.call(self.object) : self.default_value
  self.object.send(self.writer_method_name, value)
end

#writer_method_nameObject



15
16
17
# File 'lib/attrio/attribute.rb', line 15

def writer_method_name
  @writer_method_name ||= self.accessor_name_from_options(:writer) || "#{self.name}="
end

#writer_visibilityObject



23
24
25
# File 'lib/attrio/attribute.rb', line 23

def writer_visibility
  @writer_visibility ||= self.accessor_visibility_from_options(:writer) || :public
end