Class: PleaseRun::Configurable::Facet
- Inherits:
-
Object
- Object
- PleaseRun::Configurable::Facet
- 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
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#munger ⇒ Object
writeonly
Sets the attribute munger.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#validator ⇒ Object
writeonly
Sets the attribute validator.
Instance Method Summary collapse
-
#initialize(name, description, options = {}, &facet_dsl) ⇒ Facet
constructor
A new instance of Facet.
-
#set? ⇒ Boolean
def value.
-
#validate(v) ⇒ Object
def value=.
-
#value ⇒ Object
def validate.
-
#value=(v) ⇒ Object
def initialize.
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, = {}, &facet_dsl) insist { name }.is_a?(Symbol) insist { description }.is_a?(String) insist { }.is_a?(Hash) @name = name @description = description @options = FacetDSL.new(self, &facet_dsl) if block_given? validate(@options[:default]) if @options[:default] end |
Instance Attribute Details
#description ⇒ Object (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
146 147 148 |
# File 'lib/pleaserun/configurable.rb', line 146 def munger=(value) @munger = value end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
143 144 145 |
# File 'lib/pleaserun/configurable.rb', line 143 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
145 146 147 |
# File 'lib/pleaserun/configurable.rb', line 145 def @options end |
#validator=(value) ⇒ Object (writeonly)
Sets the attribute validator
146 147 148 |
# File 'lib/pleaserun/configurable.rb', line 146 def validator=(value) @validator = value end |
Instance Method Details
#set? ⇒ Boolean
def value
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 |
#value ⇒ Object
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 |