quince 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1b5c0ef933d95f7026239bcfc5685e4aeb1582382056725b245cd37838540bf4
4
- data.tar.gz: c790f0bc40602888167f46bf9636c55a739d5191645fd43e3614c78e381c6d1f
3
+ metadata.gz: 2189200f4bee6758d6a981e3f4ba7b0d8dc558d0e0e2bb66fd421c11b404f954
4
+ data.tar.gz: 4153ceb85c057cfe7c2e74852aee81b82dde74362aecec53647b485fea191b87
5
5
  SHA512:
6
- metadata.gz: d7f561f03a959ed9fb221d0462f8901fe9f0cb20bda9e80ede4757a6a5cf89c979da5f80c135054454ef64ad5ddec09751a23088fe08762084b13c097c56db3c
7
- data.tar.gz: 47f28845aad32b4ba53f76acf4fab7695c903cb8d94197b6ac0096f16a2f048684f5677d35edeaa0c06554ae19d32152294f878e5ef5e8cbd9eccd581671389a
6
+ metadata.gz: 923689df34d18c7357785812424acc62f38f89a3966c568bfd6e86f03c41eb1a9dd629eebdee4b1ae9321e42e1180d0be65af560613e31543496ebc312c40a30
7
+ data.tar.gz: 2624224d31f066ef9562333264e6f1cc00275bf72ab35942a1b0b3097864a14c89f04a6d4ebeee7ffb07ce240f22bde8f07939b61ee64a8019017ceceead23c9
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- quince (0.2.1)
4
+ quince (0.3.0)
5
5
  oj (~> 3.13)
6
6
  typed_struct (>= 0.1.4)
7
7
 
@@ -11,7 +11,7 @@ module Quince
11
11
  t = Quince::Types
12
12
  opt_string_sym = Rbs("#{t::OptionalString} | Symbol")
13
13
  opt_bool = t::OptionalBoolean
14
- opt_method = Rbs("Method | Quince::Types::Undefined")
14
+ opt_callback = Rbs("Quince::Callback::Interface | Quince::Types::Undefined")
15
15
  value = opt_string_sym # for now
16
16
 
17
17
  ATTRIBUTES_BY_ELEMENT = {
@@ -468,13 +468,13 @@ module Quince
468
468
  }.freeze
469
469
 
470
470
  DOM_EVENTS = {
471
- onclick: opt_method,
472
- onsubmit: opt_method,
473
- onblur: opt_method,
474
- onchange: opt_method,
475
- onsearch: opt_method,
476
- onkeyup: opt_method,
477
- onselect: opt_method,
471
+ onclick: opt_callback,
472
+ onsubmit: opt_callback,
473
+ onblur: opt_callback,
474
+ onchange: opt_callback,
475
+ onsearch: opt_callback,
476
+ onkeyup: opt_callback,
477
+ onselect: opt_callback,
478
478
  }.freeze
479
479
  end
480
480
  end
@@ -0,0 +1,36 @@
1
+ module Quince
2
+ class Callback
3
+ module ComponentHelpers
4
+ protected
5
+
6
+ DEFAULT_CALLBACK_OPTIONS = {
7
+ prevent_default: false,
8
+ take_form_values: false,
9
+ # others could include:
10
+ # debounce: false,
11
+ }.freeze
12
+
13
+ def callback(method_name, **opts)
14
+ unless self.class.instance_variable_get(:@exposed_actions).member?(method_name)
15
+ raise "The action you called is not exposed"
16
+ end
17
+
18
+ opts = DEFAULT_CALLBACK_OPTIONS.merge opts
19
+
20
+ Callback.new(self, method_name, **opts)
21
+ end
22
+ end
23
+
24
+ module Interface
25
+ attr_reader :receiver, :method_name, :prevent_default, :take_form_values
26
+ end
27
+
28
+ include Interface
29
+
30
+ def initialize(receiver, method_name, prevent_default:, take_form_values:)
31
+ @receiver, @method_name = receiver, method_name
32
+ @prevent_default = prevent_default
33
+ @take_form_values = take_form_values
34
+ end
35
+ end
36
+ end
@@ -1,3 +1,5 @@
1
+ require_relative "callback"
2
+
1
3
  module Quince
2
4
  class Component
3
5
  class << self
@@ -29,7 +31,7 @@ module Quince
29
31
  verb: meth0d,
30
32
  route: route,
31
33
  ) do |params|
32
- instance = Quince::Serialiser.deserialise params[:component]
34
+ instance = Quince::Serialiser.deserialise(CGI.unescapeHTML(params[:component]))
33
35
  Quince::Component.class_variable_set :@@params, params
34
36
  if @exposed_actions.member? action
35
37
  instance.send action
@@ -47,7 +49,8 @@ module Quince
47
49
  id = SecureRandom.alphanumeric 6
48
50
  instance.instance_variable_set :@__id, id
49
51
  instance.instance_variable_set :@props, initialize_props(self, id, **props)
50
- instance.instance_variable_set(:@children, block_children || children)
52
+ kids = block_children ? block_children.call : children
53
+ instance.instance_variable_set(:@children, kids)
51
54
  instance.send :initialize
52
55
  end
53
56
  end
@@ -59,6 +62,8 @@ module Quince
59
62
  end
60
63
  end
61
64
 
65
+ include Callback::ComponentHelpers
66
+
62
67
  # set default
63
68
  @@params = {}
64
69
 
@@ -82,7 +87,7 @@ module Quince
82
87
 
83
88
  attr_reader :__id
84
89
 
85
- HTML_SELECTOR_ATTR = :"data-respid"
90
+ HTML_SELECTOR_ATTR = :"data-quid"
86
91
 
87
92
  def html_element_selector
88
93
  "[#{HTML_SELECTOR_ATTR}='#{__id}']".freeze
@@ -33,26 +33,22 @@ module Quince
33
33
  attrib = case value
34
34
  when String, Integer, Float, Symbol
35
35
  value.to_s
36
- when Method
37
- owner = value.owner
36
+ when Callback::Interface
38
37
  receiver = value.receiver
39
- name = value.name
38
+ owner = receiver.class.name
39
+ name = value.method_name
40
40
  selector = receiver.send :html_element_selector
41
41
  internal = Quince::Serialiser.serialise receiver
42
- payload = { component: internal }.to_json
43
- case key
44
- when :onclick
45
- CGI.escape_html(
46
- "callRemoteEndpoint(`/api/#{owner}/#{name}`,`#{payload}`,`#{selector}`)"
47
- )
48
- when :onsubmit, :onchange, :onblur, :onsearch, :onkeyup, :onselect
49
- ev = "const p = #{payload}; callRemoteEndpoint( `/api/#{owner}/#{name}`, JSON.stringify({...p, params: getFormValues(this)}), `#{selector}`)"
50
- case key
51
- when :onsubmit
52
- ev += "; return false"
42
+ payload = { component: CGI.escapeHTML(internal) }.to_json
43
+ payload_var_name = "p"
44
+ stringify_payload = if value.take_form_values
45
+ "{...#{payload_var_name}, params: getFormValues(this)}"
46
+ else
47
+ payload_var_name
53
48
  end
54
- CGI.escape_html(ev)
55
- end
49
+ cb = %Q{const #{payload_var_name} = #{payload}; callRemoteEndpoint(`/api/#{owner}/#{name}`, JSON.stringify(#{stringify_payload}),`#{selector}`)}
50
+ cb += ";return false" if value.prevent_default
51
+ CGI.escape_html(cb)
56
52
  when true
57
53
  return key
58
54
  when false, nil, Quince::Types::Undefined
@@ -2,98 +2,11 @@ module Quince
2
2
  class Serialiser
3
3
  class << self
4
4
  def serialise(obj)
5
- val = case obj
6
- when Quince::Component
7
- {
8
- id: serialise(obj.send(:__id)),
9
- props: serialise(obj.props),
10
- state: serialise(obj.state),
11
- children: serialise(obj.children),
12
- html_element_selector: serialise(obj.send(:html_element_selector)),
13
- }
14
- when Array
15
- obj.map { |e| serialise e }
16
- when TypedStruct, Struct, OpenStruct, Hash
17
- result = obj.each_pair.each_with_object({}) do |(k, v), ob|
18
- case v
19
- when Undefined
20
- next
21
- else
22
- ob[k] = serialise(v)
23
- end
24
- end
25
- if result.empty? && !obj.is_a?(Hash)
26
- obj = nil
27
- nil
28
- else
29
- result
30
- end
31
- when Proc
32
- obj = obj.call # is there a more efficient way of doing this?
33
- serialise(obj)
34
- when String
35
- res = obj.gsub "\n", '\n'
36
- res.gsub! ?", '\"'
37
- res
38
- else
39
- obj
40
- end
41
-
42
- { t: obj.class&.name, v: val }
5
+ Oj.dump(obj)
43
6
  end
44
7
 
45
8
  def deserialise(json)
46
- case json[:t]
47
- when "String", "Integer", "Float", "NilClass", "TrueClass", "FalseClass"
48
- json[:v]
49
- when "Symbol"
50
- json[:v].to_sym
51
- when "Array"
52
- json[:v].map { |e| deserialise e }
53
- when "Hash"
54
- transform_hash json[:v]
55
- when "OpenStruct"
56
- OpenStruct.new(**transform_hash(props))
57
- when nil
58
- nil
59
- else
60
- klass = Object.const_get(json[:t])
61
- if klass < TypedStruct
62
- transform_hash_for_struct(json[:v]) || {}
63
- elsif klass < Quince::Component
64
- instance = klass.allocate
65
- val = json[:v]
66
- id = deserialise val[:id]
67
-
68
- instance.instance_variable_set :@__id, id
69
- instance.instance_variable_set(
70
- :@props,
71
- klass.send(
72
- :initialize_props,
73
- klass,
74
- id,
75
- **(deserialise(val[:props]) || {}),
76
- ),
77
- )
78
- st = deserialise(val[:state])
79
- instance.instance_variable_set :@state, klass::State.new(**st) if st
80
- instance.instance_variable_set :@children, deserialise(val[:children])
81
- instance
82
- else
83
- klass = Object.const_get(json[:t])
84
- klass.new(deserialise(json[:v]))
85
- end
86
- end
87
- end
88
-
89
- private
90
-
91
- def transform_hash(hsh)
92
- hsh.transform_values! { |v| deserialise v }
93
- end
94
-
95
- def transform_hash_for_struct(hsh)
96
- hsh.to_h { |k, v| [k.to_sym, deserialise(v)] }
9
+ Oj.load(json)
97
10
  end
98
11
  end
99
12
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Quince
4
- VERSION = "0.2.1"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: quince
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joseph Johansen
@@ -57,6 +57,7 @@ files:
57
57
  - bin/setup
58
58
  - lib/quince.rb
59
59
  - lib/quince/attributes_by_element.rb
60
+ - lib/quince/callback.rb
60
61
  - lib/quince/component.rb
61
62
  - lib/quince/html_tag_components.rb
62
63
  - lib/quince/serialiser.rb