Class: Fafactory

Inherits:
ActiveResource::Base
  • Object
show all
Defined in:
lib/fafactory.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(service, model, defaults) ⇒ Fafactory

Set up a new fafactory with the given defaults



32
33
34
35
36
# File 'lib/fafactory.rb', line 32

def initialize(service, model, defaults)
  @service = service
  @model = Fafactory.camelize_model(model)
  @defaults = defaults
end

Class Method Details

.clear_definitionsObject

Clears definitions (mainly useful for testing)



69
70
71
# File 'lib/fafactory.rb', line 69

def self.clear_definitions
  @definitions = {} if defined? @definitions
end

.create(service, model, options) ⇒ Object

Constructs a new instance based on the given service and model



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/fafactory.rb', line 17

def self.create(service, model, options)
  model = camelize_model(model)
  
  # define a blank definition if we don't already have a definition
  @definitions ||= {}
  if @definitions[service] == nil ||
      @definitions[service][model] == nil
    
    define(service, model, {})
  end
  
  @definitions[service][model].create(options)
end

.create_instance(service, model, data) ⇒ Object

Creates a new instance of a remote model using the data provided.



45
46
47
48
49
50
51
# File 'lib/fafactory.rb', line 45

def self.create_instance(service, model, data)
  Fafactory.configure_site(service)
  result = Fafactory.post :create_instance, nil, 
      { :model => camelize_model(model), :data => data }.to_xml
  
  return Hash.from_xml(result.body)
end

.define(service, model, defaults) ⇒ Object

Defines a new object definition



6
7
8
9
10
11
12
13
14
# File 'lib/fafactory.rb', line 6

def self.define(service, model, defaults)
  @definitions ||= {}
  
  model = camelize_model(model)
  
  factory = Fafactory.new(service, model, defaults)
  @definitions[service] ||= {}
  @definitions[service][model] = factory
end

.find(service, model, id) ⇒ Object

Loads an instance of a remote model based on the id given



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

def self.find(service, model, id)
  Fafactory.configure_site(service)
  Fafactory.get(:find, { :model => camelize_model(model), :id => id })
end

.purge(*services) ⇒ Object



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

def self.purge(*services)
  services.each do |service|
    Fafactory.configure_site(service)
    Fafactory.delete(:purge)
  end
  
  nil
end

Instance Method Details

#create(options) ⇒ Object

Creates a new remote instance based on the given parameters



39
40
41
42
# File 'lib/fafactory.rb', line 39

def create(options)
  Fafactory.create_instance(@service, @model,
      options.merge(@defaults))[Fafactory.underscore_model(@model)]
end