class Strukt

Constants

VERSION

Public Class Methods

coerce(coercable) click to toggle source
# File lib/strukt.rb, line 14
def self.coerce(coercable)
  case coercable
  when self then coercable
  when Hash then new(coercable)
  else raise TypeError, "Unable to coerce #{coercable.class} into #{self}"
  end
end
new(*members) click to toggle source
# File lib/strukt.rb, line 6
def self.new(*members)
  Class.new do
    attr_accessor *members

    include Enumerable
    
    const_set(:MEMBERS, members.dup.freeze)

    def self.coerce(coercable)
      case coercable
      when self then coercable
      when Hash then new(coercable)
      else raise TypeError, "Unable to coerce #{coercable.class} into #{self}"
      end
    end
    
    def initialize(params = {})
      params.each { |k, v| send("#{k}=", v) }
    end
new(params = {}) click to toggle source
# File lib/strukt.rb, line 22
def initialize(params = {})
  params.each { |k, v| send("#{k}=", v) }
end

Public Instance Methods

==(other) click to toggle source
# File lib/strukt.rb, line 26
def ==(other)
  other.is_a?(self.class) && to_hash == other.to_hash
end
each(*args, &block) click to toggle source
# File lib/strukt.rb, line 42
def each(*args, &block)
  to_hash.each(*args, &block)
end
eql?(other) click to toggle source
# File lib/strukt.rb, line 30
def eql?(other)
  self == other
end
hash() click to toggle source
# File lib/strukt.rb, line 34
def hash
  to_hash.hash
end
to_hash() click to toggle source
# File lib/strukt.rb, line 38
def to_hash
  {}.tap { |h| self.class::MEMBERS.each { |m| h[m] = send(m) } }
end