Class: Hashcraft::Option

Inherits:
Object
  • Object
show all
Defined in:
lib/hashcraft/option.rb

Overview

Defines a method and corresponding attribute for a craftable class.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts = {}) ⇒ Option

:nodoc:

Raises:

  • (ArgumentError)


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

#craftObject (readonly)

Returns the value of attribute craft.



15
16
17
# File 'lib/hashcraft/option.rb', line 15

def craft
  @craft
end

#defaultObject (readonly)

Returns the value of attribute default.



15
16
17
# File 'lib/hashcraft/option.rb', line 15

def default
  @default
end

#eagerObject (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

#keyObject (readonly)

Returns the value of attribute key.



15
16
17
# File 'lib/hashcraft/option.rb', line 15

def key
  @key
end

#mutatorObject (readonly)

Returns the value of attribute mutator.



15
16
17
# File 'lib/hashcraft/option.rb', line 15

def mutator
  @mutator
end

#nameObject (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_keyObject

: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 meta(key)
  internal_meta[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