order_already 0.1.0 → 0.2.1
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 +4 -4
- data/CHANGELOG.md +7 -0
- data/Gemfile.lock +1 -1
- data/README.md +4 -2
- data/lib/order_already/version.rb +1 -1
- data/lib/order_already.rb +24 -10
- data/order_already.gemspec +2 -2
- metadata +5 -4
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/CHANGELOG.md
ADDED
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -24,7 +24,7 @@ With a plain old Ruby object (PORO):
|
|
24
24
|
class MyModel
|
25
25
|
attr_accessor :author, :subject
|
26
26
|
|
27
|
-
|
27
|
+
prepend OrderAlready.for(:author, :subject)
|
28
28
|
end
|
29
29
|
```
|
30
30
|
|
@@ -43,10 +43,12 @@ class GenericWork < ActiveFedora::Base
|
|
43
43
|
# schema (by adding accepts_nested_attributes)
|
44
44
|
include ::Hyrax::BasicMetadata
|
45
45
|
|
46
|
-
|
46
|
+
prepend OrderAlready.for(:creator, :contributor)
|
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
@@ -1,10 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require_relative "order_already/version"
|
4
|
-
# require "set"
|
5
|
-
# require "loofah"
|
6
4
|
require "rails-html-sanitizer"
|
7
|
-
# require "rails/html/scrubbers"
|
8
5
|
|
9
6
|
# In the spirit of Browse Everything and Questioning Authority, the Order Already module provides a
|
10
7
|
# simple interface for ordering properties that have an indeterminate persistence order (looking at
|
@@ -19,7 +16,11 @@ module OrderAlready
|
|
19
16
|
# @api public
|
20
17
|
#
|
21
18
|
# @param attributes [Array<Symbol>] the name of the attributes/properties you want to order.
|
22
|
-
# @
|
19
|
+
# @param serializer [#serialize, #deserialize] the service class responsible for serializing the
|
20
|
+
# data. Want to auto-alphabetize? Use a different serializer than the default.
|
21
|
+
#
|
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,25 +35,35 @@ 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|
|
41
48
|
define_method(attribute) do
|
42
|
-
|
49
|
+
serializer.deserialize(super())
|
43
50
|
end
|
44
51
|
|
45
52
|
define_method("#{attribute}=") do |values|
|
46
|
-
super(
|
53
|
+
super(serializer.serialize(values))
|
47
54
|
end
|
48
55
|
end
|
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
|
data/order_already.gemspec
CHANGED
@@ -8,8 +8,8 @@ Gem::Specification.new do |spec|
|
|
8
8
|
spec.authors = ["Jeremy Friesen", "Rob Kaufman"]
|
9
9
|
spec.email = ["jeremy.n.friesen@gmail.com", "rob@notch8.com"]
|
10
10
|
|
11
|
-
spec.summary = "A tiny gem to provide naive sorting for Fedora Commons
|
12
|
-
spec.description = "A tiny gem to provide naive sorting for Fedora Commons
|
11
|
+
spec.summary = "A tiny gem to provide naive sorting for Fedora Commons properties."
|
12
|
+
spec.description = "A tiny gem to provide naive sorting for Fedora Commons properties."
|
13
13
|
spec.homepage = "https://github.com/scientist-softserv/order_already"
|
14
14
|
spec.required_ruby_version = ">= 2.6.0"
|
15
15
|
|
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.1
|
4
|
+
version: 0.2.1
|
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: 2022-11-
|
12
|
+
date: 2022-11-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails-html-sanitizer
|
@@ -59,7 +59,7 @@ dependencies:
|
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
61
|
version: 5.0.2
|
62
|
-
description: A tiny gem to provide naive sorting for Fedora Commons
|
62
|
+
description: A tiny gem to provide naive sorting for Fedora Commons properties.
|
63
63
|
email:
|
64
64
|
- jeremy.n.friesen@gmail.com
|
65
65
|
- rob@notch8.com
|
@@ -69,6 +69,7 @@ extra_rdoc_files: []
|
|
69
69
|
files:
|
70
70
|
- ".rspec"
|
71
71
|
- ".rubocop.yml"
|
72
|
+
- CHANGELOG.md
|
72
73
|
- Gemfile
|
73
74
|
- Gemfile.lock
|
74
75
|
- LICENSE
|
@@ -102,5 +103,5 @@ requirements: []
|
|
102
103
|
rubygems_version: 3.3.7
|
103
104
|
signing_key:
|
104
105
|
specification_version: 4
|
105
|
-
summary: A tiny gem to provide naive sorting for Fedora Commons
|
106
|
+
summary: A tiny gem to provide naive sorting for Fedora Commons properties.
|
106
107
|
test_files: []
|