Class: Restful::Configuration::Option
- Inherits:
-
Object
- Object
- Restful::Configuration::Option
- Defined in:
- lib/restful/configuration.rb
Overview
Named option
-
:name => the name of the option, for use when configuring
-
:type => a default type for a complex option (like an Array, Hash or Resource)
-
:element_type => specifies the type for elements of a container like an Array or Hash
-
:default => if no type is given, may provide a default (such as
false
or:value
)
Instance Attribute Summary collapse
-
#default ⇒ Object
Returns the value of attribute default.
- #element_type ⇒ Object
-
#name ⇒ Object
Returns the value of attribute name.
- #type ⇒ Object
Instance Method Summary collapse
-
#generate_from(value) ⇒ Object
Generates a new option value from the given configuration.
-
#initialize(name, options = {}) ⇒ Option
constructor
A new instance of Option.
-
#initialized ⇒ Object
Constructs a new option instance of the given type (or nil).
- #mutator_method ⇒ Object
Constructor Details
#initialize(name, options = {}) ⇒ Option
Returns a new instance of Option.
201 202 203 204 205 206 207 208 |
# File 'lib/restful/configuration.rb', line 201 def initialize(name, = {}) = ( || {}).symbolize_keys self.name = name.to_sym self.type = [:type] self.element_type = [:element_type] self.default = [:default] end |
Instance Attribute Details
#default ⇒ Object
Returns the value of attribute default.
198 199 200 |
# File 'lib/restful/configuration.rb', line 198 def default @default end |
#element_type ⇒ Object
219 220 221 |
# File 'lib/restful/configuration.rb', line 219 def element_type @element_type_class ||= _to_class(@element_type) end |
#name ⇒ Object
Returns the value of attribute name.
198 199 200 |
# File 'lib/restful/configuration.rb', line 198 def name @name end |
#type ⇒ Object
214 215 216 217 |
# File 'lib/restful/configuration.rb', line 214 def type # lazy initialization @type_class ||= _to_class(@type) end |
Instance Method Details
#generate_from(value) ⇒ Object
Generates a new option value from the given configuration. If type/element_type are configured this will generate new options of the given type from passed configuration Hashes.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/restful/configuration.rb', line 234 def generate_from(value) if type.nil? value elsif type <= Configurator case value when Hash then type.new(value) when Configurator then value else raise(ArgumentError, "Expected option '#{name}' to be of type '#{type}', but received '#{value}'") end elsif type <= Array case value when Array element_type ? value.map { |e| _element_of_type(e) } : value else Array(value) end elsif type <= Hash case value when Hash if element_type value.inject({}) do |hash,row| hash[row[0].to_sym] = _element_of_type(row[1]) hash end else value.symbolize_keys end when Array then value.inject({}) { |hash,v| hash[v] = nil; hash } else { value => nil } end else # A type we don't handle value end end |
#initialized ⇒ Object
Constructs a new option instance of the given type (or nil)
224 225 226 227 228 229 |
# File 'lib/restful/configuration.rb', line 224 def initialized case when type then type.new else default end end |
#mutator_method ⇒ Object
210 211 212 |
# File 'lib/restful/configuration.rb', line 210 def mutator_method "#{name}=" end |