Class: HTTParrot::ResponseFactory

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

Class Method Summary collapse

Class Method Details

.build(factory_class, new_options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
# File 'lib/httparrot/response_factory.rb', line 19

def self.build(factory_class, new_options={})
  raise error_for_missing(factory_class) if @factory_classes[factory_class].nil?
  object = @factory_classes[factory_class].call

  new_options.each do |k, v|
    object.send("#{k}=", v) 
  end

  return Marshal.load(Marshal.dump(object)) 
end

.clear!Object



7
8
9
# File 'lib/httparrot/response_factory.rb', line 7

def self.clear!
  @factory_classes = {}
end

.collection_of(factory_class, number, new_options = {}) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/httparrot/response_factory.rb', line 30

def self.collection_of(factory_class, number, new_options={})
  collection = []

  number.times do 
    collection << build(factory_class, new_options)
  end

  return collection
end

.define(factory_class, &block) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/httparrot/response_factory.rb', line 11

def self.define(factory_class, &block)
  raise error_no_block(factory_class) if block.nil?
  @factory_classes ||= {} 
  warn_factory_exists(factory_class) if @factory_classes.keys.include?(factory_class)
  default_object = HTTParrot::Widget.new(:_class => "Widget::#{factory_class.to_s.camelize}")
  @factory_classes[factory_class] = lambda{ block.call(default_object); default_object } 
end

.one_of(*choices) ⇒ Object



40
41
42
43
44
# File 'lib/httparrot/response_factory.rb', line 40

def self.one_of(*choices)
  choices = [choices].flatten
  warn_no_choices if choices.size <= 0
  choices[rand(choices.size)]
end