Sha256: 6dff7a6687119327c757360412da94caca8f371502e0d33e56af5c82a7b51a7c

Contents?: true

Size: 1.44 KB

Versions: 6

Compression:

Stored size: 1.44 KB

Contents

module Rumx
  class Type
    attr_reader :name

    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

    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

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

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

    def to_s
      @name.to_s
    end

    @@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| ''})
    }

    # We've created all the instances we need
    private_class_method :new
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rumx-0.1.5 lib/rumx/type.rb
rumx-0.1.4 lib/rumx/type.rb
rumx-0.1.3 lib/rumx/type.rb
rumx-0.1.2 lib/rumx/type.rb
rumx-0.1.1 lib/rumx/type.rb
rumx-0.1.0 lib/rumx/type.rb