Class: Tailmix::Runtime::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/tailmix/runtime/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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_instanceObject (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

#definitionObject (readonly)

Returns the value of attribute definition.



13
14
15
# File 'lib/tailmix/runtime/context.rb', line 13

def definition
  @definition
end

#idObject (readonly)

Returns the value of attribute id.



13
14
15
# File 'lib/tailmix/runtime/context.rb', line 13

def id
  @id
end

#stateObject (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_proxyObject



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_nameObject



65
66
67
# File 'lib/tailmix/runtime/context.rb', line 65

def component_name
  @component_instance.class.name
end

#definition_payloadObject



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_payloadObject



69
70
71
# File 'lib/tailmix/runtime/context.rb', line 69

def state_payload
  @state.to_h.to_json
end

#state_proxyObject



26
27
28
# File 'lib/tailmix/runtime/context.rb', line 26

def state_proxy
  @state_proxy ||= StateProxy.new(self)
end