sandals 0.1.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.
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sandals
4
+ class Application
5
+ attr_reader :refresh_interval
6
+
7
+ def initialize(title:, &definition)
8
+ @title = title
9
+ @definition = definition
10
+ @refresh_interval = nil
11
+ @view_definition = nil
12
+ @snapshots = SnapshotHistory.new
13
+ instance_eval(&definition)
14
+ raise ArgumentError, "Sandals.app requires a view block" unless @view_definition
15
+ end
16
+
17
+ def new_session
18
+ self.class.new(title: @title, &@definition)
19
+ end
20
+
21
+ def view(&block)
22
+ @view_definition = block if block
23
+ @view_definition
24
+ end
25
+
26
+ def refresh(every:)
27
+ interval = Float(every, exception: false)
28
+ unless interval&.finite? && interval >= 1
29
+ raise ArgumentError, "refresh interval must be at least one second"
30
+ end
31
+
32
+ @refresh_interval = interval
33
+ end
34
+
35
+ def select(label, **options, &action)
36
+ @current_view.select(label, **options, &action)
37
+ end
38
+
39
+ def title(*arguments)
40
+ return @title if arguments.empty?
41
+
42
+ @current_view.title(*arguments)
43
+ end
44
+
45
+ def render_view
46
+ @current_view = View.new
47
+ instance_eval(&view)
48
+ @current_view
49
+ ensure
50
+ @current_view = nil
51
+ end
52
+
53
+ def render(development_reload: false)
54
+ snapshot = publish_view
55
+ HTMLRenderer.new.render(
56
+ self,
57
+ snapshot.view,
58
+ revision: snapshot.revision,
59
+ development_reload:
60
+ )
61
+ end
62
+
63
+ def dispatch(revision:, events:, oldest_pending_revision: revision)
64
+ if oldest_pending_revision > revision
65
+ raise ArgumentError,
66
+ "oldest pending revision cannot be newer than event revision"
67
+ end
68
+
69
+ snapshot = @snapshots.fetch(revision)
70
+
71
+ EventDispatcher.new(snapshot.controls).dispatch(events)
72
+
73
+ next_snapshot = publish_view
74
+ @snapshots.discard_before(oldest_pending_revision)
75
+ [next_snapshot.revision, HTMLRenderer.new.render_fragment(next_snapshot.view)]
76
+ end
77
+
78
+ private
79
+
80
+ def publish_view
81
+ view = render_view
82
+ @snapshots.publish(view, controls: controls_in(view))
83
+ end
84
+
85
+ def current_snapshot
86
+ @snapshots.current
87
+ end
88
+
89
+ def controls_in(container)
90
+ container.children.flat_map do |element|
91
+ case element
92
+ when Container
93
+ controls_in(element)
94
+ when TextField, NumberField, TextArea, Checkbox, Select, FileField, Button
95
+ element
96
+ else
97
+ []
98
+ end
99
+ end
100
+ end
101
+
102
+ def method_missing(name, *arguments, **options, &block)
103
+ if @current_view&.respond_to?(name)
104
+ @current_view.public_send(name, *arguments, **options, &block)
105
+ else
106
+ super
107
+ end
108
+ end
109
+
110
+ def respond_to_missing?(name, include_private = false)
111
+ @current_view&.respond_to?(name) || super
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,20 @@
1
+ Copyright (c) 37signals
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.