class Quince::Component
Constants
- HTML_SELECTOR_ATTR
Attributes
__id[R]
children[R]
props[R]
state[R]
Public Class Methods
Props(**kw)
click to toggle source
# File lib/quince/component.rb, line 8 def Props(**kw) self.const_set "Props", TypedStruct.new( { default: Quince::Types::Undefined }, Quince::Component::HTML_SELECTOR_ATTR => String, **kw, ) end
State(**kw)
click to toggle source
# File lib/quince/component.rb, line 16 def State(**kw) st = kw.empty? ? nil : TypedStruct.new( { default: Quince::Types::Undefined }, **kw, ) self.const_set "State", st end
create(*children, **props, &block_children)
click to toggle source
# File lib/quince/component.rb, line 45 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) instance.instance_variable_set(:@children, block_children || children) instance.send :initialize end end
exposed(action, meth0d: :POST)
click to toggle source
# File lib/quince/component.rb, line 24 def exposed(action, meth0d: :POST) @exposed_actions ||= Set.new @exposed_actions.add action route = "/api/#{self.name}/#{action}" Quince.middleware.create_route_handler( verb: meth0d, route: route, ) do |params| instance = Quince::Serialiser.deserialise params[:component] Quince::Component.class_variable_set :@@params, params if @exposed_actions.member? action instance.send action instance else raise "The action you called is not exposed" end end route end
inherited(subclass)
click to toggle source
# File lib/quince/component.rb, line 4 def inherited(subclass) Quince.define_constructor(subclass) end
Private Class Methods
initialize_props(const, id, **props)
click to toggle source
# File lib/quince/component.rb, line 57 def initialize_props(const, id, **props) const::Props.new(HTML_SELECTOR_ATTR => id, **props) if const.const_defined?("Props") end
Public Instance Methods
render()
click to toggle source
# File lib/quince/component.rb, line 67 def render raise "not implemented" end
Protected Instance Methods
params()
click to toggle source
# File lib/quince/component.rb, line 77 def params @@params end
to(route, via: :POST)
click to toggle source
# File lib/quince/component.rb, line 73 def to(route, via: :POST) self.class.exposed route, meth0d: via end
Private Instance Methods
html_element_selector()
click to toggle source
# File lib/quince/component.rb, line 87 def html_element_selector "[#{HTML_SELECTOR_ATTR}='#{__id}']".freeze end