Method: Usable.Struct

Defined in:
lib/usable/struct.rb

.Struct(attributes = {}) ⇒ Object



2
3
4
5
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
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/usable/struct.rb', line 2

def self.Struct(attributes = {})
  Class.new do
    extend Usable
    self.usables = Usable::Config.new(attributes)
    define_usable_accessors
    attributes.keys.map(&:to_sym).each do |key|
      define_method(key) { @attrs[key] }
      define_method("#{key}=") { |new_val| @attrs[key] = new_val }
    end

    attr_accessor :attrs

    def initialize(attrs = {})
      @attrs = usables.merge(attrs)
    end

    def [](key)
      @attrs[key]
    end

    def []=(key, val)
      @attrs[key] = val
    end

    def each(&block)
      @attrs.each(&block)
    end

    def to_h
      @attrs.dup
    end

    alias to_hash to_h

    def merge(other)
      to_h.merge!(other)
    end

    alias + merge
  end
end