act_with_flags 3.1.0 → 3.1.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4e2b18cdaf7777bddb191c830bc942a6fd7c377a4267c8e7beae3b2ace3552e9
4
- data.tar.gz: 1aed1f1e86fdbdd1cff793c0296c1b6e23c7400dc43f7df17ad8e13c3b1998d0
3
+ metadata.gz: c995a59c8536ec6e012c9bf671e087b9f333c7a327512a42e1ffe725f0a12a0f
4
+ data.tar.gz: 63e16f84e582fd6427846d9391e3f02b3a528cda8cbdf16670c368c719af399e
5
5
  SHA512:
6
- metadata.gz: 11154bcaf21ff519f34863fdb847ffefd1e9a2fb493cfcdd8d0e015de42b601a9b3bf20d9e914d0e8681f39e321194ba3a27048a74fb43ce3972b9844cf0f14c
7
- data.tar.gz: 4471f2dc9a7b48654fa9ea689e4f51e660f6eacb0012fc1ea75c43fd2cb9c91149c49243e24a78e4527407d4c75dd51e7b0066095efebb14f007eb3dda36a839
6
+ metadata.gz: e82dc33cbc493c8b849a3053c5d28e2d73522a9308bbb7d59fa698dfa8a58c7380b9c58df082aa3190010f112c32b51583356373a935064a6f8222cb449283d7
7
+ data.tar.gz: 2fe6db8a4d0177db3d679e57b3851d79559ddbbe741352b0144cd0a6bcaba717584241bccae242e862c60bf0109c32252ca8472acdab1591ca61b3aa579ee31e
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- act_with_flags (3.1.0)
4
+ act_with_flags (3.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -157,7 +157,7 @@ GEM
157
157
  regexp_parser (2.5.0)
158
158
  rexml (3.2.5)
159
159
  ricecream (0.2.1)
160
- rubocop (1.35.0)
160
+ rubocop (1.35.1)
161
161
  json (~> 2.3)
162
162
  parallel (~> 1.10)
163
163
  parser (>= 3.1.2.1)
@@ -180,8 +180,8 @@ GEM
180
180
  simplecov-html (0.12.3)
181
181
  simplecov_json_formatter (0.1.4)
182
182
  sqlite3 (1.4.4)
183
- standard (1.16.0)
184
- rubocop (= 1.35.0)
183
+ standard (1.16.1)
184
+ rubocop (= 1.35.1)
185
185
  rubocop-performance (= 1.14.3)
186
186
  standardrb (1.0.1)
187
187
  standard
data/README.md CHANGED
@@ -23,9 +23,50 @@ $ gem install act_with_flags
23
23
  ```
24
24
 
25
25
 
26
- ## Version 3.1.x
26
+ ## Version 3.1.1
27
27
 
28
- Added option "range" limiting the position of booleans.
28
+ Option "range" can be specified just once for all
29
+ "add_to_flags" for a specific "origin".
30
+
31
+ For example:
32
+ ~~~ruby
33
+ Order.add_to_flags range: ..0
34
+ ...
35
+ Order.add_to_flags :a
36
+ ~~~
37
+
38
+ Same as:
39
+ ~~~ruby
40
+ Order.add_to_flags :a
41
+ ...
42
+ Order.add_to_flags range: ..0
43
+ ~~~
44
+
45
+ or:
46
+ ~~~ruby
47
+ Order.add_to_flags :a, range: ..0
48
+ ~~~
49
+
50
+ Examples for "range":
51
+ ~~~ruby
52
+ Order.add_to_flags range: 0..17 # legal flag position from 0 to 17
53
+ Order.add_to_flags range: ..17 # legal flag position from 0 to 17
54
+ Order.add_to_flags range: nil..17 # legal flag position from 0 to 17
55
+ Order.add_to_flags range: 3.. # legal flag position from 3 to big_number
56
+ Order.add_to_flags range: 3..nil # legal flag position from 3 to big_number
57
+ ~~~
58
+
59
+ Invalid ranges:
60
+ ~~~ruby
61
+ Order.add_to_flags range: -1..17 # range starting with a negative position
62
+ Order.add_to_flags range: :a..:z # invalid range
63
+ Order.add_to_flags range: "a".."z" # invalid range
64
+ ~~~
65
+
66
+
67
+ ## Version 3.1.0
68
+
69
+ Added option "range" limiting the position of flags.
29
70
 
30
71
  An example:
31
72
  ```ruby
@@ -1,11 +1,12 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ActWithFlags::Admin
4
- attr_reader :model
4
+ attr_reader :model, :ranges
5
5
 
6
6
  def initialize(model)
7
7
  @locations = {}
8
8
  @clears = {}
9
+ @ranges = {}
9
10
  @model = model
10
11
  @boolean_hash = {}
11
12
  [true, "true", 1, "1"].each { |x| @boolean_hash[x] = true }
@@ -5,7 +5,6 @@ class ActWithFlags::Admin
5
5
  res = []
6
6
  res << title("Variables")
7
7
  res << variables(:boolean_hash)
8
- res << variables(:delete_mask)
9
8
 
10
9
  res << blk("Flags sorted alfabetically") { |key, loc|
11
10
  "#{key} #{loc}"
@@ -17,6 +16,8 @@ class ActWithFlags::Admin
17
16
  "FLAG_#{key.upcase} = #{sprintf("0x%08X", mask(key))}"
18
17
  }
19
18
 
19
+ res << title("@ranges")
20
+ res << @ranges
20
21
  res << title("@locations")
21
22
  res << @locations
22
23
  res.flatten.join("\n")
@@ -1,19 +1,19 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class ActWithFlags::Admin
4
- def add_flag(name, pos, origin, range)
4
+ def add_flag(name, pos, origin)
5
+ range = ranges[origin]
5
6
  accessor = name.to_sym
6
7
  validate_accessor accessor, "#{accessor}?", "#{accessor}="
7
8
 
8
9
  pos = check_pos(model, origin, pos)
10
+ msg = "Invalid position <#{pos}>"
11
+ raise(ArgumentError, msg) unless pos.is_a?(Integer)
12
+ raise(ArgumentError, msg) unless pos >= 0
9
13
  loc = Location.new(model, origin, pos)
10
14
  add_to_locations accessor, loc
11
15
 
12
- unless range.nil?
13
- unless range.cover?(pos)
14
- raise RangeError, "Position <#{loc.position}> out of range <#{range}>"
15
- end
16
- end
16
+ validate_position(range, pos)
17
17
 
18
18
  mask = mask(accessor)
19
19
  add_accessors(accessor, origin, mask)
@@ -50,6 +50,14 @@ class ActWithFlags::Admin
50
50
  reset_model model
51
51
  end
52
52
 
53
+ def validate_position(range, position)
54
+ return if range.nil?
55
+ return if range.cover?(position)
56
+
57
+ msg = "Position #{position} out of range #{range}"
58
+ raise RangeError, msg
59
+ end
60
+
53
61
  private
54
62
 
55
63
  def validate_accessor(*names)
@@ -1,7 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActWithFlags
4
- VERSION = "3.1.0" # 2022-08-29
4
+ VERSION = "3.1.1" # 2022-09-01
5
+ # VERSION = "3.1.0" # 2022-08-29
5
6
  # VERSION = "3.0.1" # 2022-08-07
6
7
  # VERSION = "3.0.0" # 2022-07-27
7
8
  # VERSION = "0.2.4" # 2021-06-21
@@ -14,13 +14,11 @@ module ActWithFlags
14
14
  attr_reader :act_with_flags
15
15
 
16
16
  def add_to_flags(*flags, origin: :flags, range: nil, **hash)
17
- unless @act_with_flags
18
- @act_with_flags ||= ActWithFlags::Admin.new self
19
- @act_with_flags.add_mask_et_all origin
20
- end
17
+ origin = origin.to_sym
18
+ init(origin, range)
21
19
 
22
- flags.each { |name| @act_with_flags.add_flag(name, nil, origin, range) }
23
- hash.each { |name, pos| @act_with_flags.add_flag(name, pos, origin, range) }
20
+ flags.each { |name| @act_with_flags.add_flag(name, nil, origin) }
21
+ hash.each { |name, pos| @act_with_flags.add_flag(name, pos, origin) }
24
22
 
25
23
  @act_with_flags
26
24
  end
@@ -32,6 +30,45 @@ module ActWithFlags
32
30
  def clear_flags_at_save(*flags)
33
31
  @act_with_flags.clear_at_save(*flags)
34
32
  end
33
+
34
+ private
35
+
36
+ def init(origin, range)
37
+ unless @act_with_flags
38
+ @act_with_flags ||= ActWithFlags::Admin.new self
39
+ @act_with_flags.add_mask_et_all origin
40
+ end
41
+
42
+ unless range.nil?
43
+ validate_range_value range.begin
44
+ validate_range_value range.end
45
+ end
46
+
47
+ rng = @act_with_flags.ranges[origin]
48
+ unless range.nil? || (range == rng)
49
+ msg = "incompatible ranges #{range} - #{rng}"
50
+ raise ArgumentError, msg unless rng.nil?
51
+ @act_with_flags.ranges[origin] = range if range
52
+ validate_previous_positions(origin, range)
53
+ end
54
+ end
55
+
56
+ def validate_previous_positions(origin, range)
57
+ return if range.nil?
58
+
59
+ @act_with_flags.locations.each do |name, location|
60
+ next unless location.origin == origin
61
+
62
+ @act_with_flags.validate_position(range, location.position)
63
+ end
64
+ end
65
+
66
+ def validate_range_value(range_value)
67
+ return if range_value.nil?
68
+ return if range_value.is_a?(Integer) && range_value >= 0
69
+
70
+ raise RangeError, "Invalid range value #{range_value}"
71
+ end
35
72
  end
36
73
  end
37
74
 
@@ -0,0 +1,59 @@
1
+ require "test_helper"
2
+
3
+ describe "range #2" do
4
+ def setup
5
+ reset_order
6
+ end
7
+
8
+ it "succeeds specifying just a range" do
9
+ Order.add_to_flags range: 0...0
10
+ end
11
+
12
+ it "succeeds specifying just a (same) range with an origin" do
13
+ Order.add_to_flags range: 0..0
14
+ Order.add_to_flags range: 0..0, origin: :dummy
15
+ end
16
+
17
+ it "rejects more than one range specification" do
18
+ Order.add_to_flags range: 0..0
19
+ assert_raises(ArgumentError) { Order.add_to_flags range: 1..1 }
20
+ end
21
+
22
+ it "succeeds validation (pre specification of range)" do
23
+ Order.add_to_flags range: ..0
24
+ Order.add_to_flags :a
25
+ end
26
+
27
+ it "succeeds validation (post specification of range)" do
28
+ Order.add_to_flags :a
29
+ Order.add_to_flags range: ..0
30
+ end
31
+
32
+ it "fails validation (pre specification of range)" do
33
+ Order.add_to_flags range: (1..)
34
+ assert_raises(RangeError) { Order.add_to_flags :a }
35
+ end
36
+
37
+ it "fails validation (post specification of range)" do
38
+ Order.add_to_flags :b, origin: :hugo
39
+ Order.add_to_flags :a
40
+ assert_raises(RangeError) { Order.add_to_flags range: 1.. }
41
+ end
42
+
43
+ it "succeeds validation for two flags (post specification of range)" do
44
+ Order.add_to_flags :a, :b # bits 0 & 1
45
+ Order.add_to_flags range: ..1
46
+ end
47
+
48
+ it "rejects validation for two flags (post specification of range)" do
49
+ Order.add_to_flags :a, :b # bits 0 & 1
50
+ # :a (bit 0) triggers an exception on the validation of range
51
+ assert_raises(RangeError) { Order.add_to_flags range: 1.. }
52
+ end
53
+
54
+ it "rejects post validation" do
55
+ Order.add_to_flags :a, :b # bits 0 & 1
56
+ # :b (bit 1) triggers an exception on the validation of range
57
+ assert_raises(RangeError) { Order.add_to_flags range: ..0 }
58
+ end
59
+ end
@@ -0,0 +1,37 @@
1
+ require "test_helper"
2
+
3
+ describe "range #3" do
4
+ def setup
5
+ reset_order
6
+ end
7
+
8
+ it "accepts an integer as position" do
9
+ Order.add_to_flags a: 0
10
+ Order.add_to_flags b: 1
11
+ Order.add_to_flags c: 2
12
+ Order.add_to_flags d: 100
13
+
14
+ msk = Order.add_to_flags.mask(:a, :b, :c)
15
+ assert 0x07, msk
16
+
17
+ msk = Order.add_to_flags.mask(:d)
18
+ assert 0x10000000000000000000000000, msk
19
+ end
20
+
21
+ it "rejects ivalid position" do
22
+ assert_raises(ArgumentError) { Order.add_to_flags a: -1 }
23
+ assert_raises(ArgumentError) { Order.add_to_flags a: :a_symbol }
24
+ end
25
+
26
+ [0..0, 0..1, ..0, 1.., 100..100, 0...1, ...1, 1..., ...100].each do |range|
27
+ it "checks valid range #{range}" do
28
+ Order.add_to_flags range: range
29
+ end
30
+ end
31
+
32
+ [-1..0, :a..:z, "a".."z"].each do |range|
33
+ it "rejects invalid range #{range}" do
34
+ assert_raises(RangeError) { Order.add_to_flags range: range }
35
+ end
36
+ end
37
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: act_with_flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dittmar Krall
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-29 00:00:00.000000000 Z
11
+ date: 2022-09-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -151,6 +151,8 @@ files:
151
151
  - test/one_test.rb
152
152
  - test/origin_test.rb
153
153
  - test/origins_test.rb
154
+ - test/range2_test.rb
155
+ - test/range3_test.rb
154
156
  - test/range_test.rb
155
157
  - test/remove_from_test.rb
156
158
  - test/string_test.rb