Module: XRVG::Attributable::ClassMethods

Defined in:
lib/attributable.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#add_attribute(attribute) ⇒ Object



88
89
90
91
# File 'lib/attributable.rb', line 88

def add_attribute( attribute )
  init_attributes
  @attributes[ attribute.symbol ] = attribute
end

#attribute(symbol, default_value = nil, type = nil) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/attributable.rb', line 114

def attribute( symbol, default_value=nil, type=nil )
  if (not type and default_value)
	type = [default_value.class]
  elsif type
	if not type.is_a? Symbol
	  if not type.is_a? Array
 type = [type]
	  end
	  if default_value
 checkvaluetype( default_value, type )
	  end
    else
	  if not Attribute.typekey?( type )
 raise( "Custom type #{type} has not been defined in Attribute class map: use Attribute.addtype to do it" )
	  end
	  if default_value
 default_value = Attribute.typebuilder( type ).call( default_value )
	  end
	end
  end

  self.add_attribute( Attribute.new( symbol, default_value, type ) )
  attr_accessor symbol
end

#attributesObject



93
94
95
96
# File 'lib/attributable.rb', line 93

def attributes()
  init_attributes
  return @attributes
end

#checkvaluetype(value, type) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/attributable.rb', line 98

def checkvaluetype( value, type )
  typeOK = nil
  types = type
  types.each do |type|
	if not type.is_a? Symbol
	  if value.is_a? type
 typeOK = true
 break
	  end
	end
  end
  if not typeOK
	raise( "Attributable::checkvaluetype for class #{self} : default_value #{value.inspect} is not of type #{types.inspect}" )
  end
end

#init_attributesObject



80
81
82
83
84
85
86
# File 'lib/attributable.rb', line 80

def init_attributes
  if not @attributes
	@attributes = {}
	newattributes = (self.superclass.ancestors.include? Attributable) ? self.superclass.attributes : {}
	@attributes = @attributes.merge( newattributes )
  end
end