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

Returns a new instance of Item.



8
9
10
11
12
13
# File 'lib/test_ids/configuration.rb', line 8

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

Instance Attribute Details

#algorithmObject

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

#excludeObject

Returns the value of attribute exclude.



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

def exclude
  @exclude
end

#includeObject

Returns the value of attribute include.



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

def include
  @include
end

#needsObject

Returns the value of attribute needs.



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

def needs
  @needs
end

#sizeObject

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

#empty?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/test_ids/configuration.rb', line 29

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

#freezeObject



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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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_allObject

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