class Sire::Struct

Attributes

attributes[R]

Public Class Methods

[](hash = {}) click to toggle source
# File lib/sire/struct.rb, line 16
def [](hash = {})
  new(hash)
end
define(*attributes) click to toggle source
# File lib/sire/struct.rb, line 8
def define(*attributes)
  Class.new(self) do
    attr_accessor(*attributes)
    private(*attributes.map { |a| "#{a}=" })
    @attributes = [*attributes].freeze
  end
end
new(hash = {}) click to toggle source
# File lib/sire/struct.rb, line 21
def initialize(hash = {})
  hash.each { |k, v| send("#{k}=", v) }
  freeze
end

Public Instance Methods

==(other) click to toggle source
# File lib/sire/struct.rb, line 40
def ==(other)
  self.class == other.class && to_h == other.to_h
end
attributes() click to toggle source
# File lib/sire/struct.rb, line 32
def attributes
  self.class.attributes
end
each() { |a, send(a)| ... } click to toggle source
# File lib/sire/struct.rb, line 36
def each
  attributes.each { |a| yield a, send(a) }
end
inspect() click to toggle source
# File lib/sire/struct.rb, line 48
def inspect
  to_s
end
merge(other) click to toggle source
# File lib/sire/struct.rb, line 26
def merge(other)
  other_hash = other.respond_to?(:to_h) ? other.to_h : other
  self.class.new(to_h.merge(other_hash))
end
Also aliased as: update
to_h() click to toggle source
# File lib/sire/struct.rb, line 52
def to_h
  Hash[to_a]
end
to_s() click to toggle source
# File lib/sire/struct.rb, line 44
def to_s
  "#{self.class}[#{map { |k, v| "#{k}: #{v.inspect}" }.join(', ') }]"
end
update(other)
Alias for: merge