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.



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

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

Instance Method Details

#munge(&block) ⇒ Object



130
131
132
# File 'lib/pleaserun/configurable.rb', line 130

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

#validate(&block) ⇒ Object



126
127
128
# File 'lib/pleaserun/configurable.rb', line 126

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