Module: ValueSemantics

Defined in:
lib/value_semantics.rb,
lib/value_semantics/version.rb

Defined Under Namespace

Modules: Anything, Boolean, Semantics Classes: ArrayOf, Attribute, DSL, Either

Constant Summary collapse

VERSION =
"2.0.1"

Class Method Summary collapse

Class Method Details

.for_attributes(&block) ⇒ Object



2
3
4
5
# File 'lib/value_semantics.rb', line 2

def self.for_attributes(&block)
  attributes = DSL.run(&block)
  generate_module(attributes.freeze)
end

.generate_module(attributes) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/value_semantics.rb', line 7

def self.generate_module(attributes)
  Module.new do
    # include all the instance methods
    include(Semantics)

    # define the attr readers
    attributes.each do |attr|
      module_eval("def #{attr.name}; #{attr.instance_variable}; end")
    end

    # define BaseClass.attributes class method
    const_set(:ATTRIBUTES__, attributes)
    def self.included(base)
      base.const_set(:ValueSemantics_Generated, self)
      class << base
        def attributes
          self::ATTRIBUTES__
        end
      end
    end
  end
end