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

#initializeItem



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

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

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



4
5
6
# File 'lib/test_ids/configuration.rb', line 4

def algorithm
  @algorithm
end

#excludeObject

Returns the value of attribute exclude.



4
5
6
# File 'lib/test_ids/configuration.rb', line 4

def exclude
  @exclude
end

#includeObject

Returns the value of attribute include.



4
5
6
# File 'lib/test_ids/configuration.rb', line 4

def include
  @include
end

#needsObject

Returns the value of attribute needs.



4
5
6
# File 'lib/test_ids/configuration.rb', line 4

def needs
  @needs
end

#sizeObject

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(options = {}, &block)
  if block_given?
    @needs += Array(options[: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

#freezeObject



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_allObject

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