Class: Wholeable::Builder

Inherits:
Module
  • Object
show all
Defined in:
lib/wholeable/builder.rb

Overview

Provides core equality behavior.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*keys, kind: :immutable) ⇒ Builder

Returns a new instance of Builder.



12
13
14
15
16
17
18
# File 'lib/wholeable/builder.rb', line 12

def initialize *keys, kind: :immutable
  super()
  @keys = keys.uniq
  @kind = kind
  @members = []
  setup
end

Class Method Details

.add_aliases(descendant) ⇒ Object



6
7
8
9
10
# File 'lib/wholeable/builder.rb', line 6

def self.add_aliases descendant
  descendant.alias_method :deconstruct, :to_a
  descendant.alias_method :deconstruct_keys, :to_h
  descendant.alias_method :to_s, :inspect
end

Instance Method Details

#included(descendant) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/wholeable/builder.rb', line 20

def included descendant
  super
  coalesce_members descendant

  descendant.class_eval <<-METHODS, __FILE__, __LINE__ + 1
    def self.[](...) = new(...)

    def self.new(...) = #{mutable?} ? super.dup : super.freeze

    def self.members = #{members}

    #{mutable? ? :attr_accessor : :attr_reader} #{keys.map(&:inspect).join ", "}
  METHODS

  self.class.add_aliases descendant
end