module ValueSemantics
Constants
- NOT_SPECIFIED
@deprecated Use {Attribute::NOT_SPECIFIED} instead
- VERSION
Public Class Methods
bake_module(recipe)
click to toggle source
Creates a module from a {Recipe}
@param recipe [Recipe] @return [Module]
# 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(&block)
click to toggle source
Creates a module via the DSL
@yield The block containing the DSL
@return [Module]
@see DSL
@see InstanceMethods
# File lib/value_semantics.rb, line 43 def self.for_attributes(&block) recipe = DSL.run(&block) bake_module(recipe) end
included(base)
click to toggle source
# File lib/value_semantics.rb, line 64 def self.included(base) base.const_set(:ValueSemantics_Attributes, self) base.extend(ClassMethods) end
monkey_patch!()
click to toggle source
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.
# 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
Public Instance Methods
value_semantics(&block)
click to toggle source
@!visibility private
# File lib/value_semantics.rb, line 96 def value_semantics(&block) include ValueSemantics.for_attributes(&block) end