Class: Attrify::DSL::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/attrify/dsl/variant.rb

Instance Method Summary collapse

Constructor Details

#initializeVariant

Returns a new instance of Variant.



8
9
10
# File 'lib/attrify/dsl/variant.rb', line 8

def initialize
  @variants = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/attrify/dsl/variant.rb', line 21

def method_missing(name, *args, &block)
  # Handle the case where the variant is provided as a block
  if block
    @variants[name] = NestedVariant.new.build(&block)
  # Handle the case where the variant is provided directly as a hash
  elsif args.length == 1 && args[0].is_a?(Hash)
    @variants[name] = args[0]
  else
    super
  end
end

Instance Method Details

#buildObject



12
13
14
15
# File 'lib/attrify/dsl/variant.rb', line 12

def build(&)
  instance_eval(&)
  @variants
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to_missing?(name, include_private = false)
  true
end