Class: OpenX::Services::Base

Inherits:
Object
  • Object
show all
Extended by:
Persistance::ClassMethods
Includes:
Comparable, Persistance::InstanceMethods
Defined in:
lib/openx/services/base.rb

Direct Known Subclasses

Advertiser, Agency, Banner, Campaign, Channel, Publisher, Zone

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Persistance::ClassMethods

create!, destroy, find

Methods included from Persistance::InstanceMethods

#destroy, #save!

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



72
73
74
75
# File 'lib/openx/services/base.rb', line 72

def initialize(params = {})
  @id = nil
  params.each { |k,v| send(:"#{k}=", v) }
end

Class Attribute Details

.connectionObject

Returns the current connection (session)



40
41
42
43
# File 'lib/openx/services/base.rb', line 40

def connection
  @connection = nil unless defined?(@connection)
  @connection || OpenX::Services.default_connection
end

.createObject

Returns the value of attribute create.



11
12
13
# File 'lib/openx/services/base.rb', line 11

def create
  @create
end

.deleteObject

Returns the value of attribute delete.



11
12
13
# File 'lib/openx/services/base.rb', line 11

def delete
  @delete
end

.find_allObject

Returns the value of attribute find_all.



11
12
13
# File 'lib/openx/services/base.rb', line 11

def find_all
  @find_all
end

.find_oneObject

Returns the value of attribute find_one.



11
12
13
# File 'lib/openx/services/base.rb', line 11

def find_one
  @find_one
end

.translationsObject

Returns the value of attribute translations.



9
10
11
# File 'lib/openx/services/base.rb', line 9

def translations
  @translations
end

.updateObject

Returns the value of attribute update.



11
12
13
# File 'lib/openx/services/base.rb', line 11

def update
  @update
end

Class Method Details

.establish_connection(conn) ⇒ Object

Establishes a custom connection. Example:

class MyBanner < OpenX::Services::Banner
  establish_connection 'url' => 'http://custom.host/openx', 'user' => 'admin', 'password' => 'password'
end


18
19
20
21
# File 'lib/openx/services/base.rb', line 18

def establish_connection(conn)
  conn = OpenX::Services.establish_connection(conn) if conn.is_a?(Hash)
  self.connection = conn
end

.has_one(*things) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/openx/services/base.rb', line 52

def has_one(*things)
  things.each do |thing|
    attr_writer :"#{thing}"
    define_method(:"#{thing}") do
      klass = thing.to_s.capitalize.gsub(/_[a-z]/) { |m| m[1].chr.upcase }
      klass = OpenX::Services.const_get(:"#{klass}")
      klass.find(send("#{thing}_id"))
    end
  end
end

.openx_accessor(accessor_map) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/openx/services/base.rb', line 63

def openx_accessor(accessor_map)
  @translations ||= {}
  @translations = accessor_map.merge(@translations)
  accessor_map.each do |ruby,openx|
    attr_accessor :"#{ruby}"
  end
end

.remoteObject

Remote API proxy. Example:

OpenX::Services::Agency.remote.call 'ox.addAgency', ...


48
49
50
# File 'lib/openx/services/base.rb', line 48

def remote
  connection.remote
end

.with_connection(temporary) ⇒ Object

Execute a block using a custom connection. Example:

custom_conn = OpenX::Services.establish_connection({ ... })

OpenX::Services::Agency.with_connection(custom_conn) do
  OpenX::Services::Agency.find(:all)
end


29
30
31
32
33
34
35
36
37
# File 'lib/openx/services/base.rb', line 29

def with_connection(temporary)
  current = @connection
  begin
    establish_connection(temporary)
    yield
  ensure
    establish_connection(current)
  end
end

Instance Method Details

#<=>(other) ⇒ Object



81
82
83
# File 'lib/openx/services/base.rb', line 81

def <=>(other)
  self.id <=> other.id
end

#new_record?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/openx/services/base.rb', line 77

def new_record?
  @id.nil?
end