Module: ValueSemantics

Defined in:
lib/value_semantics.rb,
lib/value_semantics/dsl.rb,
lib/value_semantics/bool.rb,
lib/value_semantics/either.rb,
lib/value_semantics/recipe.rb,
lib/value_semantics/struct.rb,
lib/value_semantics/hash_of.rb,
lib/value_semantics/version.rb,
lib/value_semantics/anything.rb,
lib/value_semantics/array_of.rb,
lib/value_semantics/range_of.rb,
lib/value_semantics/attribute.rb,
lib/value_semantics/hash_coercer.rb,
lib/value_semantics/array_coercer.rb,
lib/value_semantics/class_methods.rb,
lib/value_semantics/instance_methods.rb,
lib/value_semantics/value_object_coercer.rb

Defined Under Namespace

Modules: Anything, Bool, ClassMethods, InstanceMethods Classes: ArrayCoercer, ArrayOf, Attribute, DSL, Either, Error, HashCoercer, HashOf, InvalidValue, MissingAttributes, NoDefaultValue, RangeOf, Recipe, Struct, UnrecognizedAttributes, ValueObjectCoercer

Constant Summary collapse

NOT_SPECIFIED =
Deprecated.
Attribute::NOT_SPECIFIED
VERSION =
"3.6.1"

Class Method Summary collapse

Class Method Details

.bake_module(recipe) ⇒ Module

Creates a module from a Recipe

Parameters:

Returns:

  • (Module)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/value_semantics.rb', line 54

def self.bake_module(recipe)
  Module.new do
    const_set(:VALUE_SEMANTICS_RECIPE__, recipe)
    include(InstanceMethods)

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

    def self.included(base)
      base.const_set(:ValueSemantics_Attributes, self)
      base.extend(ClassMethods)
    end
  end
end

.for_attributes { ... } ⇒ Module

Creates a module via the DSL

Yields:

  • The block containing the DSL

Returns:

  • (Module)

See Also:



43
44
45
46
# File 'lib/value_semantics.rb', line 43

def self.for_attributes(&block)
  recipe = DSL.run(&block)
  bake_module(recipe)
end

.monkey_patch!Object

Makes the .value_semantics convenience method available to all classes

.value_semantics is a shortcut for for_attributes. Instead of:

class Person
  include ValueSemantics.for_attributes {
    name String
  }
end

You can just write:

class Person
  value_semantics do
    name String
  end
end

Alternatively, you can require ‘value_semantics/monkey_patched’, which will call this method automatically.



93
94
95
96
97
98
99
100
101
# File 'lib/value_semantics.rb', line 93

def self.monkey_patch!
  Class.class_eval do
    # @!visibility private
    def value_semantics(&block)
      include ValueSemantics.for_attributes(&block)
    end
    private :value_semantics
  end
end