Class: Tailmix::Definition::Builders::ComponentBuilder
- Inherits:
-
Object
- Object
- Tailmix::Definition::Builders::ComponentBuilder
- Defined in:
- lib/tailmix/definition/builders/component_builder.rb
Instance Attribute Summary collapse
-
#component_name ⇒ Object
readonly
Returns the value of attribute component_name.
Instance Method Summary collapse
- #action(name, &block) ⇒ Object
- #build_definition ⇒ Object
- #element(name, classes = "", &block) ⇒ Object
-
#initialize(component_name:) ⇒ ComponentBuilder
constructor
A new instance of ComponentBuilder.
- #plugin(name, options = {}) ⇒ Object
- #react(on:, run: nil, **options, &block) ⇒ Object
- #state(name, default: nil, endpoint: nil, toggle: false) ⇒ Object
Constructor Details
#initialize(component_name:) ⇒ ComponentBuilder
Returns a new instance of ComponentBuilder.
14 15 16 17 18 19 20 21 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 14 def initialize(component_name:) @states = {} @actions = {} @elements = {} @component_name = component_name @plugins = {} @reactions = {} end |
Instance Attribute Details
#component_name ⇒ Object (readonly)
Returns the value of attribute component_name.
12 13 14 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 12 def component_name @component_name end |
Instance Method Details
#action(name, &block) ⇒ Object
31 32 33 34 35 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 31 def action(name, &block) builder = ActionBuilder.new builder.instance_exec(PayloadProxy.new, &block) @actions[name.to_sym] = builder end |
#build_definition ⇒ Object
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 68 def build_definition Result::Context.new( name: component_name, states: @states.freeze, actions: @actions.transform_values(&:build_definition).freeze, elements: @elements.transform_values(&:build_definition).freeze, reactions: @reactions.freeze, plugins: @plugins.freeze ) end |
#element(name, classes = "", &block) ⇒ Object
37 38 39 40 41 42 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 37 def element(name, classes = "", &block) builder = ElementBuilder.new(name) builder.attributes.classes(classes.split) builder.instance_eval(&block) if block @elements[name.to_sym] = builder end |
#plugin(name, options = {}) ⇒ Object
44 45 46 47 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 44 def plugin(name, = {}) plugin_name = name.to_s.gsub(/_([a-z])/) { $1.upcase } @plugins[plugin_name] = end |
#react(on:, run: nil, **options, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 49 def react(on:, run: nil, **, &block) watched_states = Array(on) # Processing the short form: `react on: :query, run: :search` if run builder = ReactorBuilder.new(watched_states.first) builder.run(run, **) watched_states.each { |state| (@reactions[state] ||= []).concat(builder.build_rules) } return end # Processing the full form with the block. if block builder = ReactorBuilder.new(watched_states.first) builder.instance_eval(&block) # `instance_eval` чтобы не писать `r.` watched_states.each { |state| (@reactions[state] ||= []).concat(builder.build_rules) } end end |
#state(name, default: nil, endpoint: nil, toggle: false) ⇒ Object
23 24 25 26 27 28 29 |
# File 'lib/tailmix/definition/builders/component_builder.rb', line 23 def state(name, default: nil, endpoint: nil, toggle: false) @states[name.to_sym] = { default: default, endpoint: endpoint }.compact if toggle action_name = :"toggle_#{name}" action(action_name) { toggle name } end end |