Module: Subvirtus::ClassMethods

Defined in:
lib/subvirtus.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

#set_attributesObject (readonly)

Returns the value of attribute set_attributes.



8
9
10
# File 'lib/subvirtus.rb', line 8

def set_attributes
  @set_attributes
end

#value_attributesObject (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, options = {} )
  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 = options[ :coercer ]
    default = options[ :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, options }
        else
          determine_value name, value, type.first, options
        end
      else
        determine_value name, value, type, options
      end
    end
  end
  private :"#{ name }" if options[ :reader ] == :private
  self
end