Class: Hashcraft::Option
- Inherits:
-
Object
- Object
- Hashcraft::Option
- Defined in:
- lib/hashcraft/option.rb
Overview
Defines a method and corresponding attribute for a craftable class.
Instance Attribute Summary collapse
-
#craft ⇒ Object
readonly
Returns the value of attribute craft.
-
#default ⇒ Object
readonly
Returns the value of attribute default.
-
#eager ⇒ Object
(also: #eager?)
readonly
Returns the value of attribute eager.
-
#key ⇒ Object
readonly
Returns the value of attribute key.
-
#mutator ⇒ Object
readonly
Returns the value of attribute mutator.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
-
#craft_value(value, &block) ⇒ Object
:nodoc:.
-
#hash_key ⇒ Object
:nodoc:.
-
#initialize(name, opts = {}) ⇒ Option
constructor
:nodoc:.
-
#meta(key) ⇒ Object
Options are sent into transformers as arguments.
-
#value!(data, key, value) ⇒ Object
:nodoc:.
Constructor Details
#initialize(name, opts = {}) ⇒ Option
:nodoc:
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/hashcraft/option.rb', line 24 def initialize(name, opts = {}) # :nodoc: raise ArgumentError, 'name is required' if name.to_s.empty? @craft = opts[:craft] @default = opts[:default] @eager = opts[:eager] || false @internal_meta = symbolize_keys(opts[:meta] || {}) @key = opts[:key].to_s @mutator = opts[:mutator] @name = name.to_s freeze end |
Instance Attribute Details
#craft ⇒ Object (readonly)
Returns the value of attribute craft.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def craft @craft end |
#default ⇒ Object (readonly)
Returns the value of attribute default.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def default @default end |
#eager ⇒ Object (readonly) Also known as: eager?
Returns the value of attribute eager.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def eager @eager end |
#key ⇒ Object (readonly)
Returns the value of attribute key.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def key @key end |
#mutator ⇒ Object (readonly)
Returns the value of attribute mutator.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def mutator @mutator end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
15 16 17 |
# File 'lib/hashcraft/option.rb', line 15 def name @name end |
Instance Method Details
#craft_value(value, &block) ⇒ Object
:nodoc:
53 54 55 |
# File 'lib/hashcraft/option.rb', line 53 def craft_value(value, &block) # :nodoc: craft ? craft.new(value, &block) : value end |
#hash_key ⇒ Object
:nodoc:
49 50 51 |
# File 'lib/hashcraft/option.rb', line 49 def hash_key # :nodoc: key.empty? ? name : key end |
#meta(key) ⇒ Object
Options are sent into transformers as arguments. Leverage the meta key for an option to store any additional data that you may need in transformers. This method provides a quick message-based entry point into inspecting the meta key’s value.
45 46 47 |
# File 'lib/hashcraft/option.rb', line 45 def (key) [key.to_s.to_sym] end |
#value!(data, key, value) ⇒ Object
:nodoc:
38 39 40 |
# File 'lib/hashcraft/option.rb', line 38 def value!(data, key, value) # :nodoc: Mutators.instance.value!(mutator, data, key, value) end |