Class: Rumx::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/rumx/type.rb

Constant Summary collapse

@@allowed_types =
{
    :integer => new(:integer, Attribute,     lambda {|s| s.to_i}),
    :float   => new(:float,   Attribute,     lambda {|s| s.to_f}),
    :string  => new(:string,  Attribute,     lambda {|s| s.to_s}),
    :symbol  => new(:symbol,  Attribute,     lambda {|s| s.to_sym}),
    :boolean => new(:boolean, Attribute,     lambda {|s| s.to_s == 'true'}),
    :list    => new(:list,    ListAttribute, nil),
    :hash    => new(:hash,    HashAttribute, nil),
    :void    => new(:void,    nil,           lambda {|s| nil}, lambda {|v| ''})
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, attribute_class, string_to_value_proc, value_to_string_proc = lambda{|v| v.to_s}) ⇒ Type

Returns a new instance of Type.



31
32
33
34
35
36
# File 'lib/rumx/type.rb', line 31

def initialize(name, attribute_class, string_to_value_proc, value_to_string_proc=lambda{|v| v.to_s})
  @name                 = name
  @attribute_class      = attribute_class
  @string_to_value_proc = string_to_value_proc
  @value_to_string_proc = value_to_string_proc
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/rumx/type.rb', line 3

def name
  @name
end

Class Method Details

.find(type_name) ⇒ Object



5
6
7
8
9
# File 'lib/rumx/type.rb', line 5

def self.find(type_name)
  type = @@allowed_types[type_name.to_sym]
  raise "No such type=#{type_name} in Rumx::Type" unless type
  type
end

.find_by_value(val) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rumx/type.rb', line 11

def self.find_by_value(val)
  if val.kind_of?(Integer)
    @@llowed_types[:integer]
  elsif val.kind_of?(Float)
    @@llowed_types[:float]
  elsif val.kind_of?(String)
    @@llowed_types[:string]
  elsif val.kind_of?(Symbol)
    @@llowed_types[:symbol]
  elsif val.kind_of?(Boolean)
    @@llowed_types[:boolean]
  elsif val.kind_of?(Array)
    @@llowed_types[:list]
  elsif val.kind_of?(Hash)
    @@llowed_types[:hash]
  else
    nil
  end
end

Instance Method Details

#create_attribute(name, description, allow_read, allow_write, options) ⇒ Object



38
39
40
# File 'lib/rumx/type.rb', line 38

def create_attribute(name, description, allow_read, allow_write, options)
  @attribute_class.new(name, self, description, allow_read, allow_write, options)
end

#string_to_value(string) ⇒ Object



42
43
44
# File 'lib/rumx/type.rb', line 42

def string_to_value(string)
  @string_to_value_proc.call(string)
end

#to_sObject



50
51
52
# File 'lib/rumx/type.rb', line 50

def to_s
  @name.to_s
end

#value_to_string(val) ⇒ Object



46
47
48
# File 'lib/rumx/type.rb', line 46

def value_to_string(val)
  @value_to_string_proc.call(val)
end