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.



138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/pleaserun/configurable.rb', line 138

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.



135
136
137
# File 'lib/pleaserun/configurable.rb', line 135

def description
  @description
end

#munger=(value) ⇒ Object (writeonly)

Sets the attribute munger

Parameters:

  • value

    the value to set the attribute munger to.



136
137
138
# File 'lib/pleaserun/configurable.rb', line 136

def munger=(value)
  @munger = value
end

#nameObject (readonly)

Returns the value of attribute name.



134
135
136
# File 'lib/pleaserun/configurable.rb', line 134

def name
  @name
end

#validator=(value) ⇒ Object (writeonly)

Sets the attribute validator

Parameters:

  • value

    the value to set the attribute validator to.



136
137
138
# File 'lib/pleaserun/configurable.rb', line 136

def validator=(value)
  @validator = value
end

Instance Method Details

#set?Boolean

def value

Returns:

  • (Boolean)


170
171
172
# File 'lib/pleaserun/configurable.rb', line 170

def set?
  return !@value.nil?
end

#validate(v) ⇒ Object

def value=



158
159
160
161
162
# File 'lib/pleaserun/configurable.rb', line 158

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



164
165
166
167
168
# File 'lib/pleaserun/configurable.rb', line 164

def value
  return @value if @value
  return @options[:default] if @options.include?(:default)
  return nil
end

#value=(v) ⇒ Object

def initialize



152
153
154
155
156
# File 'lib/pleaserun/configurable.rb', line 152

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