superfluid 0.0.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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data/LICENSE.adoc +134 -0
- data/README.adoc +226 -0
- data/lib/superfluid/environment.rb +84 -0
- data/lib/superfluid/registries/filter.rb +78 -0
- data/lib/superfluid/renderer.rb +17 -0
- data/lib/superfluid/systems/memory.rb +21 -0
- data/lib/superfluid/tags/template.rb +41 -0
- data/lib/superfluid.rb +23 -0
- data/superfluid.gemspec +35 -0
- data.tar.gz.sig +0 -0
- metadata +153 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 46ef69a407c3fbeaa0e095f67b3cf42bf8862e4fc6d9a8327807619d6058bdcc
|
|
4
|
+
data.tar.gz: fddb30cd353a521163f394fd9fc3b5b30235d19a3e48be6357d791d89a7ec13e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 900585c327e6a3539a6feb06be51b7d217b2b6bb8712ac7d436ff9d37ff774b12f4c6be89b9f4b7170b8f7b0779664fa43ea3e64b940419ec7a4620f19eafb55
|
|
7
|
+
data.tar.gz: 0e398a2b3683039d6b247ea8b1fa33d75491b702d938534918d969ea86b1d8f1ce1daac4295d8de0ef5283228d7e3e3ff3771bac0f636a79d58d8fc39b2a23d1
|
checksums.yaml.gz.sig
ADDED
|
Binary file
|
data/LICENSE.adoc
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
= Hippocratic License
|
|
2
|
+
|
|
3
|
+
Version: 2.1.0.
|
|
4
|
+
|
|
5
|
+
Purpose. The purpose of this License is for the Licensor named above to
|
|
6
|
+
permit the Licensee (as defined below) broad permission, if consistent
|
|
7
|
+
with Human Rights Laws and Human Rights Principles (as each is defined
|
|
8
|
+
below), to use and work with the Software (as defined below) within the
|
|
9
|
+
full scope of Licensor’s copyright and patent rights, if any, in the
|
|
10
|
+
Software, while ensuring attribution and protecting the Licensor from
|
|
11
|
+
liability.
|
|
12
|
+
|
|
13
|
+
Permission and Conditions. The Licensor grants permission by this
|
|
14
|
+
license ("License"), free of charge, to the extent of Licensor’s
|
|
15
|
+
rights under applicable copyright and patent law, to any person or
|
|
16
|
+
entity (the "Licensee") obtaining a copy of this software and
|
|
17
|
+
associated documentation files (the "Software"), to do everything with
|
|
18
|
+
the Software that would otherwise infringe (i) the Licensor’s copyright
|
|
19
|
+
in the Software or (ii) any patent claims to the Software that the
|
|
20
|
+
Licensor can license or becomes able to license, subject to all of the
|
|
21
|
+
following terms and conditions:
|
|
22
|
+
|
|
23
|
+
* Acceptance. This License is automatically offered to every person and
|
|
24
|
+
entity subject to its terms and conditions. Licensee accepts this
|
|
25
|
+
License and agrees to its terms and conditions by taking any action with
|
|
26
|
+
the Software that, absent this License, would infringe any intellectual
|
|
27
|
+
property right held by Licensor.
|
|
28
|
+
* Notice. Licensee must ensure that everyone who gets a copy of any part
|
|
29
|
+
of this Software from Licensee, with or without changes, also receives
|
|
30
|
+
the License and the above copyright notice (and if included by the
|
|
31
|
+
Licensor, patent, trademark and attribution notice). Licensee must cause
|
|
32
|
+
any modified versions of the Software to carry prominent notices stating
|
|
33
|
+
that Licensee changed the Software. For clarity, although Licensee is
|
|
34
|
+
free to create modifications of the Software and distribute only the
|
|
35
|
+
modified portion created by Licensee with additional or different terms,
|
|
36
|
+
the portion of the Software not modified must be distributed pursuant to
|
|
37
|
+
this License. If anyone notifies Licensee in writing that Licensee has
|
|
38
|
+
not complied with this Notice section, Licensee can keep this License by
|
|
39
|
+
taking all practical steps to comply within 30 days after the notice. If
|
|
40
|
+
Licensee does not do so, Licensee’s License (and all rights licensed
|
|
41
|
+
hereunder) shall end immediately.
|
|
42
|
+
* Compliance with Human Rights Principles and Human Rights Laws.
|
|
43
|
+
[arabic]
|
|
44
|
+
. Human Rights Principles.
|
|
45
|
+
[loweralpha]
|
|
46
|
+
.. Licensee is advised to consult the articles of the United Nations
|
|
47
|
+
Universal Declaration of Human Rights and the United Nations Global
|
|
48
|
+
Compact that define recognized principles of international human rights
|
|
49
|
+
(the "Human Rights Principles"). Licensee shall use the Software in a
|
|
50
|
+
manner consistent with Human Rights Principles.
|
|
51
|
+
.. Unless the Licensor and Licensee agree otherwise, any dispute,
|
|
52
|
+
controversy, or claim arising out of or relating to (i) Section 1(a)
|
|
53
|
+
regarding Human Rights Principles, including the breach of Section 1(a),
|
|
54
|
+
termination of this License for breach of the Human Rights Principles,
|
|
55
|
+
or invalidity of Section 1(a) or (ii) a determination of whether any Law
|
|
56
|
+
is consistent or in conflict with Human Rights Principles pursuant to
|
|
57
|
+
Section 2, below, shall be settled by arbitration in accordance with the
|
|
58
|
+
Hague Rules on Business and Human Rights Arbitration (the "Rules");
|
|
59
|
+
provided, however, that Licensee may elect not to participate in such
|
|
60
|
+
arbitration, in which event this License (and all rights licensed
|
|
61
|
+
hereunder) shall end immediately. The number of arbitrators shall be one
|
|
62
|
+
unless the Rules require otherwise.
|
|
63
|
+
+
|
|
64
|
+
Unless both the Licensor and Licensee agree to the contrary: (1) All
|
|
65
|
+
documents and information concerning the arbitration shall be public and
|
|
66
|
+
may be disclosed by any party; (2) The repository referred to under
|
|
67
|
+
Article 43 of the Rules shall make available to the public in a timely
|
|
68
|
+
manner all documents concerning the arbitration which are communicated
|
|
69
|
+
to it, including all submissions of the parties, all evidence admitted
|
|
70
|
+
into the record of the proceedings, all transcripts or other recordings
|
|
71
|
+
of hearings and all orders, decisions and awards of the arbitral
|
|
72
|
+
tribunal, subject only to the arbitral tribunal’s powers to take such
|
|
73
|
+
measures as may be necessary to safeguard the integrity of the arbitral
|
|
74
|
+
process pursuant to Articles 18, 33, 41 and 42 of the Rules; and (3)
|
|
75
|
+
Article 26(6) of the Rules shall not apply.
|
|
76
|
+
. Human Rights Laws. The Software shall not be used by any person or
|
|
77
|
+
entity for any systems, activities, or other uses that violate any Human
|
|
78
|
+
Rights Laws. "Human Rights Laws" means any applicable laws,
|
|
79
|
+
regulations, or rules (collectively, "Laws") that protect human,
|
|
80
|
+
civil, labor, privacy, political, environmental, security, economic, due
|
|
81
|
+
process, or similar rights; provided, however, that such Laws are
|
|
82
|
+
consistent and not in conflict with Human Rights Principles (a dispute
|
|
83
|
+
over the consistency or a conflict between Laws and Human Rights
|
|
84
|
+
Principles shall be determined by arbitration as stated above). Where
|
|
85
|
+
the Human Rights Laws of more than one jurisdiction are applicable or in
|
|
86
|
+
conflict with respect to the use of the Software, the Human Rights Laws
|
|
87
|
+
that are most protective of the individuals or groups harmed shall
|
|
88
|
+
apply.
|
|
89
|
+
. Indemnity. Licensee shall hold harmless and indemnify Licensor (and
|
|
90
|
+
any other contributor) against all losses, damages, liabilities,
|
|
91
|
+
deficiencies, claims, actions, judgments, settlements, interest, awards,
|
|
92
|
+
penalties, fines, costs, or expenses of whatever kind, including
|
|
93
|
+
Licensor’s reasonable attorneys’ fees, arising out of or relating to
|
|
94
|
+
Licensee’s use of the Software in violation of Human Rights Laws or
|
|
95
|
+
Human Rights Principles.
|
|
96
|
+
* Failure to Comply. Any failure of Licensee to act according to the
|
|
97
|
+
terms and conditions of this License is both a breach of the License and
|
|
98
|
+
an infringement of the intellectual property rights of the Licensor
|
|
99
|
+
(subject to exceptions under Laws, e.g., fair use). In the event of a
|
|
100
|
+
breach or infringement, the terms and conditions of this License may be
|
|
101
|
+
enforced by Licensor under the Laws of any jurisdiction to which
|
|
102
|
+
Licensee is subject. Licensee also agrees that the Licensor may enforce
|
|
103
|
+
the terms and conditions of this License against Licensee through
|
|
104
|
+
specific performance (or similar remedy under Laws) to the extent
|
|
105
|
+
permitted by Laws. For clarity, except in the event of a breach of this
|
|
106
|
+
License, infringement, or as otherwise stated in this License, Licensor
|
|
107
|
+
may not terminate this License with Licensee.
|
|
108
|
+
* Enforceability and Interpretation. If any term or provision of this
|
|
109
|
+
License is determined to be invalid, illegal, or unenforceable by a
|
|
110
|
+
court of competent jurisdiction, then such invalidity, illegality, or
|
|
111
|
+
unenforceability shall not affect any other term or provision of this
|
|
112
|
+
License or invalidate or render unenforceable such term or provision in
|
|
113
|
+
any other jurisdiction; provided, however, subject to a court
|
|
114
|
+
modification pursuant to the immediately following sentence, if any term
|
|
115
|
+
or provision of this License pertaining to Human Rights Laws or Human
|
|
116
|
+
Rights Principles is deemed invalid, illegal, or unenforceable against
|
|
117
|
+
Licensee by a court of competent jurisdiction, all rights in the
|
|
118
|
+
Software granted to Licensee shall be deemed null and void as between
|
|
119
|
+
Licensor and Licensee. Upon a determination that any term or provision
|
|
120
|
+
is invalid, illegal, or unenforceable, to the extent permitted by Laws,
|
|
121
|
+
the court may modify this License to affect the original purpose that
|
|
122
|
+
the Software be used in compliance with Human Rights Principles and
|
|
123
|
+
Human Rights Laws as closely as possible. The language in this License
|
|
124
|
+
shall be interpreted as to its fair meaning and not strictly for or
|
|
125
|
+
against any party.
|
|
126
|
+
* Disclaimer. TO THE FULL EXTENT ALLOWED BY LAW, THIS SOFTWARE COMES
|
|
127
|
+
"AS IS," WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED, AND LICENSOR AND
|
|
128
|
+
ANY OTHER CONTRIBUTOR SHALL NOT BE LIABLE TO ANYONE FOR ANY DAMAGES OR
|
|
129
|
+
OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE
|
|
130
|
+
OR THIS LICENSE, UNDER ANY KIND OF LEGAL CLAIM.
|
|
131
|
+
|
|
132
|
+
This Hippocratic License is an link:https://ethicalsource.dev[Ethical Source license] and is offered
|
|
133
|
+
for use by licensors and licensees at their own risk, on an "AS IS" basis, and with no warranties
|
|
134
|
+
express or implied, to the maximum extent permitted by Laws.
|
data/README.adoc
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
:toc: macro
|
|
2
|
+
:toclevels: 5
|
|
3
|
+
:figure-caption!:
|
|
4
|
+
|
|
5
|
+
:command_pattern_link: link:https://alchemists.io/articles/command_pattern[Command Pattern]
|
|
6
|
+
:core_link: link:https://alchemists.io/projects/core[Core]
|
|
7
|
+
:liquid_link: link:https://shopify.github.io/liquid/[Liquid]
|
|
8
|
+
:structs_link: link:https://alchemists.io/articles/ruby_structs[Structs]
|
|
9
|
+
|
|
10
|
+
= Superfluid
|
|
11
|
+
|
|
12
|
+
toc::[]
|
|
13
|
+
|
|
14
|
+
== Features
|
|
15
|
+
|
|
16
|
+
* Simple renderer initialization.
|
|
17
|
+
* Powerful, and customizable, default environment with a cleaner Object API.
|
|
18
|
+
* Ability to register filters as commands (see {command_pattern_link} for details).
|
|
19
|
+
|
|
20
|
+
== Requirements
|
|
21
|
+
|
|
22
|
+
. link:https://www.ruby-lang.org[Ruby].
|
|
23
|
+
. {liquid_link}.
|
|
24
|
+
|
|
25
|
+
== Setup
|
|
26
|
+
|
|
27
|
+
To install _with_ security, run:
|
|
28
|
+
|
|
29
|
+
[source,bash]
|
|
30
|
+
----
|
|
31
|
+
# 💡 Skip this line if you already have the public certificate installed.
|
|
32
|
+
gem cert --add <(curl --compressed --location https://alchemists.io/gems.pem)
|
|
33
|
+
gem install superfluid --trust-policy HighSecurity
|
|
34
|
+
----
|
|
35
|
+
|
|
36
|
+
To install _without_ security, run:
|
|
37
|
+
|
|
38
|
+
[source,bash]
|
|
39
|
+
----
|
|
40
|
+
gem install superfluid
|
|
41
|
+
----
|
|
42
|
+
|
|
43
|
+
You can also add the gem directly to your project:
|
|
44
|
+
|
|
45
|
+
[source,bash]
|
|
46
|
+
----
|
|
47
|
+
bundle add superfluid
|
|
48
|
+
----
|
|
49
|
+
|
|
50
|
+
Once the gem is installed, you only need to require it:
|
|
51
|
+
|
|
52
|
+
[source,ruby]
|
|
53
|
+
----
|
|
54
|
+
require "superfluid"
|
|
55
|
+
----
|
|
56
|
+
|
|
57
|
+
== Usage
|
|
58
|
+
|
|
59
|
+
The quickest way to get started is to create a new instance:
|
|
60
|
+
|
|
61
|
+
[source,ruby]
|
|
62
|
+
----
|
|
63
|
+
renderer = Superfluid.new
|
|
64
|
+
----
|
|
65
|
+
|
|
66
|
+
Now you can render your template and associated data:
|
|
67
|
+
|
|
68
|
+
[source,ruby]
|
|
69
|
+
----
|
|
70
|
+
renderer.call "Wow, that was {{ value }}!", "value" => "quick"
|
|
71
|
+
----
|
|
72
|
+
|
|
73
|
+
The above will yield the following output:
|
|
74
|
+
|
|
75
|
+
----
|
|
76
|
+
"Wow, that was quick!"
|
|
77
|
+
----
|
|
78
|
+
|
|
79
|
+
=== Environments
|
|
80
|
+
|
|
81
|
+
To build the default environment, use:
|
|
82
|
+
|
|
83
|
+
[source,ruby]
|
|
84
|
+
----
|
|
85
|
+
environment = Superfluid.build
|
|
86
|
+
----
|
|
87
|
+
|
|
88
|
+
You can also build the default environment directly (which is what the above does:
|
|
89
|
+
|
|
90
|
+
[source,ruby]
|
|
91
|
+
----
|
|
92
|
+
environment = Superfluid::Environment.for
|
|
93
|
+
----
|
|
94
|
+
|
|
95
|
+
Both of the above produce a _frozen_ environment (including frozen tags). If you need an environment that isn't frozen (tags included), use:
|
|
96
|
+
|
|
97
|
+
[source,ruby]
|
|
98
|
+
----
|
|
99
|
+
environment = Superfluid::Environment.new
|
|
100
|
+
----
|
|
101
|
+
|
|
102
|
+
You can, later, freeze the environment once you've registered any/all filters and tags as follows:
|
|
103
|
+
|
|
104
|
+
[source,ruby]
|
|
105
|
+
----
|
|
106
|
+
environment.freeze
|
|
107
|
+
----
|
|
108
|
+
|
|
109
|
+
Finally, the environment is an instance of a `Struct` which means you can leverage all of the power of {structs_link}.
|
|
110
|
+
|
|
111
|
+
When using `Superfluid.build`, `Superfluid::Environment.for`, or `Superfluid::Environment.new`, you can customize behavior via the following keyword arguments:
|
|
112
|
+
|
|
113
|
+
* `default_resource_limits`: The resource limits you want to set. Default: `{}`.
|
|
114
|
+
* `error_mode`: The error mode. Default: `:strict`.
|
|
115
|
+
* `exception_renderer`: The exception renderer. Default: `Core::Identity` (see {core_link} for details).
|
|
116
|
+
* `file_system`: The file system. Default: `Superfluid::Systems::Memory.new`.
|
|
117
|
+
* `filter_registry`: The filter registry. Default: `Superfluid::Registries::Filter.new`.
|
|
118
|
+
* `tags`: The default tags. Default: `Liquid::Tags::STANDARD_TAGS`.
|
|
119
|
+
|
|
120
|
+
=== Filters
|
|
121
|
+
|
|
122
|
+
By default, {liquid_link} only allows registration of filters as instance methods on a namespace (`Module`). Unfortunately, this leads to terrible design, bad practices, and unnecessarily hard to test objects. You can still register namespaces but this gem encourages the use of commands (see {command_pattern_link}) which allows you tap into Functional Programming by using procs, lambdas, and any object that responds to `#call`. This allow you to build -- and reuse -- more powerful filters.
|
|
123
|
+
|
|
124
|
+
[source,ruby]
|
|
125
|
+
----
|
|
126
|
+
primary = Module.new { def one = 1 }
|
|
127
|
+
|
|
128
|
+
secondary = Module.new do
|
|
129
|
+
def two = 2
|
|
130
|
+
|
|
131
|
+
def three = 3
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
function_a = proc { "four" }
|
|
135
|
+
function_b = proc { "five" }
|
|
136
|
+
|
|
137
|
+
class Sayer
|
|
138
|
+
def initialize prefix = "Demo"
|
|
139
|
+
@prefix = prefix
|
|
140
|
+
end
|
|
141
|
+
|
|
142
|
+
def call(message) = "#{prefix}: #{message}"
|
|
143
|
+
|
|
144
|
+
private
|
|
145
|
+
|
|
146
|
+
attr_reader :prefix
|
|
147
|
+
end
|
|
148
|
+
|
|
149
|
+
environment = Superfluid.build do |instance|
|
|
150
|
+
instance.register_filter(primary)
|
|
151
|
+
.register_filter(primary, four: function_a, sayer: Sayer.new)
|
|
152
|
+
.register_filters(primary, secondary, four: function_a, five: function_b)
|
|
153
|
+
end
|
|
154
|
+
----
|
|
155
|
+
|
|
156
|
+
While the above is contrived, duplicate keys are ignored. Use of `#register_filter` is for backwards compatibility with {liquid_link}. All you need is `#register_filters` to register both namespaces (modules) and commands.
|
|
157
|
+
|
|
158
|
+
=== Tags
|
|
159
|
+
|
|
160
|
+
As per {liquid_link} documentation, tags only need to inherit from `Liquid::Tag` and be initialized with `tag_name`, `factor`, and `tokens`. Then you can implement a render method which accepts a `context`. Example:
|
|
161
|
+
|
|
162
|
+
[source,ruby]
|
|
163
|
+
----
|
|
164
|
+
class Multiply < Liquid::Tag
|
|
165
|
+
def initialize(tag_name, factor, tokens)
|
|
166
|
+
super
|
|
167
|
+
@factor = factor.to_f
|
|
168
|
+
end
|
|
169
|
+
|
|
170
|
+
def render(context) = (factor * context["multiply_by"].to_f).to_s
|
|
171
|
+
|
|
172
|
+
private
|
|
173
|
+
|
|
174
|
+
attr_reader :factor
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
environment = Superfluid.build do |instance|
|
|
178
|
+
instance.register_tag(:multiply, Multiply)
|
|
179
|
+
end
|
|
180
|
+
----
|
|
181
|
+
|
|
182
|
+
== Development
|
|
183
|
+
|
|
184
|
+
To contribute, run:
|
|
185
|
+
|
|
186
|
+
[source,bash]
|
|
187
|
+
----
|
|
188
|
+
git clone https://github.com/bkuhlmann/superfluid
|
|
189
|
+
cd superfluid
|
|
190
|
+
bin/setup
|
|
191
|
+
----
|
|
192
|
+
|
|
193
|
+
You can also use the IRB console for direct access to all objects:
|
|
194
|
+
|
|
195
|
+
[source,bash]
|
|
196
|
+
----
|
|
197
|
+
bin/console
|
|
198
|
+
----
|
|
199
|
+
|
|
200
|
+
== Tests
|
|
201
|
+
|
|
202
|
+
To test, run:
|
|
203
|
+
|
|
204
|
+
[source,bash]
|
|
205
|
+
----
|
|
206
|
+
bin/rake
|
|
207
|
+
----
|
|
208
|
+
|
|
209
|
+
== link:https://alchemists.io/policies/license[License]
|
|
210
|
+
|
|
211
|
+
== link:https://alchemists.io/policies/security[Security]
|
|
212
|
+
|
|
213
|
+
== link:https://alchemists.io/policies/code_of_conduct[Code of Conduct]
|
|
214
|
+
|
|
215
|
+
== link:https://alchemists.io/policies/contributions[Contributions]
|
|
216
|
+
|
|
217
|
+
== link:https://alchemists.io/policies/developer_certificate_of_origin[Developer Certificate of Origin]
|
|
218
|
+
|
|
219
|
+
== link:https://alchemists.io/projects/superfluid/versions[Versions]
|
|
220
|
+
|
|
221
|
+
== link:https://alchemists.io/community[Community]
|
|
222
|
+
|
|
223
|
+
== Credits
|
|
224
|
+
|
|
225
|
+
* Built with link:https://alchemists.io/projects/gemsmith[Gemsmith].
|
|
226
|
+
* Engineered by link:https://alchemists.io/team/brooke_kuhlmann[Brooke Kuhlmann].
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "core"
|
|
4
|
+
|
|
5
|
+
module Superfluid
|
|
6
|
+
# The default Liquid environment.
|
|
7
|
+
Environment = Struct.new(
|
|
8
|
+
:default_resource_limits,
|
|
9
|
+
:error_mode,
|
|
10
|
+
:exception_renderer,
|
|
11
|
+
:file_system,
|
|
12
|
+
:filter_registry,
|
|
13
|
+
:tags
|
|
14
|
+
) do
|
|
15
|
+
def self.for(**)
|
|
16
|
+
instance = new(**)
|
|
17
|
+
|
|
18
|
+
yield instance if block_given?
|
|
19
|
+
|
|
20
|
+
instance.freeze
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def self.default
|
|
24
|
+
@default ||= new
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def initialize(**)
|
|
28
|
+
super
|
|
29
|
+
|
|
30
|
+
self[:default_resource_limits] ||= Core::EMPTY_HASH
|
|
31
|
+
self[:error_mode] ||= :strict
|
|
32
|
+
self[:exception_renderer] ||= Core::Identity
|
|
33
|
+
self[:file_system] ||= Systems::Memory.new
|
|
34
|
+
self[:filter_registry] ||= Registries::Filter.for mode: error_mode
|
|
35
|
+
self[:tags] ||= Liquid::Tags::STANDARD_TAGS.dup
|
|
36
|
+
|
|
37
|
+
@filters_cache = {}
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def create_filter_registry _context = nil,
|
|
41
|
+
filters = Core::EMPTY_ARRAY,
|
|
42
|
+
namespace: Liquid::StandardFilters
|
|
43
|
+
return filter_registry if filters.empty?
|
|
44
|
+
|
|
45
|
+
filter_registry.add(namespace, *filters)
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
alias_method :create_strainer, :create_filter_registry
|
|
49
|
+
|
|
50
|
+
def filter_names = filter_registry.names
|
|
51
|
+
|
|
52
|
+
alias_method :filter_method_names, :filter_names
|
|
53
|
+
|
|
54
|
+
def register_filter(namespace, **)
|
|
55
|
+
filters_cache.clear
|
|
56
|
+
filter_registry.add(namespace, **)
|
|
57
|
+
self
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def register_filters(*, **)
|
|
61
|
+
filters_cache.clear
|
|
62
|
+
filter_registry.add(*, **)
|
|
63
|
+
self
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def register_tag name, klass
|
|
67
|
+
tags[name.to_s] = klass
|
|
68
|
+
self
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def find_tag(name) = tags[name.to_s]
|
|
72
|
+
|
|
73
|
+
alias_method :tag_for_name, :find_tag
|
|
74
|
+
|
|
75
|
+
def freeze
|
|
76
|
+
tags.freeze
|
|
77
|
+
super
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
private
|
|
81
|
+
|
|
82
|
+
attr_reader :filters_cache
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Superfluid
|
|
4
|
+
module Registries
|
|
5
|
+
# A filter registry.
|
|
6
|
+
Filter = Data.define :mode, :commands do
|
|
7
|
+
def self.for(namespace = Liquid::StandardFilters, mode: :strict) = new(mode:).add namespace
|
|
8
|
+
|
|
9
|
+
def initialize mode: :strict, commands: {}
|
|
10
|
+
super
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def add(*, **)
|
|
14
|
+
add_namespaces(*)
|
|
15
|
+
add_commands(**)
|
|
16
|
+
self
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def call(name, *)
|
|
20
|
+
dispatch(name, *)
|
|
21
|
+
rescue ArgumentError => error
|
|
22
|
+
raise Liquid::ArgumentError, error.message, error.backtrace
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
alias_method :invoke, :call
|
|
26
|
+
|
|
27
|
+
def clear
|
|
28
|
+
commands.clear
|
|
29
|
+
self
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def names = commands.keys
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
|
|
36
|
+
def add_namespaces(*all) = all.each { add_methods it }
|
|
37
|
+
|
|
38
|
+
def add_methods namespace
|
|
39
|
+
shadow = Class.new.include(namespace).new
|
|
40
|
+
|
|
41
|
+
all = namespace.instance_methods
|
|
42
|
+
.each
|
|
43
|
+
.with_object({}) do |method, all|
|
|
44
|
+
name = method.name
|
|
45
|
+
all[name] = shadow.method name
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
add_commands(**all)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def add_commands(**all) = all.each { |name, command| add_command name, command }
|
|
52
|
+
|
|
53
|
+
# :reek:ManualDispatch
|
|
54
|
+
def add_command name, function
|
|
55
|
+
key = name.to_s
|
|
56
|
+
|
|
57
|
+
return if commands.key? key
|
|
58
|
+
|
|
59
|
+
case function
|
|
60
|
+
in Proc | Method | Object if function.respond_to? :call then commands[key] = function
|
|
61
|
+
else fail Liquid::ArgumentError, "Filter must be callable."
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
def dispatch name, *positionals
|
|
66
|
+
key = name.to_s
|
|
67
|
+
|
|
68
|
+
if commands.key? key
|
|
69
|
+
commands[key].call(*positionals)
|
|
70
|
+
elsif mode == :strict
|
|
71
|
+
fail Liquid::UndefinedFilter, "Undefined filter: #{name}."
|
|
72
|
+
else
|
|
73
|
+
positionals.first
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Superfluid
|
|
4
|
+
# Renders template and associated data.
|
|
5
|
+
class Renderer
|
|
6
|
+
def initialize environment: Environment.for, parser: Liquid::Template
|
|
7
|
+
@environment = environment
|
|
8
|
+
@parser = parser
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def call(template, data) = parser.parse(template, environment:).render data
|
|
12
|
+
|
|
13
|
+
private
|
|
14
|
+
|
|
15
|
+
attr_reader :environment, :parser
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Superfluid
|
|
4
|
+
module Systems
|
|
5
|
+
# An in-memory file system for storing templates.
|
|
6
|
+
Memory = Data.define :templates do
|
|
7
|
+
def initialize templates: {}
|
|
8
|
+
super
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def register name, content
|
|
12
|
+
templates[name.to_s] = content
|
|
13
|
+
self
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def read_template_file name
|
|
17
|
+
templates.fetch(name) { fail Liquid::FileSystemError, "Template not found: #{name}." }
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Superfluid
|
|
4
|
+
module Tags
|
|
5
|
+
# Define custom templates within the context of the current template.
|
|
6
|
+
class Template < Liquid::Block
|
|
7
|
+
NAME_PATTERN = %r(\A[0-9a-zA-Z_/]+\z)
|
|
8
|
+
|
|
9
|
+
def initialize tag_name, markup, options
|
|
10
|
+
super
|
|
11
|
+
|
|
12
|
+
@name = markup.strip
|
|
13
|
+
@content = +""
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def parse tokens
|
|
17
|
+
content.clear
|
|
18
|
+
|
|
19
|
+
while (token = tokens.shift)
|
|
20
|
+
break if token.strip == "{% endtemplate %}"
|
|
21
|
+
|
|
22
|
+
content << token
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def render context
|
|
27
|
+
unless NAME_PATTERN.match? name
|
|
28
|
+
return "Liquid error: Invalid template name #{name.inspect} - template names " \
|
|
29
|
+
"must contain only letters, numbers, underscores, and forward slashes."
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
context.registers[:file_system].register name, content.strip
|
|
33
|
+
""
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
private
|
|
37
|
+
|
|
38
|
+
attr_reader :name, :content
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
data/lib/superfluid.rb
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "liquid"
|
|
4
|
+
require "zeitwerk"
|
|
5
|
+
|
|
6
|
+
Zeitwerk::Loader.new.then do |loader|
|
|
7
|
+
loader.tag = File.basename __FILE__, ".rb"
|
|
8
|
+
loader.push_dir __dir__
|
|
9
|
+
loader.setup
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Main namespace.
|
|
13
|
+
module Superfluid
|
|
14
|
+
def self.loader registry = Zeitwerk::Registry
|
|
15
|
+
@loader ||= registry.loaders.each.find do |loader|
|
|
16
|
+
loader.tag == File.basename(__FILE__, ".rb")
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.build(**) = Environment.for(**) { yield it if block_given? }
|
|
21
|
+
|
|
22
|
+
def self.new(**) = Renderer.new(**)
|
|
23
|
+
end
|
data/superfluid.gemspec
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
Gem::Specification.new do |spec|
|
|
4
|
+
spec.name = "superfluid"
|
|
5
|
+
spec.version = "0.0.0"
|
|
6
|
+
spec.authors = ["Brooke Kuhlmann"]
|
|
7
|
+
spec.email = ["brooke@alchemists.io"]
|
|
8
|
+
spec.homepage = "https://alchemists.io/projects/superfluid"
|
|
9
|
+
spec.summary = "Enhances Liquid with object composition and functional design."
|
|
10
|
+
spec.license = "Hippocratic-2.1"
|
|
11
|
+
|
|
12
|
+
spec.metadata = {
|
|
13
|
+
"bug_tracker_uri" => "https://github.com/bkuhlmann/superfluid/issues",
|
|
14
|
+
"changelog_uri" => "https://alchemists.io/projects/superfluid/versions",
|
|
15
|
+
"homepage_uri" => "https://alchemists.io/projects/superfluid",
|
|
16
|
+
"funding_uri" => "https://github.com/sponsors/bkuhlmann",
|
|
17
|
+
"label" => "Superfluid",
|
|
18
|
+
"rubygems_mfa_required" => "true",
|
|
19
|
+
"source_code_uri" => "https://github.com/bkuhlmann/superfluid"
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
spec.signing_key = Gem.default_key_path
|
|
23
|
+
spec.cert_chain = [Gem.default_cert_path]
|
|
24
|
+
|
|
25
|
+
spec.required_ruby_version = ">= 4.0"
|
|
26
|
+
|
|
27
|
+
spec.add_dependency "base64", "~> 0.3"
|
|
28
|
+
spec.add_dependency "core", "~> 3.3"
|
|
29
|
+
spec.add_dependency "liquid", "~> 5.13"
|
|
30
|
+
spec.add_dependency "refinements", "~> 14.0"
|
|
31
|
+
spec.add_dependency "zeitwerk", "~> 2.8"
|
|
32
|
+
|
|
33
|
+
spec.extra_rdoc_files = Dir["README*", "LICENSE*"]
|
|
34
|
+
spec.files = Dir["*.gemspec", "lib/**/*"]
|
|
35
|
+
end
|
data.tar.gz.sig
ADDED
|
Binary file
|
metadata
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: superfluid
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Brooke Kuhlmann
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain:
|
|
10
|
+
- |
|
|
11
|
+
-----BEGIN CERTIFICATE-----
|
|
12
|
+
MIIENjCCAp6gAwIBAgIBAzANBgkqhkiG9w0BAQsFADBBMQ8wDQYDVQQDDAZicm9v
|
|
13
|
+
a2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQBGRYC
|
|
14
|
+
aW8wHhcNMjYwMzI1MTI0OTEyWhcNMjcwMzI1MTI0OTEyWjBBMQ8wDQYDVQQDDAZi
|
|
15
|
+
cm9va2UxGjAYBgoJkiaJk/IsZAEZFgphbGNoZW1pc3RzMRIwEAYKCZImiZPyLGQB
|
|
16
|
+
GRYCaW8wggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCro8tj5/E1Hg88
|
|
17
|
+
f4qfiwPVd2zJQHvdYt4GHVvuHRRgx4HGhJuNp+4BId08RBn7V6V1MW6MY3kezRBs
|
|
18
|
+
M+7QOQ4b1xNLTvY7FYQB1wGK5a4x7TTokDrPYQxDB2jmsdDYCzVbIMrAvUfcecRi
|
|
19
|
+
khyGZCdByiiCl4fKv77P12tTT+NfsvXkLt/AYCGwjOUyGKTQ01Z6eC09T27GayPH
|
|
20
|
+
QQvIkakyFgcJtzSyGzs8bzK5q9u7wQ12MNTjJoXzW69lqp0oNvDylu81EiSUb5S6
|
|
21
|
+
QzzPxZBiRB1sgtbt1gUbVI262ZDq1gR+HxPFmp+Cgt7ZLIJZAtesQvtcMzseXpfn
|
|
22
|
+
hpmm0Sw22KGhRAy/mqHBRhDl5HqS1SJp2Ko3lcnpXeFResp0HNlt8NSu13vhC08j
|
|
23
|
+
GUHU9MyIXbFOsnp3K3ADrAVjPWop8EZkmUR3MV/CUm00w2cZHCSGiXl1KMpiVKvk
|
|
24
|
+
Ywr1gd2ZME4QLSo+EXUtLxDUa/W3xnBS8dBOuMMz02FPWYr3PN8CAwEAAaM5MDcw
|
|
25
|
+
CQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0OBBYEFAFgmv0tYMZnItuPycSM
|
|
26
|
+
F5wykJEVMA0GCSqGSIb3DQEBCwUAA4IBgQAG+ykjp+DIXSybGEtX+/ve974mYfN6
|
|
27
|
+
8U7qcVfRM+qDSOZ+97iu30qUTbVAKIHlHCDKRn3SgOffDUB5VU2MsJBh/3TPKWBZ
|
|
28
|
+
anB/uzMcwOfru+qyA3b7ZFqZzRLWmR5FtPObFxc0gYMT3YvLNHk2Nb9Vjq/PoiGG
|
|
29
|
+
e75PXweDOokwDA5m1gMOz1rdp/dlGMXkSFQg94PPVyUKXgO4VzWTgePSDxOIL+v6
|
|
30
|
+
+OWV6AaEH9BaqxnmdA5ubi0L7bhl0gbN92FxpNO3kpTjww8kme856a+wCK3qyM5w
|
|
31
|
+
7ZLbUexynDN0Au8eSpT2Bf6ztGmB1S9ffzDJsGX1/lkpMIB51e48Xe2+gzzOgemk
|
|
32
|
+
CdZaGupj6WkarnT8kh/cPtyA5ax4rGX6GOS8meGxzkv8Uy0JSEOYAp6wLfIisYZp
|
|
33
|
+
IJBIXIOkwKKJ0eB5YHrUSJxzpP4LlcIg/eTftaXmJdYjy+2VRrCZYDjfguyLmMjR
|
|
34
|
+
KR9w4/Fjvqy87kCHmxMWa6IL2Vzt1Clm2cA=
|
|
35
|
+
-----END CERTIFICATE-----
|
|
36
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
37
|
+
dependencies:
|
|
38
|
+
- !ruby/object:Gem::Dependency
|
|
39
|
+
name: base64
|
|
40
|
+
requirement: !ruby/object:Gem::Requirement
|
|
41
|
+
requirements:
|
|
42
|
+
- - "~>"
|
|
43
|
+
- !ruby/object:Gem::Version
|
|
44
|
+
version: '0.3'
|
|
45
|
+
type: :runtime
|
|
46
|
+
prerelease: false
|
|
47
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
48
|
+
requirements:
|
|
49
|
+
- - "~>"
|
|
50
|
+
- !ruby/object:Gem::Version
|
|
51
|
+
version: '0.3'
|
|
52
|
+
- !ruby/object:Gem::Dependency
|
|
53
|
+
name: core
|
|
54
|
+
requirement: !ruby/object:Gem::Requirement
|
|
55
|
+
requirements:
|
|
56
|
+
- - "~>"
|
|
57
|
+
- !ruby/object:Gem::Version
|
|
58
|
+
version: '3.3'
|
|
59
|
+
type: :runtime
|
|
60
|
+
prerelease: false
|
|
61
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
62
|
+
requirements:
|
|
63
|
+
- - "~>"
|
|
64
|
+
- !ruby/object:Gem::Version
|
|
65
|
+
version: '3.3'
|
|
66
|
+
- !ruby/object:Gem::Dependency
|
|
67
|
+
name: liquid
|
|
68
|
+
requirement: !ruby/object:Gem::Requirement
|
|
69
|
+
requirements:
|
|
70
|
+
- - "~>"
|
|
71
|
+
- !ruby/object:Gem::Version
|
|
72
|
+
version: '5.13'
|
|
73
|
+
type: :runtime
|
|
74
|
+
prerelease: false
|
|
75
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
76
|
+
requirements:
|
|
77
|
+
- - "~>"
|
|
78
|
+
- !ruby/object:Gem::Version
|
|
79
|
+
version: '5.13'
|
|
80
|
+
- !ruby/object:Gem::Dependency
|
|
81
|
+
name: refinements
|
|
82
|
+
requirement: !ruby/object:Gem::Requirement
|
|
83
|
+
requirements:
|
|
84
|
+
- - "~>"
|
|
85
|
+
- !ruby/object:Gem::Version
|
|
86
|
+
version: '14.0'
|
|
87
|
+
type: :runtime
|
|
88
|
+
prerelease: false
|
|
89
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
90
|
+
requirements:
|
|
91
|
+
- - "~>"
|
|
92
|
+
- !ruby/object:Gem::Version
|
|
93
|
+
version: '14.0'
|
|
94
|
+
- !ruby/object:Gem::Dependency
|
|
95
|
+
name: zeitwerk
|
|
96
|
+
requirement: !ruby/object:Gem::Requirement
|
|
97
|
+
requirements:
|
|
98
|
+
- - "~>"
|
|
99
|
+
- !ruby/object:Gem::Version
|
|
100
|
+
version: '2.8'
|
|
101
|
+
type: :runtime
|
|
102
|
+
prerelease: false
|
|
103
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
104
|
+
requirements:
|
|
105
|
+
- - "~>"
|
|
106
|
+
- !ruby/object:Gem::Version
|
|
107
|
+
version: '2.8'
|
|
108
|
+
email:
|
|
109
|
+
- brooke@alchemists.io
|
|
110
|
+
executables: []
|
|
111
|
+
extensions: []
|
|
112
|
+
extra_rdoc_files:
|
|
113
|
+
- LICENSE.adoc
|
|
114
|
+
- README.adoc
|
|
115
|
+
files:
|
|
116
|
+
- LICENSE.adoc
|
|
117
|
+
- README.adoc
|
|
118
|
+
- lib/superfluid.rb
|
|
119
|
+
- lib/superfluid/environment.rb
|
|
120
|
+
- lib/superfluid/registries/filter.rb
|
|
121
|
+
- lib/superfluid/renderer.rb
|
|
122
|
+
- lib/superfluid/systems/memory.rb
|
|
123
|
+
- lib/superfluid/tags/template.rb
|
|
124
|
+
- superfluid.gemspec
|
|
125
|
+
homepage: https://alchemists.io/projects/superfluid
|
|
126
|
+
licenses:
|
|
127
|
+
- Hippocratic-2.1
|
|
128
|
+
metadata:
|
|
129
|
+
bug_tracker_uri: https://github.com/bkuhlmann/superfluid/issues
|
|
130
|
+
changelog_uri: https://alchemists.io/projects/superfluid/versions
|
|
131
|
+
homepage_uri: https://alchemists.io/projects/superfluid
|
|
132
|
+
funding_uri: https://github.com/sponsors/bkuhlmann
|
|
133
|
+
label: Superfluid
|
|
134
|
+
rubygems_mfa_required: 'true'
|
|
135
|
+
source_code_uri: https://github.com/bkuhlmann/superfluid
|
|
136
|
+
rdoc_options: []
|
|
137
|
+
require_paths:
|
|
138
|
+
- lib
|
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
140
|
+
requirements:
|
|
141
|
+
- - ">="
|
|
142
|
+
- !ruby/object:Gem::Version
|
|
143
|
+
version: '4.0'
|
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
145
|
+
requirements:
|
|
146
|
+
- - ">="
|
|
147
|
+
- !ruby/object:Gem::Version
|
|
148
|
+
version: '0'
|
|
149
|
+
requirements: []
|
|
150
|
+
rubygems_version: 4.0.19
|
|
151
|
+
specification_version: 4
|
|
152
|
+
summary: Enhances Liquid with object composition and functional design.
|
|
153
|
+
test_files: []
|
metadata.gz.sig
ADDED
|
Binary file
|