Class: Rant

Inherits:
Object
  • Object
show all
Includes:
Data
Defined in:
lib/rant.rb,
lib/rant/data.rb,
lib/rant/generator.rb

Defined Under Namespace

Modules: Chars, Data, Silly Classes: GuardFailure, Property, TooManyTries

Constant Summary collapse

INTEGER_MAX =

wanna avoid going into Bignum when calling range with these.

(2**(0.size * 8 -2) -1) / 2
INTEGER_MIN =
-(INTEGER_MAX)

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Data

#email, #password

Constructor Details

#initializeRant

Returns a new instance of Rant.



78
79
80
# File 'lib/rant/generator.rb', line 78

def initialize
  reset
end

Class Attribute Details

.default_sizeObject



10
11
12
# File 'lib/rant/generator.rb', line 10

def default_size
  @default_size || 6
end

Instance Attribute Details

#classifiersObject

Returns the value of attribute classifiers.



76
77
78
# File 'lib/rant/generator.rb', line 76

def classifiers
  @classifiers
end

Class Method Details

.genObject



14
15
16
# File 'lib/rant/generator.rb', line 14

def gen
  self.singleton
end

.singletonObject



5
6
7
8
# File 'lib/rant/generator.rb', line 5

def singleton
  @singleton ||= Rant.new
  @singleton
end

Instance Method Details

#array(*freq_pairs) ⇒ Object



191
192
193
194
195
# File 'lib/rant/generator.rb', line 191

def array(*freq_pairs)
  acc = []
  self.size.times { acc << freq(*freq_pairs) }
  acc
end

#boolObject



161
162
163
# File 'lib/rant/generator.rb', line 161

def bool
  range(0,1) == 0 ? true : false
end

#branch(*gens) ⇒ Object



149
150
151
# File 'lib/rant/generator.rb', line 149

def branch(*gens)
  self.call(choose(*gens))
end

#call(gen, *args) ⇒ Object



135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/rant/generator.rb', line 135

def call(gen,*args)
  case gen
  when Symbol
    return self.send(gen,*args)
  when Array
    raise "empty array" if gen.empty?
    return self.send(gen[0],*gen[1..-1])
  when Proc
    return self.instance_eval(&gen)
  else
    raise "don't know how to call type: #{gen}"
  end
end

#choose(*vals) ⇒ Object



153
154
155
# File 'lib/rant/generator.rb', line 153

def choose(*vals)
  vals[range(0,vals.length-1)]
end

#classify(classifier) ⇒ Object



87
88
89
# File 'lib/rant/generator.rb', line 87

def classify(classifier)
  @classifiers[classifier] += 1
end

#each(n, limit = 10, &block) ⇒ Object

limit attempts to 10 times of how many things we want to generate



39
40
41
# File 'lib/rant/generator.rb', line 39

def each(n,limit=10,&block)
  generate(n,limit,block)
end

#floatObject



125
126
127
# File 'lib/rant/generator.rb', line 125

def float
  rand
end

#freq(*pairs) ⇒ Object

Raises:

  • (RuntimeError)


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/rant/generator.rb', line 165

def freq(*pairs)
  pairs = pairs.map do |pair|
    case pair
    when Symbol, String, Proc
      [1,pair]
    when Array
      unless pair.first.is_a?(Integer)
        [1] + pair
      else
        pair
      end
    end
  end
  total = pairs.inject(0) { |sum,p| sum + p.first }
  raise(RuntimeError, "Illegal frequency:#{pairs.inspect}") if total == 0
  pos = range(1,total)
  pairs.each do |p|
    weight, gen, *args = p
    if pos <= p[0]
      return self.call(gen,*args)
    else
      pos -= weight
    end
  end
end

#generate(n, limit_arg, gen_block, &handler) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rant/generator.rb', line 57

def generate(n,limit_arg,gen_block,&handler)
  limit = n * limit_arg
  nfailed = 0
  nsuccess = 0
  while nsuccess < n
    raise TooManyTries.new(limit_arg*n,nfailed) if limit < 0
    begin
      val = self.instance_eval(&gen_block)
    rescue GuardFailure
      nfailed += 1
      limit -= 1
      next
    end
    nsuccess += 1
    limit -= 1
    handler.call(val) if handler
  end
end

#guard(test) ⇒ Object

Raises:



91
92
93
# File 'lib/rant/generator.rb', line 91

def guard(test)
  raise GuardFailure.new unless test
end

#integer(n = nil) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/rant/generator.rb', line 111

def integer(n=nil)
  if n
    raise "n should be greater than zero" if n < 0
    hi, lo = n, -n
  else
    hi, lo = INTEGER_MAX, INTEGER_MIN
  end
  range(lo,hi)
end

#literal(value) ⇒ Object



157
158
159
# File 'lib/rant/generator.rb', line 157

def literal(value)
  value
end

#map(n, limit = 10, &block) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/rant/generator.rb', line 43

def map(n,limit=10,&block)
  acc = []
  generate(n,limit,block) do |val|
    acc << val
  end
  acc
end

#positive_integerObject



121
122
123
# File 'lib/rant/generator.rb', line 121

def positive_integer
  range(0)
end

#range(lo = nil, hi = nil) ⇒ Object



129
130
131
132
133
# File 'lib/rant/generator.rb', line 129

def range(lo=nil,hi=nil)
  lo ||= INTEGER_MIN
  hi ||= INTEGER_MAX
  rand(hi+1-lo) + lo
end

#resetObject



82
83
84
85
# File 'lib/rant/generator.rb', line 82

def reset
  @size = nil
  @classifiers = Hash.new(0)
end

#sizeObject



95
96
97
# File 'lib/rant/generator.rb', line 95

def size
  @size || Rant.default_size
end

#sized(n, &block) ⇒ Object



99
100
101
102
103
104
105
106
# File 'lib/rant/generator.rb', line 99

def sized(n,&block)
  raise "size needs to be greater than zero" if n < 0
  old_size = @size
  @size = n
  r = self.instance_eval(&block)
  @size = old_size
  return r
end

#string(char_class = :print) ⇒ Object



243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'lib/rant/generator.rb', line 243

def string(char_class=:print)
  chars = case char_class
          when Regexp
            Chars.of(char_class)
          when Symbol
            Chars::CLASSES[char_class]
          end
  raise "bad arg" unless chars
  str = ""
  size.times do
    str << choose(*chars)
  end
  str
end

#value(limit = 10, &block) ⇒ Object



51
52
53
54
55
# File 'lib/rant/generator.rb', line 51

def value(limit=10,&block)
  generate(1,limit,block) do |val|
    return val
  end
end