Module: Subvirtus::ClassMethods
- Defined in:
- lib/subvirtus.rb
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#set_attributes ⇒ Object
readonly
Returns the value of attribute set_attributes.
-
#value_attributes ⇒ Object
readonly
Returns the value of attribute value_attributes.
Instance Method Summary collapse
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
7 8 9 |
# File 'lib/subvirtus.rb', line 7 def attributes @attributes end |
#set_attributes ⇒ Object (readonly)
Returns the value of attribute set_attributes.
8 9 10 |
# File 'lib/subvirtus.rb', line 8 def set_attributes @set_attributes end |
#value_attributes ⇒ Object (readonly)
Returns the value of attribute value_attributes.
9 10 11 |
# File 'lib/subvirtus.rb', line 9 def value_attributes @value_attributes end |
Instance Method Details
#attribute(name, type = nil, options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/subvirtus.rb', line 11 def attribute( name, type = nil, = {} ) if @in_values @value_attributes ||= [] @value_attributes << name unless @value_attributes.include? name end @attributes ||= [] @attributes << name unless @attributes.include? name define_method( "#{ name }=") do |value| @set_attributes ||= [] @set_attributes << name instance_variable_set "@#{ name }", value end define_method( name ) do value = instance_variable_get "@#{ name }" coercer = [ :coercer ] default = [ :default ] if coercer coercer.call value elsif value.nil? and not default.nil? and default.is_a? Symbol and respond_to? default send default else if type.is_a? Array if value.nil? [] elsif value.is_a? Array value.map { |v| determine_value name, v, type.first, } else determine_value name, value, type.first, end else determine_value name, value, type, end end end private :"#{ name }" if [ :reader ] == :private self end |