Class: Tailmix::Definition::Builders::ElementBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/tailmix/definition/builders/element_builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ ElementBuilder

Returns a new instance of ElementBuilder.



11
12
13
14
15
16
17
18
19
# File 'lib/tailmix/definition/builders/element_builder.rb', line 11

def initialize(name)
  @name = name
  @default_attributes = {}
  @dimensions = {}
  @compound_variants = []
  @event_bindings = []
  @attribute_bindings = {}
  @model_bindings = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



25
26
27
28
# File 'lib/tailmix/definition/builders/element_builder.rb', line 25

def method_missing(name, *args, &block)
  attribute_name = name.to_s.chomp("=").to_sym
  @default_attributes[attribute_name] = args.first
end

Instance Method Details

#attributesObject



21
22
23
# File 'lib/tailmix/definition/builders/element_builder.rb', line 21

def attributes
  @attributes_builder ||= AttributeBuilder.new
end

#bind(attribute_name, to:) ⇒ Object



39
40
41
# File 'lib/tailmix/definition/builders/element_builder.rb', line 39

def bind(attribute_name, to:)
  @attribute_bindings[attribute_name.to_sym] = to.to_sym
end

#build_definitionObject



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tailmix/definition/builders/element_builder.rb', line 68

def build_definition
  Result::Element.new(
    name: @name,
    attributes: attributes.build_definition,
    default_attributes: @default_attributes.freeze,
    dimensions: @dimensions.freeze,
    compound_variants: @compound_variants.freeze,
    event_bindings: @event_bindings.freeze,
    attribute_bindings: @attribute_bindings.freeze,
    model_bindings: @model_bindings.freeze
  )
end

#compound_variant(on:, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/tailmix/definition/builders/element_builder.rb', line 58

def compound_variant(on:, &block)
  builder = VariantBuilder.new
  builder.instance_eval(&block)

  @compound_variants << {
    on: on,
    modifications: builder.build_variant
  }
end

#dimension(name, &block) ⇒ Object



52
53
54
55
56
# File 'lib/tailmix/definition/builders/element_builder.rb', line 52

def dimension(name, &block)
  builder = DimensionBuilder.new
  builder.instance_eval(&block)
  @dimensions[name.to_sym] = builder.build_dimension
end

#model(attribute_name, to:, on: :input, action: nil, debounce: nil) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/tailmix/definition/builders/element_builder.rb', line 43

def model(attribute_name, to:, on: :input, action: nil, debounce: nil)
  @model_bindings[attribute_name.to_sym] = {
    state: to.to_sym,
    event: on,
    action: action,
    debounce: debounce
  }.compact
end

#on(event_name, action_name, with: nil, **options) ⇒ Object



34
35
36
37
# File 'lib/tailmix/definition/builders/element_builder.rb', line 34

def on(event_name, action_name, with: nil, **options)
  # `with` mapping: `{ payload_key => state_key }`
  @event_bindings << { event: event_name, action: action_name, with: with, options: options }
end

#respond_to_missing?(*_args) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/tailmix/definition/builders/element_builder.rb', line 30

def respond_to_missing?(*_args)
  true
end