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.
6 7 8 |
# File 'lib/test_ids/configuration.rb', line 6 def algorithm @algorithm end |
#exclude ⇒ Object
Returns the value of attribute exclude.
6 7 8 |
# File 'lib/test_ids/configuration.rb', line 6 def exclude @exclude end |
#include ⇒ Object
Returns the value of attribute include.
6 7 8 |
# File 'lib/test_ids/configuration.rb', line 6 def include @include end |
#needs ⇒ Object
Returns the value of attribute needs.
6 7 8 |
# File 'lib/test_ids/configuration.rb', line 6 def needs @needs end |
#size ⇒ Object
Returns the value of attribute size.
6 7 8 |
# File 'lib/test_ids/configuration.rb', line 6 def size @size end |
Instance Method Details
#callback(options = {}, &block) ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/test_ids/configuration.rb', line 15 def callback( = {}, &block) if block_given? @needs += Array([:needs]) @callback = block else @callback end end |
#empty? ⇒ Boolean
29 30 31 |
# File 'lib/test_ids/configuration.rb', line 29 def empty? include.empty? && exclude.empty? && !algorithm && !callback end |
#freeze ⇒ Object
44 45 46 47 48 49 |
# File 'lib/test_ids/configuration.rb', line 44 def freeze @include.freeze @exclude.freeze @needs.freeze super end |
#function? ⇒ Boolean
33 34 35 |
# File 'lib/test_ids/configuration.rb', line 33 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.
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/test_ids/configuration.rb', line 52 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
24 25 26 27 |
# File 'lib/test_ids/configuration.rb', line 24 def needs?(type) !!(!empty? && function? && (needs.include?(type) || (algorithm && (algorithm.to_s =~ /#{type.to_s[0]}/i)))) end |
#to_json(*a) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/test_ids/configuration.rb', line 66 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
37 38 39 40 41 42 |
# File 'lib/test_ids/configuration.rb', line 37 def valid?(number) fail 'valid? is not supported for algorithm or callback-based assignments' if function? 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
81 82 83 84 85 86 |
# File 'lib/test_ids/configuration.rb', line 81 def yield_all include.yield_all do |i| yield i unless exclude.include?(i) end nil end |