Sha256: ab16f9459ad8755de4e46c6b5e7ac1be67573d4ef3ef2eaed28913db63707561

Contents?: true

Size: 969 Bytes

Versions: 8

Compression:

Stored size: 969 Bytes

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, string_to_value_proc, value_to_string_proc=lambda{|v| v.to_s})
      @name = name
      @string_to_value_proc = string_to_value_proc
      @value_to_string_proc = value_to_string_proc
    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, lambda {|s| s.to_i}),
        :float   => new(:float,   lambda {|s| s.to_f}),
        :string  => new(:string,  lambda {|s| s.to_s}),
        :boolean => new(:boolean, lambda {|s| s.to_s == 'true'}),
        :void    => new(:void,    lambda {|s| nil}, lambda {|v| ''})
    }

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

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
rumx-0.0.8 lib/rumx/type.rb
rumx-0.0.7 lib/rumx/type.rb
rumx-0.0.6 lib/rumx/type.rb
rumx-0.0.5 lib/rumx/type.rb
rumx-0.0.4 lib/rumx/type.rb
rumx-0.0.3 lib/rumx/type.rb
rumx-0.0.2 lib/rumx/type.rb
rumx-0.0.1 lib/rumx/type.rb