Class: Strukt

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/strukt.rb

Constant Summary collapse

VERSION =
"0.0.2"

Class Method Summary collapse

Class Method Details

.new(*members) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 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 initialize(params = {})
      params.each { |k, v| send("#{k}=", v) }
    end

    def ==(other)
      other.is_a?(self.class) && to_hash == other.to_hash
    end

    def to_hash
      {}.tap { |h| self.class::MEMBERS.each { |m| h[m] = send(m) } }
    end

    def each(*args, &block)
      to_hash.each(*args, &block)
    end
  end
end