order_already 0.2.0 → 0.2.1
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 +20 -6
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1dab15b63f8b14d65dbbcb5f6fe968a03330e88ef48d8104233a90fa374a5087
|
4
|
+
data.tar.gz: fd1f5a1f2d9895c5012e280f40405b07c7c7729a98aa433b9ba9460f1bf0bd90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b06788255b18b24895117f7ff9dfd605ac67e85c5d60fb934dbb79fdcecea09b399510d74fc2327d0b0fbd9bd5b8643186940e3488d52c86c4593391084c8d4
|
7
|
+
data.tar.gz: '09010bdfeae1a452fb97c178c44f9ea0143f3df3b532f7684c3baf113edbb36295a51bf611f8066ebd38a95b7f3d782a4fc260a805f76b6161014348e4a32468'
|
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,10 @@ 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!
|
23
24
|
#
|
24
25
|
# @note In testing, you need to use `prepend` instead of `include`; but that may be a function of
|
25
26
|
# my specs.
|
@@ -34,7 +35,13 @@ module OrderAlready
|
|
34
35
|
# end
|
35
36
|
# prepend OrderAlready.for(:creators)
|
36
37
|
# end
|
37
|
-
|
38
|
+
#
|
39
|
+
# class OtherRecord
|
40
|
+
# attr_accessor :subjects
|
41
|
+
# # Assumes there's an Alphabetizer constant that responds to .serialize and .deserialize
|
42
|
+
# prepend OrderAlready.for(:subjects, serializer: Alphabetizer)
|
43
|
+
# end
|
44
|
+
def self.for(*attributes, serializer: InputOrderSerializer)
|
38
45
|
# By creating a module, we have access to `super`.
|
39
46
|
Module.new do
|
40
47
|
attributes.each do |attribute|
|
@@ -49,10 +56,14 @@ module OrderAlready
|
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
52
|
-
|
53
|
-
|
59
|
+
# This serializer preserves the input order regardless of underlying persistence order.
|
60
|
+
#
|
61
|
+
# The two public methods are {.deserialize} and {.serialize}.
|
62
|
+
module InputOrderSerializer
|
54
63
|
TOKEN_DELIMITER = '~'
|
55
64
|
|
65
|
+
# @api public
|
66
|
+
#
|
56
67
|
# Convert a serialized array to a normal array of values.
|
57
68
|
# @param arr [Array]
|
58
69
|
# @return [Array]
|
@@ -64,6 +75,8 @@ module OrderAlready
|
|
64
75
|
end
|
65
76
|
end
|
66
77
|
|
78
|
+
# @api public
|
79
|
+
#
|
67
80
|
# Serialize a normal array of values to an array of ordered values
|
68
81
|
#
|
69
82
|
# @param arr [Array]
|
@@ -93,7 +106,8 @@ module OrderAlready
|
|
93
106
|
|
94
107
|
# Sort an array of serialized values using the index token to determine the order
|
95
108
|
def self.sort(arr)
|
96
|
-
# Hack to force a stable sort; see
|
109
|
+
# Hack to force a stable sort; see
|
110
|
+
# https://stackoverflow.com/questions/15442298/is-sort-in-ruby-stable
|
97
111
|
n = 0
|
98
112
|
arr.sort_by { |val| [get_index(val), n += 1] }
|
99
113
|
end
|