order_already 0.2.0 → 0.3.0

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: c26d17f72e31fe01a89459e031b394237dfe163d8c5548307cc7b00308115175
4
- data.tar.gz: 0c1b6a4e25e973ff90a2dcd05cc21b9b3267a9110f762c15707cee9d4b7e10a8
3
+ metadata.gz: 06d0792edcb48d855dd6d7fe7f7e7bd28ee198f6144a816e4b7594f49ea7fe32
4
+ data.tar.gz: bc0bb4eafc7c7f01bdbb90e3a5ab20b59abc74f797509f0da4f592ba946ca628
5
5
  SHA512:
6
- metadata.gz: 640aae2c6f8f6c678afe164aa37044afad6192f88ea042e7aa50b431e62258cd267a95368ab82bbcb2a28845e57be939d7a663f8e8995bb532dc038d5d8c0c54
7
- data.tar.gz: 6400c35df376c1767b59d7a8bbab52a8554917c51c84fdd4592a09b6bdf703d344e2765824d6a7f1ab24d1d96b37a62045a74bd03906ab981ae302c9a2e53719
6
+ metadata.gz: 57752a7bf98a638194b27db452a8028e30cfe94e2490972ee3b9164e72c3e20e6fbb3db4db9c1b6e3a1987259daf29e69b78657578a42342d68377d11e127a50
7
+ data.tar.gz: 16d4471e24105b2641ff0640963e3de6a2eff56dc84e0ea104415b1b4cbbe77ab146fcb71df6938f60f4bf1158b5fcf0bdee2d7e2c22939a8209669c83455d29
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- order_already (0.1.0)
4
+ order_already (0.2.1)
5
5
  rails-html-sanitizer (~> 1.4)
6
6
 
7
7
  GEM
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.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module OrderAlready
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
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? Inject a new serializer.
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
- def self.for(*attributes, serializer: StringSerializer)
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
- attributes.each do |attribute|
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
- module StringSerializer
53
- # defaults
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 https://stackoverflow.com/questions/15442298/is-sort-in-ruby-stable
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.2.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: 2022-11-19 00:00:00.000000000 Z
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