Class: Quince::Component

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Quince::Callback::ComponentHelpers, HtmlTagComponents
Defined in:
lib/quince/component.rb

Constant Summary

Constants included from HtmlTagComponents

HtmlTagComponents::ATTRIBUTES_BY_ELEMENT, HtmlTagComponents::CALLBACK_ERB_INSTANCE, HtmlTagComponents::CALLBACK_TEMPLATE, HtmlTagComponents::DOM_EVENTS, HtmlTagComponents::GLOBAL_HTML_ATTRS, HtmlTagComponents::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HtmlTagComponents

#error_message_styles, #internal_scripts

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



82
83
84
# File 'lib/quince/component.rb', line 82

def children
  @children
end

#propsObject (readonly)

Returns the value of attribute props.



82
83
84
# File 'lib/quince/component.rb', line 82

def props
  @props
end

#stateObject (readonly)

Returns the value of attribute state.



82
83
84
# File 'lib/quince/component.rb', line 82

def state
  @state
end

Class Method Details

.create(*children, **props, &block_children) ⇒ Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/quince/component.rb', line 55

def create(*children, **props, &block_children)
  allocate.tap do |instance|
    id = SecureRandom.alphanumeric 6
    instance.instance_variable_set :@__id, id
    instance.instance_variable_set :@props, initialize_props(self, id, **props)
    kids = block_children ? block_children.call : children
    instance.instance_variable_set(:@children, kids)
    instance.send :initialize
  end
end

.exposed(action, method: :POST) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/quince/component.rb', line 24

def exposed(action, method: :POST)
  @exposed_actions ||= Set.new
  @exposed_actions.add action
  route = "/api/#{self.name}/#{action}"
  Quince::SinatraApp.create_route_handler(
    verb: method,
    route: route,
  ) do |bind|
    Thread.current[:request_binding] = bind
    params = bind.receiver.params
    Thread.current[:params] = params[:params] || {}
    instance = Quince::Serialiser.deserialise(CGI.unescapeHTML(params[:component]))
    if params[:rerender]
      instance.instance_variable_set :@state_container, params[:stateContainer]
      render_with = params[:rerender][:method].to_sym
    else
      render_with = :render
    end
    instance.instance_variable_set :@render_with, render_with
    instance.instance_variable_set :@callback_event, params[:event]
    if @exposed_actions.member? action
      instance.send action
      instance
    else
      raise "The action you called is not exposed"
    end
  end

  route
end

.inherited(subclass) ⇒ Object



7
8
9
# File 'lib/quince/component.rb', line 7

def inherited(subclass)
  Quince.define_constructor(subclass)
end

.Props(**kw) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/quince/component.rb', line 11

def Props(**kw)
  self.const_set "Props", Quince::Config.props_struct_type.new(
    Quince::Component::PARENT_SELECTOR_ATTR => String,
    Quince::Component::SELF_SELECTOR => String,
    **kw,
  )
end

.State(**kw) ⇒ Object



19
20
21
22
# File 'lib/quince/component.rb', line 19

def State(**kw)
  st = kw.empty? ? nil : Quince::Config.state_struct_type.new(**kw)
  self.const_set "State", st
end

Instance Method Details

#renderObject



84
85
86
# File 'lib/quince/component.rb', line 84

def render
  raise "not implemented"
end