order_already 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +2 -0
- data/lib/order_already/version.rb +1 -1
- data/lib/order_already.rb +30 -7
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 06d0792edcb48d855dd6d7fe7f7e7bd28ee198f6144a816e4b7594f49ea7fe32
|
4
|
+
data.tar.gz: bc0bb4eafc7c7f01bdbb90e3a5ab20b59abc74f797509f0da4f592ba946ca628
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 57752a7bf98a638194b27db452a8028e30cfe94e2490972ee3b9164e72c3e20e6fbb3db4db9c1b6e3a1987259daf29e69b78657578a42342d68377d11e127a50
|
7
|
+
data.tar.gz: 16d4471e24105b2641ff0640963e3de6a2eff56dc84e0ea104415b1b4cbbe77ab146fcb71df6938f60f4bf1158b5fcf0bdee2d7e2c22939a8209669c83455d29
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -47,6 +47,8 @@ class GenericWork < ActiveFedora::Base
|
|
47
47
|
end
|
48
48
|
```
|
49
49
|
|
50
|
+
As of v0.2.0, the `OrderAlready.for` method takes a `:serializer` keyword. By default this is the preserve the order of the input serializers.
|
51
|
+
|
50
52
|
## Development
|
51
53
|
|
52
54
|
After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/order_already.rb
CHANGED
@@ -17,9 +17,11 @@ module OrderAlready
|
|
17
17
|
#
|
18
18
|
# @param attributes [Array<Symbol>] the name of the attributes/properties you want to order.
|
19
19
|
# @param serializer [#serialize, #deserialize] the service class responsible for serializing the
|
20
|
-
# data. Want to auto-alphabetize?
|
20
|
+
# data. Want to auto-alphabetize? Use a different serializer than the default.
|
21
21
|
#
|
22
|
-
# @return [Module] a module that wraps the attr_accessor methods for the given :attributes.
|
22
|
+
# @return [Module] a module that wraps the attr_accessor methods for the given :attributes. In
|
23
|
+
# using a module, we have access to `super`; which is super convenient! The module will
|
24
|
+
# also add a `#attribute_is_ordered_already?` method.
|
23
25
|
#
|
24
26
|
# @note In testing, you need to use `prepend` instead of `include`; but that may be a function of
|
25
27
|
# my specs.
|
@@ -34,10 +36,20 @@ module OrderAlready
|
|
34
36
|
# end
|
35
37
|
# prepend OrderAlready.for(:creators)
|
36
38
|
# end
|
37
|
-
|
39
|
+
#
|
40
|
+
# class OtherRecord
|
41
|
+
# attr_accessor :subjects
|
42
|
+
# # Assumes there's an Alphabetizer constant that responds to .serialize and .deserialize
|
43
|
+
# prepend OrderAlready.for(:subjects, serializer: Alphabetizer)
|
44
|
+
# end
|
45
|
+
def self.for(*attributes, serializer: InputOrderSerializer)
|
46
|
+
# Capturing the named attributes to create a local binding; this helps ensure we have that
|
47
|
+
# available in the later :attribute_is_ordered_already? method definition.
|
48
|
+
ordered_attributes = attributes.map(&:to_sym)
|
49
|
+
|
38
50
|
# By creating a module, we have access to `super`.
|
39
51
|
Module.new do
|
40
|
-
|
52
|
+
ordered_attributes.each do |attribute|
|
41
53
|
define_method(attribute) do
|
42
54
|
serializer.deserialize(super())
|
43
55
|
end
|
@@ -46,13 +58,21 @@ module OrderAlready
|
|
46
58
|
super(serializer.serialize(values))
|
47
59
|
end
|
48
60
|
end
|
61
|
+
|
62
|
+
define_method(:attribute_is_ordered_already?) do |attribute|
|
63
|
+
ordered_attributes.include?(attribute.to_sym)
|
64
|
+
end
|
49
65
|
end
|
50
66
|
end
|
51
67
|
|
52
|
-
|
53
|
-
|
68
|
+
# This serializer preserves the input order regardless of underlying persistence order.
|
69
|
+
#
|
70
|
+
# The two public methods are {.deserialize} and {.serialize}.
|
71
|
+
module InputOrderSerializer
|
54
72
|
TOKEN_DELIMITER = '~'
|
55
73
|
|
74
|
+
# @api public
|
75
|
+
#
|
56
76
|
# Convert a serialized array to a normal array of values.
|
57
77
|
# @param arr [Array]
|
58
78
|
# @return [Array]
|
@@ -64,6 +84,8 @@ module OrderAlready
|
|
64
84
|
end
|
65
85
|
end
|
66
86
|
|
87
|
+
# @api public
|
88
|
+
#
|
67
89
|
# Serialize a normal array of values to an array of ordered values
|
68
90
|
#
|
69
91
|
# @param arr [Array]
|
@@ -93,7 +115,8 @@ module OrderAlready
|
|
93
115
|
|
94
116
|
# Sort an array of serialized values using the index token to determine the order
|
95
117
|
def self.sort(arr)
|
96
|
-
# Hack to force a stable sort; see
|
118
|
+
# Hack to force a stable sort; see
|
119
|
+
# https://stackoverflow.com/questions/15442298/is-sort-in-ruby-stable
|
97
120
|
n = 0
|
98
121
|
arr.sort_by { |val| [get_index(val), n += 1] }
|
99
122
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: order_already
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeremy Friesen
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: exe
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2023-03-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails-html-sanitizer
|