Class: PleaseRun::Configurable::FacetDSL

Inherits:
Object
  • Object
show all
Defined in:
lib/pleaserun/configurable.rb

Overview

A DSL for describing a facet.

For example:

Facet.new(:temperature, "The temperature value") do
  validate do |v|
    fail "Temperature must be a number" unless v.is_a?(Numeric)
  end
  munge do |v|
    Float(v)
  end
end

Both validation and munge blocks are optional.

The ‘validate’ block is expcted to fail if the value given to the facet is not valid.

The ‘munge’ block is intended to help you coerce a value. For example, if you take “1234” from the user input (for example, as a command line flag value), you could use ‘munge’ to convert it to a number, as above.

Munge is invoked before validation. Munge can fail if an invalid value is given.

Instance Method Summary collapse

Constructor Details

#initialize(facet, &block) ⇒ FacetDSL

Returns a new instance of FacetDSL.



112
113
114
115
# File 'lib/pleaserun/configurable.rb', line 112

def initialize(facet, &block)
  @facet = facet
  instance_eval(&block)
end

Instance Method Details

#munge(&block) ⇒ Object



121
122
123
# File 'lib/pleaserun/configurable.rb', line 121

def munge(&block)
  @facet.munger = block
end

#validate(&block) ⇒ Object



117
118
119
# File 'lib/pleaserun/configurable.rb', line 117

def validate(&block)
  @facet.validator = block
end