Class: Attrio::Attribute
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #default? ⇒ Boolean
- #default_value ⇒ Object
- #define_reader(klass) ⇒ Object
- #define_writer(klass) ⇒ Object
-
#initialize(name, type, options) ⇒ Attribute
constructor
A new instance of Attribute.
- #instance_variable_name ⇒ Object
- #reader_method_name ⇒ Object
- #reader_visibility ⇒ Object
- #reset! ⇒ Object
- #writer_method_name ⇒ Object
- #writer_visibility ⇒ Object
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, ) @name = name; @type = type; @options = Helpers.symbolize_hash_keys() end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/attrio/attribute.rb', line 5 def name @name end |
#object ⇒ Object (readonly)
Returns the value of attribute object.
5 6 7 |
# File 'lib/attrio/attribute.rb', line 5 def object @object end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
5 6 7 |
# File 'lib/attrio/attribute.rb', line 5 def @options end |
#type ⇒ Object (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
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_value ⇒ Object
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.[: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..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..merge({ :method_name => self.writer_method_name, :method_visibility => self.writer_visibility, :instance_variable_name => self.instance_variable_name }) ) self end |
#instance_variable_name ⇒ Object
27 28 29 |
# File 'lib/attrio/attribute.rb', line 27 def instance_variable_name @instance_variable_name ||= self.[:instance_variable_name] || "@#{self.name}" end |
#reader_method_name ⇒ Object
11 12 13 |
# File 'lib/attrio/attribute.rb', line 11 def reader_method_name @reader_method_name ||= self.(:reader) || self.name end |
#reader_visibility ⇒ Object
19 20 21 |
# File 'lib/attrio/attribute.rb', line 19 def reader_visibility @reader_visibility ||= self.(:reader) || :public end |
#reset! ⇒ Object
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_name ⇒ Object
15 16 17 |
# File 'lib/attrio/attribute.rb', line 15 def writer_method_name @writer_method_name ||= self.(:writer) || "#{self.name}=" end |
#writer_visibility ⇒ Object
23 24 25 |
# File 'lib/attrio/attribute.rb', line 23 def writer_visibility @writer_visibility ||= self.(:writer) || :public end |