Module: ValueSemantics

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

Defined Under Namespace

Modules: AnythingValidator, Semantics Classes: Attribute, DSL

Constant Summary collapse

IdentityCoercer =
->(value) { value }
VERSION =
"0.1.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)
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.tap do |m|
    # include all the instance methods
    m.include(Semantics)

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

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