reposition 0.0.0 → 0.0.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/lib/reposition.rb +63 -3
- 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: 0366a6b6c190349ab95d6445041ca00e7b5114d4d76ff3fa919ae34f64ee919e
|
4
|
+
data.tar.gz: 03edf89fdab5cb72e713fbc8c5d427fb9df2f892f6cd88217cb07e321a7e9be1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff8f923e84172806a087dde5271a01b38b70a9df91effb468b0fad07c8d5f3b667f9684340d2b46feba579551a720bf394db90191162565c42b2000988e7c907
|
7
|
+
data.tar.gz: ce9e49b3e20fa03d1d9f1e9cfdb9b7aa232f46c06e080bf56ddfe05c901ddd28e42f1f8e04cd726f028e1fec607287f12e091f6c8e69792fa8bc5124b905ee06
|
data/lib/reposition.rb
CHANGED
@@ -1,5 +1,65 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
module Reposition
|
2
|
+
# Repositions an object between two other objects based on a given attribute.
|
3
|
+
#
|
4
|
+
# @param object [Object] The object to reposition.
|
5
|
+
# @param preceding_object [Object] The object that comes before the object to reposition.
|
6
|
+
# @param following_object [Object] The object that comes after the object to reposition.
|
7
|
+
# @param attribute_name [Symbol] The name of the attribute to use for repositioning.
|
8
|
+
# @param options [Hash] Additional options for saving the object.
|
9
|
+
# - :validate [Boolean] Whether to validate the object before saving. Default is true.
|
10
|
+
# - :save [string] Options are save and save!. The default is not to save.
|
11
|
+
#
|
12
|
+
# @return [Object] The repositioned object.
|
13
|
+
def self.reposition(object:, preceding_object:, following_object:, attribute_name:, options: { validate: true, save: nil })
|
14
|
+
validate_arguments(object, preceding_object, following_object, attribute_name)
|
15
|
+
|
16
|
+
if preceding_object.nil?
|
17
|
+
object.update_attribute(attribute_name, following_object.send(attribute_name) - 1)
|
18
|
+
elsif following_object.nil?
|
19
|
+
object.update_attribute(attribute_name, preceding_object.send(attribute_name) + 1)
|
20
|
+
else
|
21
|
+
object.update_attribute(attribute_name, (preceding_object.send(attribute_name) + following_object.send(attribute_name)) / 2)
|
22
|
+
end
|
23
|
+
|
24
|
+
save_object(object, options)
|
25
|
+
|
26
|
+
object
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
# Validates the arguments for the reposition method.
|
32
|
+
#
|
33
|
+
# @param object [Object] The object to reposition.
|
34
|
+
# @param preceding_object [Object] The object that comes before the object to reposition.
|
35
|
+
# @param following_object [Object] The object that comes after the object to reposition.
|
36
|
+
# @param attribute_name [Symbol] The name of the attribute to use for repositioning.
|
37
|
+
#
|
38
|
+
# @raise [ArgumentError] If any of the arguments are invalid.
|
39
|
+
def self.validate_arguments(object, preceding_object, following_object, attribute_name)
|
40
|
+
raise ArgumentError, "object must be a Ruby object" unless object.is_a?(Object)
|
41
|
+
raise ArgumentError, "object does not have attribute #{attribute_name}" unless object.respond_to?(attribute_name)
|
42
|
+
raise ArgumentError, "preceding_object's #{attribute_name} is greater than following_object's #{attribute_name}" if preceding_object && following_object && preceding_object.send(attribute_name) > following_object.send(attribute_name)
|
43
|
+
end
|
44
|
+
|
45
|
+
# Updates the attribute of the object and saves it.
|
46
|
+
#
|
47
|
+
# @param object [Object] The object to update and save.
|
48
|
+
# @param attribute_name [Symbol] The name of the attribute to update.
|
49
|
+
# @param options [Hash] Additional options for saving the object.
|
50
|
+
# - :validate [Boolean] Whether to validate the object before saving. Default is true.
|
51
|
+
#
|
52
|
+
# @raise [ActiveRecord::RecordInvalid] If the object is invalid.
|
53
|
+
def self.update_attribute(object, attribute_name, options)
|
54
|
+
object.update_attribute(attribute_name, value)
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.save_object(object, options)
|
58
|
+
validate = [false, "false"].include?(options[:validate]) ? false : true
|
59
|
+
if options[:save] == "save!"
|
60
|
+
object.save!(validate: validate)
|
61
|
+
elsif options[:save] == "save"
|
62
|
+
object.save(validate: validate)
|
63
|
+
end
|
4
64
|
end
|
5
65
|
end
|