Class: Rantly

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

Defined Under Namespace

Modules: Chars, Check, 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

#initializeRantly

Returns a new instance of Rantly


89
90
91
# File 'lib/rantly/generator.rb', line 89

def initialize
  reset
end

Class Attribute Details

.default_sizeObject


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

def default_size
  @default_size || 6
end

Instance Attribute Details

#classifiersObject

Returns the value of attribute classifiers


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

def classifiers
  @classifiers
end

Class Method Details

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


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

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

.genObject


26
27
28
# File 'lib/rantly/generator.rb', line 26

def gen
  self.singleton
end

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


18
19
20
# File 'lib/rantly/generator.rb', line 18

def map(n,limit=10,&block)
  gen.map(n,limit,&block)
end

.singletonObject


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

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

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


22
23
24
# File 'lib/rantly/generator.rb', line 22

def value(limit=10,&block)
  gen.value(limit,&block)
end

Instance Method Details

#array(n = self.size, &block) ⇒ Object


210
211
212
# File 'lib/rantly/generator.rb', line 210

def array(n=self.size,&block)
  n.times.map { self.instance_eval(&block) }
end

#booleanObject


180
181
182
# File 'lib/rantly/generator.rb', line 180

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

#branch(*gens) ⇒ Object


168
169
170
# File 'lib/rantly/generator.rb', line 168

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

#call(gen, *args) ⇒ Object


154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/rantly/generator.rb', line 154

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


172
173
174
# File 'lib/rantly/generator.rb', line 172

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

#classify(classifier) ⇒ Object


98
99
100
# File 'lib/rantly/generator.rb', line 98

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

#dict(n = self.size, &block) ⇒ Object


214
215
216
217
218
219
220
221
# File 'lib/rantly/generator.rb', line 214

def dict(n=self.size,&block)
  h = {}
  each(n) do
    k,v = instance_eval(&block)
    h[k] = v if guard(!h.has_key?(k))
  end
  h
end

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

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


50
51
52
# File 'lib/rantly/generator.rb', line 50

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

#floatObject


144
145
146
# File 'lib/rantly/generator.rb', line 144

def float
  rand
end

#freq(*pairs) ⇒ Object

Raises:

  • (RuntimeError)

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/rantly/generator.rb', line 184

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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/rantly/generator.rb', line 68

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


102
103
104
105
106
107
108
# File 'lib/rantly/generator.rb', line 102

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

#integer(limit = nil) ⇒ Object


126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/rantly/generator.rb', line 126

def integer(limit=nil)
  case limit
  when Range
    hi = limit.end
    lo = limit.begin
  when Integer
    raise "n should be greater than zero" if limit < 0
    hi, lo = limit, -limit
  else
    hi, lo = INTEGER_MAX, INTEGER_MIN
  end
  range(lo,hi)
end

#literal(value) ⇒ Object


176
177
178
# File 'lib/rantly/generator.rb', line 176

def literal(value)
  value
end

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


54
55
56
57
58
59
60
# File 'lib/rantly/generator.rb', line 54

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

#positive_integerObject


140
141
142
# File 'lib/rantly/generator.rb', line 140

def positive_integer
  range(0)
end

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


148
149
150
151
152
# File 'lib/rantly/generator.rb', line 148

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

#resetObject


93
94
95
96
# File 'lib/rantly/generator.rb', line 93

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

#sizeObject


110
111
112
# File 'lib/rantly/generator.rb', line 110

def size
  @size || Rantly.default_size
end

#sized(n, &block) ⇒ Object


114
115
116
117
118
119
120
121
# File 'lib/rantly/generator.rb', line 114

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


266
267
268
269
270
271
272
273
274
275
276
277
278
279
# File 'lib/rantly/generator.rb', line 266

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


62
63
64
65
66
# File 'lib/rantly/generator.rb', line 62

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