class UnionValue

Public Class Methods

json_create(o) click to toggle source
# File lib/unionvalue.rb, line 44
def self.json_create(o)
  self.send(o['data']['type'], o['data']['data'])
end
new(*values, &block) click to toggle source
# File lib/unionvalue.rb, line 4
def self.new(*values, &block)

  Class.new do
    attr_accessor(:type, :data, *values)

    values.each do |value|
        define_singleton_method(value) do |*val|
            a = self.new
            a.data = val.first if val
            a.type = value
            a.freeze
            a
        end

        define_method(:"is_#{value}?") do
            type == value
        end
    end

    const_set :VALUES, values

    def ==(other)
      eql?(other)
    end

    def eql?(other)
      self.class == other.class && self.type == other.type && self.data == other.data
    end

    def inspect
      "#<#{self.class.name} #{self.type}:#{self.data}>"
    end

    def to_json(*a)
      {
        "json_class"   => self.class.name,
        "data"         => {"type" => type, "data" => data }
      }.to_json(*a)
    end

    def self.json_create(o)
      self.send(o['data']['type'], o['data']['data'])
    end

    class_eval &block if block
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/unionvalue.rb, line 25
def ==(other)
  eql?(other)
end
eql?(other) click to toggle source
# File lib/unionvalue.rb, line 29
def eql?(other)
  self.class == other.class && self.type == other.type && self.data == other.data
end
inspect() click to toggle source
# File lib/unionvalue.rb, line 33
def inspect
  "#<#{self.class.name} #{self.type}:#{self.data}>"
end
to_json(*a) click to toggle source
# File lib/unionvalue.rb, line 37
def to_json(*a)
  {
    "json_class"   => self.class.name,
    "data"         => {"type" => type, "data" => data }
  }.to_json(*a)
end