Class: RandomPerson::Facade

Inherits:
Object
  • Object
show all
Defined in:
lib/randomperson.rb

Overview

Wrapper for convenience.

Constant Summary collapse

DEFAULT_gen_new_BLOCK =

The default default error block :)

->(error) {
  warn error.message
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#last_demo_nameObject

Returns the value of attribute last_demo_name.



96
97
98
# File 'lib/randomperson.rb', line 96

def last_demo_name
  @last_demo_name
end

Class Method Details

.default_error_blockObject

This holds the default error block



162
163
164
# File 'lib/randomperson.rb', line 162

def self.default_error_block
  @default_gen_new_error_block ||= DEFAULT_gen_new_BLOCK
end

.default_error_block=(block) ⇒ Object

Set the default error block

Parameters:

  • block (#call)


169
170
171
# File 'lib/randomperson.rb', line 169

def self.default_error_block=( block )
  @default_gen_new_error_block = block
end

Instance Method Details

#clearObject Also known as: reset

start afresh



87
88
89
90
91
92
# File 'lib/randomperson.rb', line 87

def clear
  @demos = nil
  @generators = nil
  @person = nil
  @last_demo_name = nil
end

#demographic(name = nil, opts = {}) ⇒ Object

A little factory

Parameters:

  • name (String, Symbol, Integer) (defaults to: nil)

    The key for retrieving the demographic.

  • opts (Hash) (defaults to: {})

    Option hash to pass to Demographic.new

See Also:



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/randomperson.rb', line 73

def demographic( name=nil, opts={} )
  if name.kind_of? Hash
    opts = name
    name = nil
  end
  name = demographics.length.to_s if name.nil?
  demo = Demographic.new name, opts
  demographics[name] = demo
  generators.delete name # just in case it already exists
  demo
end

#demographicsHash{String => RandomPerson::Demographic}

Returns:



52
53
54
# File 'lib/randomperson.rb', line 52

def demographics
  @demos ||= DemoHash.new
end

#gen_new(demo_name = nil, &block) ⇒ Object

If not given a demographic’s name then the *last demographic defined* will be used. If there is no demographic already defined a new one will be created. If a key is given but does not exist then the supplied block will be called. If no block is given an exception will be raised.

Parameters:

  • demo_name (String, Symbol, Integer) (defaults to: nil)

    The key for retrieving the demographic.

  • block (#call)

    Default for when a key is given that does not exist.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/randomperson.rb', line 177

def gen_new( demo_name=nil, &block )
  block = self.class.default_error_block if block.nil?
  demo_name, demo = if demo_name.nil?
    if demographics.nil? || demographics.empty?
      generate_demo
    else
      demographics.to_a.last # this produces a 2 dimensional array
    end
  else
    if ds = demographics.assoc(demo_name)
      ds
    else
      fail "That demographic does not exist!" 
    end
  end

  @last_demo_name = demo_name
  unless generators.has_key? demo_name
    generators[demo_name] = Generator.make_generator( demo )
  end

  @person = generators[demo_name].call 
    
  [@person, demo_name]
rescue => error
  block.call(error)
end

#generate(demo_name = nil, &block) ⇒ RandomPerson::Demographic

Generate a new demographic

Parameters:

  • demo_name (#to_s) (defaults to: nil)

    A name for the demographic.

  • block (#call)

Returns:



136
137
138
139
# File 'lib/randomperson.rb', line 136

def generate( demo_name=nil, &block )
  ds = gen_new( demo_name, &block )
  ds.nil? ? nil : ds.first
end

#generate_demoString, RandomPerson::Demographic

For when a demo isn’t given but you still need one.

Returns:



144
145
146
147
148
149
150
151
152
# File 'lib/randomperson.rb', line 144

def generate_demo
  Demographic.load
  yesses = %w{prefix suffix female -male last}.map {|word|
    Demographic.classify_true(word).to_a.sample
  }
  demo = self.demographic
  demo.require_and_add yesses
  [demo.name, demo]
end

#generatorsObject

class instance variable to keep track of generators



64
65
66
# File 'lib/randomperson.rb', line 64

def generators
  @generators ||= {}
end

#loaded_classesHash

Returns:

  • (Hash)


58
59
60
# File 'lib/randomperson.rb', line 58

def loaded_classes
  demographics.loaded_classes
end

#person(demo_name = nil, &block) ⇒ RandomPerson::Person

The last person generated. If a demographic name is given that is different to the last then a new person is generated. If no name is given then the last is used.

Parameters:

  • demo_name (String, Symbol, Integer) (defaults to: nil)

    The key for retrieving the demographic.

  • block (#call)

    Error handler for when a key is given that does not exist.

Returns:



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/randomperson.rb', line 104

def person( demo_name=nil, &block )
  person, last_demo_name = 
    if demo_name.nil? 
      if @person.nil?
        # either generate a new one
        gen_new( demo_name, &block ) # gen a new person and get back demo name
      else
        #  get last one
        [@person, @last_demo_name]
      end
    else # demo name given
      if demographics.has_key? demo_name
        if demo_name == @last_demo_name
          [@person, @last_demo_name]
        else
          gen_new( demo_name, &block )
        end
      else
        gen_new( demo_name, &block )
      end
    end
    return nil if person.nil?
    @person, @last_demo_name = [person, last_demo_name]

  @person
end