Module: Hashcraft::Dsl

Included in:
Base
Defined in:
lib/hashcraft/dsl.rb

Overview

The class API used to define options for a craftable class. Each class stores its own OptionSet instance along with materializing one for its inheritance chain (child has precedence.)

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#local_key_transformerObject (readonly)

Returns the value of attribute local_key_transformer.



17
18
19
# File 'lib/hashcraft/dsl.rb', line 17

def local_key_transformer
  @local_key_transformer
end

#local_value_transformerObject (readonly)

Returns the value of attribute local_value_transformer.



17
18
19
# File 'lib/hashcraft/dsl.rb', line 17

def local_value_transformer
  @local_value_transformer
end

Instance Method Details

#find_option(name) ⇒ Object

:nodoc:



52
53
54
# File 'lib/hashcraft/dsl.rb', line 52

def find_option(name) # :nodoc:
  option_set.find(name)
end

#key_transformer(name) ⇒ Object

DSL Method used to declare what the sub-class should use as a transformer for all keys. It will follow the typical inheritance chain and find the closest transformer to use (child-first).



23
24
25
# File 'lib/hashcraft/dsl.rb', line 23

def key_transformer(name)
  tap { @local_key_transformer = name }
end

#key_transformer_to_useObject

:nodoc:



34
35
36
37
38
39
40
41
# File 'lib/hashcraft/dsl.rb', line 34

def key_transformer_to_use # :nodoc:
  return @key_transformer_to_use if @key_transformer_to_use

  @key_transformer_to_use =
    ancestors.select { |a| a < Base }
             .find(&:local_key_transformer)
             &.local_key_transformer
end

#local_option_setObject

:nodoc:



88
89
90
# File 'lib/hashcraft/dsl.rb', line 88

def local_option_set # :nodoc:
  @local_option_set ||= Generic::Dictionary.new(key: :name)
end

#option(*args) ⇒ Object

The main class-level DSL method consumed by sub-classes. This is the entry-point for the declaration of available options.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/hashcraft/dsl.rb', line 58

def option(*args)
  opts = args.last.is_a?(Hash) ? args.pop : {}

  args.each do |key|
    option = Option.new(key, opts)

    local_option_set.add(option)

    method_name = option.name

    class_eval <<~RUBY, __FILE__, __LINE__ + 1
      def #{method_name}(opts = {}, &block)
        option = find_option('#{method_name}')

        value!(option, opts, &block)
      end
    RUBY
  end

  self
end

#option_setObject

:nodoc:



80
81
82
83
84
85
86
# File 'lib/hashcraft/dsl.rb', line 80

def option_set # :nodoc:
  @option_set ||=
    ancestors
    .reverse
    .select { |a| a < Base }
    .each_with_object(Generic::Dictionary.new) { |a, memo| memo.merge!(a.local_option_set) }
end

#value_transformer(name) ⇒ Object

DSL Method used to declare what the sub-class should use as a transformer for all values. It will follow the typical inheritance chain and find the closest transformer to use (child-first).



30
31
32
# File 'lib/hashcraft/dsl.rb', line 30

def value_transformer(name)
  tap { @local_value_transformer = name }
end

#value_transformer_to_useObject

:nodoc:



43
44
45
46
47
48
49
50
# File 'lib/hashcraft/dsl.rb', line 43

def value_transformer_to_use # :nodoc:
  return @value_transformer_to_use if @value_transformer_to_use

  @value_transformer_to_use =
    ancestors.select { |a| a < Base }
             .find(&:local_value_transformer)
             &.local_value_transformer
end