class StrictStruct::AttributeModule

Public Class Methods

new(*attributes) click to toggle source
Calls superclass method
# File lib/strict_struct.rb, line 23
def initialize *attributes
  super() do
    define_method :initialize do |hash={}|
      Helper.validate_arguments(hash.keys, attributes)
      @_strict_struct_hash = Hash[hash.to_a].freeze
    end

    attributes.each do |attribute|
      define_method(attribute) do
        @_strict_struct_hash[attribute]
      end
    end

    def to_h
      to_hash
    end

    def to_hash
      Hash[@_strict_struct_hash.to_a]
    end

    define_method :== do |other|
      return false unless self.class == other.class

      attributes.all? {|name| self.send(name) == other.send(name)}
    end

    define_method :hash do
      @_strict_struct_hash.hash
    end
  end
end

Public Instance Methods

to_h() click to toggle source
# File lib/strict_struct.rb, line 36
def to_h
  to_hash
end
to_hash() click to toggle source
# File lib/strict_struct.rb, line 40
def to_hash
  Hash[@_strict_struct_hash.to_a]
end