Class: TestIds::Configuration::Item
- Inherits:
-
Object
- Object
- TestIds::Configuration::Item
- Defined in:
- lib/test_ids/configuration.rb
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
Returns the value of attribute algorithm.
-
#exclude ⇒ Object
Returns the value of attribute exclude.
-
#include ⇒ Object
Returns the value of attribute include.
-
#needs ⇒ Object
Returns the value of attribute needs.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #callback(options = {}, &block) ⇒ Object
- #empty? ⇒ Boolean
- #freeze ⇒ Object
- #function? ⇒ Boolean
-
#initialize ⇒ Item
constructor
A new instance of Item.
- #load_from_serialized(o) ⇒ Object private
- #needs?(type) ⇒ Boolean
- #to_json(*a) ⇒ Object
- #valid?(number) ⇒ Boolean
-
#yield_all ⇒ Object
Yields all included numbers to the given block, one at a time.
Constructor Details
Instance Attribute Details
#algorithm ⇒ Object
Returns the value of attribute algorithm.
4 5 6 |
# File 'lib/test_ids/configuration.rb', line 4 def algorithm @algorithm end |
#exclude ⇒ Object
Returns the value of attribute exclude.
4 5 6 |
# File 'lib/test_ids/configuration.rb', line 4 def exclude @exclude end |
#include ⇒ Object
Returns the value of attribute include.
4 5 6 |
# File 'lib/test_ids/configuration.rb', line 4 def include @include end |
#needs ⇒ Object
Returns the value of attribute needs.
4 5 6 |
# File 'lib/test_ids/configuration.rb', line 4 def needs @needs end |
#size ⇒ Object
Returns the value of attribute size.
4 5 6 |
# File 'lib/test_ids/configuration.rb', line 4 def size @size end |
Instance Method Details
#callback(options = {}, &block) ⇒ Object
13 14 15 16 17 18 19 20 |
# File 'lib/test_ids/configuration.rb', line 13 def callback( = {}, &block) if block_given? @needs += Array([:needs]) @callback = block else @callback end end |
#empty? ⇒ Boolean
27 28 29 |
# File 'lib/test_ids/configuration.rb', line 27 def empty? include.empty? && exclude.empty? && !algorithm && !callback end |
#freeze ⇒ Object
43 44 45 46 47 48 |
# File 'lib/test_ids/configuration.rb', line 43 def freeze @include.freeze @exclude.freeze @needs.freeze super end |
#function? ⇒ Boolean
31 32 33 |
# File 'lib/test_ids/configuration.rb', line 31 def function? !!algorithm || !!callback end |
#load_from_serialized(o) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/test_ids/configuration.rb', line 51 def load_from_serialized(o) if o.is_a?(Hash) @size = o['size'] @include.load_from_serialized(o['include']) @exclude.load_from_serialized(o['exclude']) elsif o == 'callback' callback do fail 'The callback for this configuration is not available!' end else self.algorithm = o end end |
#needs?(type) ⇒ Boolean
22 23 24 25 |
# File 'lib/test_ids/configuration.rb', line 22 def needs?(type) !!(!empty? && function? && (needs.include?(type) || (algorithm && (algorithm.to_s =~ /#{type.to_s[0]}/i)))) end |
#to_json(*a) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/test_ids/configuration.rb', line 65 def to_json(*a) if callback 'callback'.to_json(*a) elsif algorithm algorithm.to_s.to_json(*a) else { 'include' => include, 'exclude' => exclude, 'size' => size }.to_json(*a) end end |
#valid?(number) ⇒ Boolean
35 36 37 38 39 40 41 |
# File 'lib/test_ids/configuration.rb', line 35 def valid?(number) if function? fail 'valid? is not supported for algorithm or callback-based assignments' end number = number.to_i include.include?(number) && !exclude.include?(number) end |
#yield_all ⇒ Object
Yields all included numbers to the given block, one at a time
80 81 82 83 84 85 |
# File 'lib/test_ids/configuration.rb', line 80 def yield_all include.yield_all do |i| yield i unless exclude.include?(i) end nil end |