class PleaseRun::Configurable::Facet

A generalized facet/property/container for a single value.

Supports naming and text descriptions of this thing.

Also supports value validation and munging on assignment to help you more easily accept user input from a variety of sources and keep the validation and value munging concerns near the value itself.

Attributes

description[R]
munger[W]
name[R]
options[R]
validator[W]

Public Class Methods

new(name, description, options = {}, &facet_dsl) click to toggle source
# File lib/pleaserun/configurable.rb, line 148
def initialize(name, description, options = {}, &facet_dsl)
  insist { name }.is_a?(Symbol)
  insist { description }.is_a?(String)
  insist { options }.is_a?(Hash)

  @name = name
  @description = description
  @options = options

  FacetDSL.new(self, &facet_dsl) if block_given?

  validate(@options[:default]) if @options[:default]
end

Public Instance Methods

set?() click to toggle source
# File lib/pleaserun/configurable.rb, line 180
def set?
  return !@value.nil?
end
validate(v) click to toggle source
# File lib/pleaserun/configurable.rb, line 168
def validate(v)
  return @validator.call(v) if @validator
rescue => e
  raise ValidationError, "Invalid value '#{v.inspect}' for attribute '#{name}' (#{e})"
end
value() click to toggle source
# File lib/pleaserun/configurable.rb, line 174
def value
  return @value if @value
  return @options[:default] if @options.include?(:default)
  return nil
end
value=(v) click to toggle source
# File lib/pleaserun/configurable.rb, line 162
def value=(v)
  v = @munger.call(v) if @munger
  validate(v)
  @value = v
end