ocg 1.3.4 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2c061519b8e290df24b149da93956abfafc1376a873a9b035481a78478619aa6
4
- data.tar.gz: c8c32a9bc9011616c618c8f4bf8adae21e90931ac1061fefa09c5db23879535f
3
+ metadata.gz: 0fa3d1a73808ebe1197e76243c66e4d00b0220c4bbe398e2fcf11e1f58c0f243
4
+ data.tar.gz: 8709140dacba810330d6ec2b4889002e0a432a1acd4e5af3767ed3a91cded7a4
5
5
  SHA512:
6
- metadata.gz: aa1359a517003142c14bbf01fc65a09583eb1f7fbe8feca02c5c318ce3ff625f9701d118fdaee261c88fb5bdff3fa2a4f83de9e258df447cd126736e1abe2692
7
- data.tar.gz: 86a7fc958ba29efb13a8048c0b5fc221b5dc24b553dc8a21462b7bed93a13ace4da8a4d0faeb03492b070a897e42143e42c082c0a1e6d0d8ec7eddaff49796a3
6
+ metadata.gz: 374148f48f17496aa4d6c572c6e091d08f12b79a8bc1172c2605cb91d30bc1e9a70bda8591fc28e96b1445895c17c930f9dff0b96e5c0460a98d3ed67a9e8b3a
7
+ data.tar.gz: 1f403b651a77e2b905543008ba198ddbf6ac6eb02dac413de5aab076458d2386815b6c6baf42b77025790273d108e0ba3b13649c7a56ea49e2940dd6245f9a04
data/README.md CHANGED
@@ -82,7 +82,7 @@ Options should be prepared in the following form:
82
82
  }
83
83
  ```
84
84
 
85
- Options hash should not be empty.
85
+ Options hash may be empty.
86
86
  `option_name` can be any valid hash key.
87
87
  `option_values` should be convertable to array using `to_a`.
88
88
  `option_values` should not be empty.
data/lib/ocg/main.rb CHANGED
@@ -17,7 +17,7 @@ class OCG
17
17
 
18
18
  def_delegators :@generator, *DELEGATORS
19
19
 
20
- def initialize(generator_or_options)
20
+ def initialize(generator_or_options = {})
21
21
  @generator = self.class.prepare_generator generator_or_options
22
22
  end
23
23
 
@@ -27,15 +27,15 @@ class OCG
27
27
  Options.new generator_or_options
28
28
  end
29
29
 
30
- def and(generator_or_options)
30
+ def and(generator_or_options = {})
31
31
  Operator::AND.new self, generator_or_options
32
32
  end
33
33
 
34
- def mix(generator_or_options)
34
+ def mix(generator_or_options = {})
35
35
  Operator::MIX.new self, generator_or_options
36
36
  end
37
37
 
38
- def or(generator_or_options)
38
+ def or(generator_or_options = {})
39
39
  Operator::OR.new self, generator_or_options
40
40
  end
41
41
 
@@ -22,6 +22,16 @@ class OCG
22
22
  nil
23
23
  end
24
24
 
25
+ protected def merge_results(left, right)
26
+ if left.nil?
27
+ right
28
+ elsif right.nil?
29
+ left
30
+ else
31
+ left.merge right
32
+ end
33
+ end
34
+
25
35
  # :nocov:
26
36
  def next
27
37
  raise NotImplementedError, "\"next\" is not implemented"
@@ -11,21 +11,22 @@ class OCG
11
11
 
12
12
  if @right_generator.finished?
13
13
  @right_generator.reset
14
- @left_generator.next.merge @right_generator.next
14
+ left = @left_generator.next
15
15
  else
16
- left_last = @left_generator.last
17
- left_last = @left_generator.next if left_last.nil?
18
- left_last.merge @right_generator.next
16
+ left = @left_generator.last
17
+ left = @left_generator.next if left.nil?
19
18
  end
19
+
20
+ right = @right_generator.next
21
+
22
+ merge_results left, right
20
23
  end
21
24
 
22
25
  def last
23
- left_last = @left_generator.last
24
- right_last = @right_generator.last
25
-
26
- return nil if left_last.nil? || right_last.nil?
26
+ left = @left_generator.last
27
+ right = @right_generator.last
27
28
 
28
- left_last.merge right_last
29
+ merge_results left, right
29
30
  end
30
31
 
31
32
  def started?
@@ -37,7 +38,16 @@ class OCG
37
38
  end
38
39
 
39
40
  def length
40
- @left_generator.length * @right_generator.length
41
+ left_length = @left_generator.length
42
+ right_length = @right_generator.length
43
+
44
+ if left_length.zero?
45
+ right_length
46
+ elsif right_length.zero?
47
+ left_length
48
+ else
49
+ left_length * right_length
50
+ end
41
51
  end
42
52
  end
43
53
  end
@@ -32,16 +32,18 @@ class OCG
32
32
 
33
33
  @left_generator.reset if @left_generator.finished?
34
34
  @right_generator.reset if @right_generator.finished?
35
- @left_generator.next.merge @right_generator.next
35
+
36
+ left = @left_generator.next
37
+ right = @right_generator.next
38
+
39
+ merge_results left, right
36
40
  end
37
41
 
38
42
  def last
39
- left_last = @left_generator.last
40
- right_last = @right_generator.last
41
-
42
- return nil if left_last.nil? || right_last.nil?
43
+ left = @left_generator.last
44
+ right = @right_generator.last
43
45
 
44
- left_last.merge right_last
46
+ merge_results left, right
45
47
  end
46
48
 
47
49
  def started?
@@ -17,13 +17,13 @@ class OCG
17
17
  end
18
18
 
19
19
  def last
20
- left_last = @left_generator.last
21
- right_last = @right_generator.last
20
+ left = @left_generator.last
21
+ right = @right_generator.last
22
22
 
23
- if right_last.nil?
24
- left_last
23
+ if right.nil?
24
+ left
25
25
  else
26
- right_last
26
+ right
27
27
  end
28
28
  end
29
29
 
data/lib/ocg/options.rb CHANGED
@@ -21,7 +21,7 @@ class OCG
21
21
 
22
22
  reset_value_indexes
23
23
 
24
- @is_finished = false
24
+ @is_finished = length.zero?
25
25
  end
26
26
 
27
27
  protected def reset_value_indexes
@@ -36,7 +36,7 @@ class OCG
36
36
  # If state is finished than all value indexes are already zero.
37
37
  reset_value_indexes unless @is_finished
38
38
 
39
- @is_finished = false
39
+ @is_finished = length.zero?
40
40
 
41
41
  nil
42
42
  end
@@ -76,7 +76,13 @@ class OCG
76
76
  end
77
77
 
78
78
  def length
79
- @options.reduce(1) { |length, (_name, values)| length * values.length }
79
+ @options.reduce(0) do |length, (_name, values)|
80
+ if length.zero?
81
+ values.length
82
+ else
83
+ length * values.length
84
+ end
85
+ end
80
86
  end
81
87
  end
82
88
  end
@@ -7,7 +7,6 @@ class OCG
7
7
  module Validation
8
8
  def self.validate_options(options)
9
9
  raise ValidateError, "invalid options hash" unless options.is_a? ::Hash
10
- raise ValidateError, "options should not be empty" if options.empty?
11
10
 
12
11
  options.each do |_name, values|
13
12
  raise ValidateError, "option values should respond to \"to_a\"" unless values.respond_to? :to_a
data/lib/ocg/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # Copyright (c) 2019 AUTHORS, MIT License.
3
3
 
4
4
  class OCG
5
- VERSION = "1.3.4".freeze
5
+ VERSION = "1.4.0".freeze
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ocg
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.4
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Aladjev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-24 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: codecov
@@ -72,14 +72,14 @@ dependencies:
72
72
  requirements:
73
73
  - - "~>"
74
74
  - !ruby/object:Gem::Version
75
- version: '1.21'
75
+ version: '1.22'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
80
  - - "~>"
81
81
  - !ruby/object:Gem::Version
82
- version: '1.21'
82
+ version: '1.22'
83
83
  - !ruby/object:Gem::Dependency
84
84
  name: rubocop-minitest
85
85
  requirement: !ruby/object:Gem::Requirement