Class: PleaseRun::Configurable::Facet

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

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, description, options = {}, &facet_dsl) ⇒ Facet

Returns a new instance of Facet.



148
149
150
151
152
153
154
155
156
157
158
159
160
# 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

Instance Attribute Details

#descriptionObject (readonly)

Returns the value of attribute description.



144
145
146
# File 'lib/pleaserun/configurable.rb', line 144

def description
  @description
end

#munger=(value) ⇒ Object (writeonly)

Sets the attribute munger

Parameters:

  • value

    the value to set the attribute munger to.



146
147
148
# File 'lib/pleaserun/configurable.rb', line 146

def munger=(value)
  @munger = value
end

#nameObject (readonly)

Returns the value of attribute name.



143
144
145
# File 'lib/pleaserun/configurable.rb', line 143

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



145
146
147
# File 'lib/pleaserun/configurable.rb', line 145

def options
  @options
end

#validator=(value) ⇒ Object (writeonly)

Sets the attribute validator

Parameters:

  • value

    the value to set the attribute validator to.



146
147
148
# File 'lib/pleaserun/configurable.rb', line 146

def validator=(value)
  @validator = value
end

Instance Method Details

#set?Boolean

def value

Returns:

  • (Boolean)


180
181
182
# File 'lib/pleaserun/configurable.rb', line 180

def set?
  return !@value.nil?
end

#validate(v) ⇒ Object

def value=



168
169
170
171
172
# 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

#valueObject

def validate



174
175
176
177
178
# 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) ⇒ Object

def initialize



162
163
164
165
166
# File 'lib/pleaserun/configurable.rb', line 162

def value=(v)
  v = @munger.call(v) if @munger
  validate(v)
  @value = v
end