Class: MadderLib::Conditional::Helper::TestBlock

Inherits:
Object
  • Object
show all
Defined in:
lib/madderlib/conditional/helper.rb

Constant Summary collapse

FALSE =
lambda { false }
ONE =
lambda { 1 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ TestBlock

Returns a new instance of TestBlock.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/madderlib/conditional/helper.rb', line 11

def initialize(*args, &block)
	if block
		#	if we get a block, use it!
		raise Error, 'block AND args provided, requires one or the other' if (args && (! args.empty?))
		raise Error, 'block arity should be 0; 1 (count) or; 2 (count, Context)' if (block.arity > 2)

		#	it will remain unchanging
		@criteria = block
	else
		#	leave the originals alone
		args = args.clone

		begin
			#	how does it start?
			arg = args.shift

			if Range === arg
				#	we received a Range
				@criteria = arg
			elsif arg.respond_to?(:integer?) && arg.integer?
				upper = args.first
				if upper && upper.respond_to?(:integer?) && upper.integer?
					#	we can make a Range from that
					@criteria = Range.new(arg, args.shift)
				else
					#	just a count
					@criteria = arg
				end
			else
				raise Error, "invalid test block argument : #{arg.inspect}"
			end
		rescue Error => e
			raise e
		rescue Exception => e
			#	wrap
			raise e
			raise Error.new("invalid test block argument : #{arg.inspect}", e)
		end

		#	beyond that, is there a unit?
		#		we deal with all of that during build
		@units = args.shift
	end
end

Instance Attribute Details

#criteriaObject (readonly)

Returns the value of attribute criteria.



9
10
11
# File 'lib/madderlib/conditional/helper.rb', line 9

def criteria
  @criteria
end

Instance Method Details

#blockObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/madderlib/conditional/helper.rb', line 58

def block
	if Proc === @criteria
		#	the block will do its own testing
		@criteria
	elsif Range === @criteria
		#	we'll stop somewhere in that range
		#		note that it's inclusive
		#		if the caller says '2 .. 4', 4 should be a possibility
		limit = unitize(@criteria.rand_inclusive)
		lambda {|count| count < limit }
	elsif @criteria.integer?
		limit = unitize(@criteria)
		lambda {|count| count < limit }
	else
		#	never will succeed
		FALSE
	end
end

#invoke(*args, &given_block) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/madderlib/conditional/helper.rb', line 77

def invoke(*args, &given_block)
	#	take in a block, or use the internally held one
	#		allows for external buffering
	b = block_given? ? given_block : self.block
	p = args

	if b.arity < 1
		#	how interesting!  arity of -1
		b.call
	else
		#	only as many args as supported
		#		do not worry about shortfall; that'll be a failure
		p.pop while b.arity < p.size
		b.call *p
	end
end

#to_i(context) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/madderlib/conditional/helper.rb', line 96

def to_i(context)
	value = nil

	if Proc === @criteria
		value = MadderLib::Context.invoke(@criteria, context)
	elsif Range === @criteria
		value = @criteria.max
	elsif @criteria.integer?
		value = @criteria
	end

	#	has to be an integer, by definition
	if value && value.respond_to?(:integer?) && value.integer?
		unitize(value)
	else
		nil
	end
end