reposition 0.0.0 → 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/reposition.rb +63 -3
  3. metadata +3 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 27b3dfd5e89f59aba3bdc0e40aa4550f23d50c8c0305992cd75163c74e232906
4
- data.tar.gz: 4a4b700526ae0dcaf4b5d7b2a0db7067e736c9edb7476db8bfc00acefacbf40d
3
+ metadata.gz: 8f93defba45b51ce984baf4cc9edbc7d0aba93c330ee849b2eed941901997e75
4
+ data.tar.gz: 78fc8e57fa4f351e831aec1f8f383d46cc352a548dda07442f8a4d1394ddeb95
5
5
  SHA512:
6
- metadata.gz: 428052bfa0def8545a73ea0a45f52f4bf3feb7a3f1083b2175f1704a4130af6e0e38ec6a9f20e2bbff024dd8bd4e8a5e4aa47732c4312013296845f6f4875714
7
- data.tar.gz: 32c25aff6b67aa03f82b2ad611b7c93ec46289e0c7fd8804235872b4d0c22ac741a76aa2bbf02c2ebd904805a68d29fd7d35afb39ad6d8b0a8c5770deca38f77
6
+ metadata.gz: a2c63c5d1c0aa699e9d5763efc71b8b04dc557833cb3127774408b3f17f10fa2a8bbbea3bac827909c9d1bbbf673ddb35fb982f0cce9c80c5f2e86b46e89a9b1
7
+ data.tar.gz: 457ecf1977c71166cebc609dc123d956dd10fc5d7c6ea5c45399e21419099df840f58ed743515969688e5326f06ec4659f84230b9667fa121992e3b797f2c87d
data/lib/reposition.rb CHANGED
@@ -1,5 +1,65 @@
1
- class Reposition
2
- def self.hi
3
- puts "Hello world!"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: reposition
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Benson
@@ -20,7 +20,8 @@ files:
20
20
  homepage: https://rubygems.org/gems/reposition
21
21
  licenses:
22
22
  - MIT
23
- metadata: {}
23
+ metadata:
24
+ source_code_uri: https://github.com/mabenson00/reposition
24
25
  post_install_message:
25
26
  rdoc_options: []
26
27
  require_paths: