Class: Tailmix::Runtime::Context
- Inherits:
-
Object
- Object
- Tailmix::Runtime::Context
- Defined in:
- lib/tailmix/runtime/context.rb
Instance Attribute Summary collapse
-
#component_instance ⇒ Object
readonly
Returns the value of attribute component_instance.
-
#definition ⇒ Object
readonly
Returns the value of attribute definition.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#state ⇒ Object
readonly
Returns the value of attribute state.
Instance Method Summary collapse
- #action_proxy ⇒ Object
- #attributes_for(element_name) ⇒ Object
- #component_name ⇒ Object
- #definition_payload ⇒ Object
-
#initialize(component_instance, definition, initial_state = {}, id: nil) ⇒ Context
constructor
A new instance of Context.
- #run_action(action_def, payload) ⇒ Object
- #state_payload ⇒ Object
- #state_proxy ⇒ Object
Constructor Details
#initialize(component_instance, definition, initial_state = {}, id: nil) ⇒ Context
Returns a new instance of Context.
15 16 17 18 19 20 21 22 23 24 |
# File 'lib/tailmix/runtime/context.rb', line 15 def initialize(component_instance, definition, initial_state = {}, id: nil) @component_instance = component_instance @definition = definition @id = id @cache = AttributeCache.new @state = State.new(definition.states, initial_state, cache: @cache) Registry.instance.register(component_instance.class) end |
Instance Attribute Details
#component_instance ⇒ Object (readonly)
Returns the value of attribute component_instance.
13 14 15 |
# File 'lib/tailmix/runtime/context.rb', line 13 def component_instance @component_instance end |
#definition ⇒ Object (readonly)
Returns the value of attribute definition.
13 14 15 |
# File 'lib/tailmix/runtime/context.rb', line 13 def definition @definition end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
13 14 15 |
# File 'lib/tailmix/runtime/context.rb', line 13 def id @id end |
#state ⇒ Object (readonly)
Returns the value of attribute state.
13 14 15 |
# File 'lib/tailmix/runtime/context.rb', line 13 def state @state end |
Instance Method Details
#action_proxy ⇒ Object
30 31 32 |
# File 'lib/tailmix/runtime/context.rb', line 30 def action_proxy @action_proxy ||= ActionProxy.new(self) end |
#attributes_for(element_name) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/tailmix/runtime/context.rb', line 54 def attributes_for(element_name) cached = @cache.get(element_name) return cached if cached element_def = @definition.elements.fetch(element_name) attributes = AttributeBuilder.new(element_def, @state, self).build @cache.set(element_name, attributes) attributes end |
#component_name ⇒ Object
65 66 67 |
# File 'lib/tailmix/runtime/context.rb', line 65 def component_name @component_instance.class.name end |
#definition_payload ⇒ Object
73 74 75 |
# File 'lib/tailmix/runtime/context.rb', line 73 def definition_payload @definition.to_h.to_json end |
#run_action(action_def, payload) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tailmix/runtime/context.rb', line 34 def run_action(action_def, payload) action_def.transitions.each do |transition| # Here we simulate what JS does on the client. case transition[:type] when :set # Processing PayloadProxy, if it exists. value = transition[:payload][:value] if value.is_a?(Hash) && value[:__type] == 'payload_value' set_state(transition[:payload][:key], payload[value[:key]]) else set_state(transition[:payload][:key], value) end when :toggle key = transition[:payload][:key] set_state(key, !get_state(key)) # `refresh` and `dispatch` are purely client-side operations; we ignore them on the server. end end end |
#state_payload ⇒ Object
69 70 71 |
# File 'lib/tailmix/runtime/context.rb', line 69 def state_payload @state.to_h.to_json end |
#state_proxy ⇒ Object
26 27 28 |
# File 'lib/tailmix/runtime/context.rb', line 26 def state_proxy @state_proxy ||= StateProxy.new(self) end |