liquidum 1.4.3 → 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: 96d0ead154fefe58f08b4f87c9981cf141ae8b149a740bb02b30550fca32137c
4
- data.tar.gz: 2bfceaf7029f7a3d9675424197b90e8f54ce14820a0eecda3ef58a3d72c3826a
3
+ metadata.gz: 8db3d8bb7377ff3aa91290a354be830933a670195a0b6e72019e1a07b16576f5
4
+ data.tar.gz: 54ee59d79bef0cd63f0dd65fef80063677cca5e2faa8e1e897f6b1dc96a160c3
5
5
  SHA512:
6
- metadata.gz: 199d3b0735522241c505b79e095b257bf5013f417d4daa947ae94490d35dcf4adf5d6c852c1329225c29d8e4dbe29e58a2b5fdd0e4449451cd4397cd307cc521
7
- data.tar.gz: bdd601cb35ccee0d7cba791ddc2b7201399074cbb53e45564f7469442ea158c0f8f9f9cdc1cbc9fc37430a8baa2c643bb70a0c0c624083f4263aa2b6e86f635a
6
+ metadata.gz: 05aecfca616268ef62926c597c047be0c9eb81db1e87cb652745c331a0753dba965aa3662db32000813b7c6fbcf2d59e29310982f5503e8aa8551b39caee23bf
7
+ data.tar.gz: 2a942dc1585ddc53962295e838f895e240157c6bd128c6d5284e7076010841f1e1c03e5c7843577c71053b5674e305cf8b0ca54a5c939e139809b4d16019a4cc
data/.shipkit.yml CHANGED
@@ -1,2 +1,2 @@
1
1
  ---
2
- previous_version: 1.4.2
2
+ previous_version: 1.4.4
data/README.md CHANGED
@@ -93,13 +93,20 @@ 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),
100
- `format` (a date's), `key` (a translation's) or `any`. An app describes its own
100
+ `format` (a date's, strftime's, with the app's `date.formats` and
101
+ `time.formats` as its `values`), `key` (a translation's), `locale` (with the
102
+ app's locales as its `values`, as `localize: 'nl'` has it) or `any`. An app describes its own
101
103
  filters with `Liquidum::Filters.describe :money, arguments: [[:currency]]`, and
102
- 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`).
103
110
 
104
111
  And for what can follow `{%`, the tags, each with how it's written and whether
105
112
  it's a block, which an editor writes with its closing tag:
@@ -27,6 +27,10 @@ en:
27
27
  context:
28
28
  not_a_json_object: 'must be a JSON object, like {"name": "value"}'
29
29
  liquidum:
30
+ # The app's date.formats and time.formats, for a date filter's format (Liquidum::Filters)
31
+ formats:
32
+ date: "Date, %{name}"
33
+ time: "Time, %{name}"
30
34
  tags:
31
35
  assign: "Sets a variable"
32
36
  attach: "Attaches a file"
@@ -27,6 +27,10 @@ nl:
27
27
  context:
28
28
  not_a_json_object: 'moet een JSON-object zijn, zoals {"naam": "waarde"}'
29
29
  liquidum:
30
+ # The app's date.formats and time.formats, for a date filter's format (Liquidum::Filters)
31
+ formats:
32
+ date: "Datum, %{name}"
33
+ time: "Tijd, %{name}"
30
34
  tags:
31
35
  assign: "Zet een variabele"
32
36
  attach: "Voegt een bestand toe"
@@ -14,9 +14,14 @@ module Liquidum
14
14
  #
15
15
  # An argument's kind says what goes there: a property of the collection's
16
16
  # items ("property"), a value a property can have ("value", the property
17
- # being the argument before), a date format ("format"), a translation's key
18
- # ("key"), or anything ("any"). `keeps` is "collection" (the same items),
19
- # "item" (one of them) or nil. Labels are liquidum.filters.<name>, in the
17
+ # being the argument before), a date format ("format", strftime's, with the
18
+ # app's date.formats and time.formats as its `values`), a
19
+ # translation's key ("key"), a locale ("locale", with the app's locales as
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
20
25
  # current locale; a host describes its own filters the same way:
21
26
  #
22
27
  # Liquidum::Filters.describe :money, arguments: [], keeps: nil
@@ -34,23 +39,64 @@ module Liquidum
34
39
  def all
35
40
  names = Liquid::Environment.default.strainer_template.filter_method_names.map(&:to_s).sort
36
41
  names.map do |name|
37
- 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] }
38
46
  {
39
47
  name: name,
40
48
  label: I18n.t(name, scope: "liquidum.filters", default: name.humanize),
41
- arguments: description[:arguments].map { |argument, kind| {name: argument.to_s, kind: (kind || :any).to_s} },
42
- 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
43
51
  }.compact
44
52
  end
45
53
  end
46
54
 
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)
69
+ values = case kind
70
+ when :locale then I18n.available_locales.map { |locale| {value: locale.to_s, label: locale.to_s} }
71
+ when :format then formats
72
+ end
73
+ optional = parameter ? parameter.first == :opt : nil
74
+ {name: name.to_s, kind: kind.to_s, values: values, optional: optional}.compact
75
+ end
76
+
77
+ # The app's own date and time formats (date.formats, time.formats in the
78
+ # current locale), strftime's patterns as date and localize take them,
79
+ # each once, labelled with its name
80
+ def formats
81
+ %w[date time].flat_map do |type|
82
+ patterns = I18n.t("#{type}.formats", default: {})
83
+ next [] unless patterns.is_a?(Hash)
84
+
85
+ patterns.filter_map do |format, pattern|
86
+ next unless pattern.is_a?(String)
87
+
88
+ {value: pattern, label: I18n.t(type, scope: "liquidum.formats", name: format, default: "#{type} #{format}")}
89
+ end
90
+ end.uniq { |format| format[:value] }
91
+ end
92
+
47
93
  property = [:property, :property]
48
94
  value = [:value, :value]
49
95
  {
50
96
  # On collections
51
- "compact" => [[], :collection], "concat" => [[[:items]], :collection], "first" => [[], :item], "last" => [[], :item],
52
- "join" => [[[:separator]]], "map" => [[property]], "reverse" => [[], :collection], "size" => [[]],
53
- "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],
54
100
  "where" => [[property, value], :collection], "reject" => [[property, value], :collection],
55
101
  "where_exp" => [[[:item], [:condition]], :collection], "find" => [[property, value], :item],
56
102
  "find_index" => [[property, value]], "has" => [[property, value]], "sum" => [[]], "min" => [[]], "max" => [[]],
@@ -71,7 +117,7 @@ module Liquidum
71
117
  "minus" => [[[:number]]], "times" => [[[:number]]], "divided_by" => [[[:number]]], "modulo" => [[[:number]]],
72
118
  "at_least" => [[[:number]]], "at_most" => [[[:number]]],
73
119
  # Dates
74
- "date" => [[[:format, :format]]], "localize" => [[[:locale], [:format, :format]]], "strptime" => [[[:format, :format]]],
120
+ "date" => [[[:format, :format]]], "localize" => [[[:locale, :locale], [:format, :format]]], "strptime" => [[[:format, :format]]],
75
121
  "timezone" => [[[:zone]]], "to" => [[[:type]]]
76
122
  }.each { |name, (arguments, keeps)| describe(name, arguments: arguments, keeps: keeps) }
77
123
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Liquidum
4
- VERSION = "1.4.3"
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.3
4
+ version: 1.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom de Grunt