liquidum 1.4.4 → 1.4.5

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: 184a362b7a5a6c5344164fe79127d14f872765546e8c26e80d8d44fd70df4dd5
4
- data.tar.gz: 5d3cf537732e80265b97ecf0de056d335cae2130e824edf0402dbc747bc4aea6
3
+ metadata.gz: 8db3d8bb7377ff3aa91290a354be830933a670195a0b6e72019e1a07b16576f5
4
+ data.tar.gz: 54ee59d79bef0cd63f0dd65fef80063677cca5e2faa8e1e897f6b1dc96a160c3
5
5
  SHA512:
6
- metadata.gz: 05cbe8851ecaee7d729460f73c3ac082369967f393a766eb4caa30f3bca3d76c93f3946cd28e58bd5ce48ed0686a0c0f2c5727403e362626cf4f444e31dbf30f
7
- data.tar.gz: 4f0eab6bd9f6dd0248a8727b09eea109c9cd5fa148d5d242ea317bdec3112517cda46d3df09e1d4d488ce88af3b55f48540d505c3c67954529866957722970e2
6
+ metadata.gz: 05aecfca616268ef62926c597c047be0c9eb81db1e87cb652745c331a0753dba965aa3662db32000813b7c6fbcf2d59e29310982f5503e8aa8551b39caee23bf
7
+ data.tar.gz: 2a942dc1585ddc53962295e838f895e240157c6bd128c6d5284e7076010841f1e1c03e5c7843577c71053b5674e305cf8b0ca54a5c939e139809b4d16019a4cc
data/.shipkit.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  ---
2
- previous_version: 1.4.3
2
+ previous_version: 1.4.4
data/README.md CHANGED
@@ -93,7 +93,7 @@ leaves: the same items (`where`, `sort`) or one of them (`first`):
93
93
 
94
94
  ```
95
95
  GET /liquidum/filters
96
- {"filters": [{"name": "where", "label": "Those whose property has the value", "arguments": [{"name": "property", "kind": "property"}, {"name": "value", "kind": "value"}], "keeps": "collection"}, …]}
96
+ {"filters": [{"name": "where", "label": "Those whose property has the value", "arguments": [{"name": "property", "kind": "property", "optional": false}, {"name": "value", "kind": "value", "optional": true}], "keeps": "collection"}, …]}
97
97
  ```
98
98
 
99
99
  An argument's kind is `property`, `value` (of the property before it),
@@ -101,7 +101,12 @@ An argument's kind is `property`, `value` (of the property before it),
101
101
  `time.formats` as its `values`), `key` (a translation's), `locale` (with the
102
102
  app's locales as its `values`, as `localize: 'nl'` has it) or `any`. An app describes its own
103
103
  filters with `Liquidum::Filters.describe :money, arguments: [[:currency]]`, and
104
- labels them as `liquidum.filters.<name>`.
104
+ labels them as `liquidum.filters.<name>`. Whether an argument is `optional`
105
+ comes from the filter's method (`def pad(input, width, char = " ")`), and a
106
+ filter that isn't described has its method's arguments; named ones (an
107
+ `options` hash) aren't listed. `keeps` says what a filter on a collection
108
+ leaves: the same items (`collection`), one of them (`item`) or a list of the
109
+ property `map` names (`property`).
105
110
 
106
111
  And for what can follow `{%`, the tags, each with how it's written and whether
107
112
  it's a block, which an editor writes with its closing tag:
@@ -17,8 +17,11 @@ module Liquidum
17
17
  # being the argument before), a date format ("format", strftime's, with the
18
18
  # app's date.formats and time.formats as its `values`), a
19
19
  # translation's key ("key"), a locale ("locale", with the app's locales as
20
- # its `values`), or anything ("any"). `keeps` is "collection" (the same items),
21
- # "item" (one of them) or nil. Labels are liquidum.filters.<name>, in the
20
+ # its `values`), or anything ("any"). `optional` says whether it can be left
21
+ # out, from the filter's method (truncate: 20 or truncate: 20, "…"), when its
22
+ # method says. `keeps` is "collection" (the same items), "item" (one of
23
+ # them), "property" (a list of the property its first argument names, as
24
+ # map gives) or nil. An undescribed filter has its method's arguments. Labels are liquidum.filters.<name>, in the
22
25
  # current locale; a host describes its own filters the same way:
23
26
  #
24
27
  # Liquidum::Filters.describe :money, arguments: [], keeps: nil
@@ -36,22 +39,39 @@ module Liquidum
36
39
  def all
37
40
  names = Liquid::Environment.default.strainer_template.filter_method_names.map(&:to_s).sort
38
41
  names.map do |name|
39
- description = DESCRIPTIONS.fetch(name, {arguments: [], keeps: nil})
42
+ description = DESCRIPTIONS[name]
43
+ parameters = parameters(name)
44
+ # Undescribed, its arguments are its method's, of any kind
45
+ arguments = description ? description[:arguments] : Array(parameters).map { |_, parameter| [parameter] }
40
46
  {
41
47
  name: name,
42
48
  label: I18n.t(name, scope: "liquidum.filters", default: name.humanize),
43
- arguments: description[:arguments].map { |argument, kind| argument(argument, kind || :any) },
44
- keeps: description[:keeps]&.to_s
49
+ arguments: arguments.each_with_index.map { |(argument, kind), index| argument(argument, kind || :any, parameters && parameters[index]) },
50
+ keeps: description&.dig(:keeps)&.to_s
45
51
  }.compact
46
52
  end
47
53
  end
48
54
 
49
- def argument(name, kind)
55
+ # A filter method's positional arguments after its input, [kind, name]
56
+ # with kind :req or :opt; named ones (t: name: …, an options hash) aren't.
57
+ # nil when it takes any number (*args) or isn't a method
58
+ def parameters(name)
59
+ parameters = Liquid::Environment.default.strainer_template.instance_method(name).parameters.drop(1)
60
+ return nil if parameters.any? { |kind, _| kind == :rest }
61
+
62
+ parameters.select { |kind, parameter| %i[req opt].include?(kind) && parameter != :options }
63
+ rescue NameError
64
+ nil
65
+ end
66
+
67
+ # `optional` from its method's parameter at its place, when known
68
+ def argument(name, kind, parameter = nil)
50
69
  values = case kind
51
70
  when :locale then I18n.available_locales.map { |locale| {value: locale.to_s, label: locale.to_s} }
52
71
  when :format then formats
53
72
  end
54
- {name: name.to_s, kind: kind.to_s, values: values}.compact
73
+ optional = parameter ? parameter.first == :opt : nil
74
+ {name: name.to_s, kind: kind.to_s, values: values, optional: optional}.compact
55
75
  end
56
76
 
57
77
  # The app's own date and time formats (date.formats, time.formats in the
@@ -74,9 +94,9 @@ module Liquidum
74
94
  value = [:value, :value]
75
95
  {
76
96
  # On collections
77
- "compact" => [[], :collection], "concat" => [[[:items]], :collection], "first" => [[], :item], "last" => [[], :item],
78
- "join" => [[[:separator]]], "map" => [[property]], "reverse" => [[], :collection], "size" => [[]],
79
- "sort" => [[property], :collection], "sort_natural" => [[property], :collection], "uniq" => [[], :collection],
97
+ "compact" => [[property], :collection], "concat" => [[[:items]], :collection], "first" => [[], :item], "last" => [[], :item],
98
+ "join" => [[[:separator]]], "map" => [[property], :property], "reverse" => [[], :collection], "size" => [[]],
99
+ "sort" => [[property], :collection], "sort_natural" => [[property], :collection], "uniq" => [[property], :collection],
80
100
  "where" => [[property, value], :collection], "reject" => [[property, value], :collection],
81
101
  "where_exp" => [[[:item], [:condition]], :collection], "find" => [[property, value], :item],
82
102
  "find_index" => [[property, value]], "has" => [[property, value]], "sum" => [[]], "min" => [[]], "max" => [[]],
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Liquidum
4
- VERSION = "1.4.4"
4
+ VERSION = "1.4.5"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: liquidum
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.4
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt