class ValueSemantics::DSL

Builds a {Recipe} via DSL methods

DSL blocks are instance_evald against an object of this class.

@see Recipe @see ValueSemantics.for_attributes

Constants

IDENTITY_COERCER

Attributes

__attributes[R]

Public Class Methods

new() click to toggle source
# File lib/value_semantics/dsl.rb, line 30
def initialize
  @__attributes = []
end
run(&block) click to toggle source

Builds a {Recipe} from a DSL block

@yield to the block containing the DSL @return [Recipe]

# File lib/value_semantics/dsl.rb, line 22
def self.run(&block)
  dsl = new
  dsl.instance_eval(&block)
  Recipe.new(attributes: dsl.__attributes.freeze)
end

Public Instance Methods

Anything() click to toggle source
# File lib/value_semantics/dsl.rb, line 42
def Anything
  Anything
end
ArrayCoercer(element_coercer) click to toggle source
# File lib/value_semantics/dsl.rb, line 65
def ArrayCoercer(element_coercer)
  ArrayCoercer.new(element_coercer)
end
ArrayOf(element_validator) click to toggle source
# File lib/value_semantics/dsl.rb, line 46
def ArrayOf(element_validator)
  ArrayOf.new(element_validator)
end
Bool() click to toggle source
# File lib/value_semantics/dsl.rb, line 34
def Bool
  Bool
end
Either(*subvalidators) click to toggle source
# File lib/value_semantics/dsl.rb, line 38
def Either(*subvalidators)
  Either.new(subvalidators)
end
HashCoercer(keys: IDENTITY_COERCER, values: IDENTITY_COERCER) click to toggle source
# File lib/value_semantics/dsl.rb, line 70
def HashCoercer(keys: IDENTITY_COERCER, values: IDENTITY_COERCER)
  HashCoercer.new(key_coercer: keys, value_coercer: values)
end
HashOf(key_validator_to_value_validator) click to toggle source
# File lib/value_semantics/dsl.rb, line 50
def HashOf(key_validator_to_value_validator)
  unless key_validator_to_value_validator.size.equal?(1)
    raise ArgumentError, "HashOf() takes a hash with one key and one value"
  end

  HashOf.new(
    key_validator_to_value_validator.keys.first,
    key_validator_to_value_validator.values.first,
  )
end
RangeOf(subvalidator) click to toggle source
# File lib/value_semantics/dsl.rb, line 61
def RangeOf(subvalidator)
  RangeOf.new(subvalidator)
end
def_attr(*args, **kwargs) click to toggle source

Defines one attribute.

This is the method that gets called under the hood, when defining attributes the typical #method_missing way.

You can use this method directly if your attribute name results in invalid Ruby syntax. For example, if you want an attribute named then, you can do:

include ValueSemantics.for_attributes {
  # Does not work:
  then String, default: "whatever"
  #=> SyntaxError: syntax error, unexpected `then'

  # Works:
  def_attr :then, String, default: "whatever"
}
# File lib/value_semantics/dsl.rb, line 94
def def_attr(*args, **kwargs)
  __attributes << Attribute.define(*args, **kwargs)
  nil
end
method_missing(name, *args, **kwargs) click to toggle source
Calls superclass method
# File lib/value_semantics/dsl.rb, line 99
def method_missing(name, *args, **kwargs)
  if respond_to_missing?(name)
    def_attr(name, *args, **kwargs)
  else
    super
  end
end
respond_to_missing?(method_name, _include_private=nil) click to toggle source
# File lib/value_semantics/dsl.rb, line 107
def respond_to_missing?(method_name, _include_private=nil)
  first_letter = method_name.to_s.each_char.first
  first_letter.eql?(first_letter.downcase)
end