Class: TestIds::Configuration::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/test_ids/configuration.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Item

Returns a new instance of Item.



6
7
8
9
10
# File 'lib/test_ids/configuration.rb', line 6

def initialize
  @include = BinArray.new
  @exclude = BinArray.new
  @size = 1
end

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

#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(&block) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/test_ids/configuration.rb', line 12

def callback(&block)
  if block_given?
    @callback = block
  else
    @callback
  end
end

#empty? ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/test_ids/configuration.rb', line 20

def empty?
  include.empty? && exclude.empty? && !algorithm && !callback
end

#freeze ⇒ Object



36
37
38
39
40
# File 'lib/test_ids/configuration.rb', line 36

def freeze
  @include.freeze
  @exclude.freeze
  super
end

#function? ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/test_ids/configuration.rb', line 24

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.



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/test_ids/configuration.rb', line 43

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

#to_json(*a) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/test_ids/configuration.rb', line 57

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

Returns:

  • (Boolean)


28
29
30
31
32
33
34
# File 'lib/test_ids/configuration.rb', line 28

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



72
73
74
75
76
77
# File 'lib/test_ids/configuration.rb', line 72

def yield_all
  include.yield_all do |i|
    yield i unless exclude.include?(i)
  end
  nil
end