Class: AdGear::Base

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Base

Returns a new instance of Base.



3
4
5
6
7
8
# File 'lib/ad_gear/base.rb', line 3

def initialize(params={})
  super({})
  params.each do |key, value|
    send("#{key}=", value)
  end
end

Class Method Details

.belongs_to(*attributes) ⇒ Object

Declares a relationship where we store the ID, but accept and/or return the full object.

Examples:

class AdGear::AdSpot < AdGear::Base
  belongs_to :format
  belongs_to :bookable, :polymorphic => true
  belongs_to :web_campaign, :as => :campaign
end

include AdGear
AdSpot.new(:format => Format.find(1), :bookable => Publisher.find(2), :web_campaign => WebCampaign.find(3)).attributes
  #=> {"format_id" => 1, "bookable_id" => 2, "bookable_type" => "Publisher", "campaign_id" => 3}


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/ad_gear/base.rb', line 22

def self.belongs_to(*attributes)
  options = attributes.extract_options!
  attributes.each do |attribute|
    attribute_name = attribute.to_s
    belongs_to_associations[attribute_name] = options.reverse_merge(:as => attribute_name)

    if options[:polymorphic] then
      polymorphic_belongs_to_attribute_writer_for(attribute_name)
      polymorphic_belongs_to_attribute_reader_for(attribute_name)
    else
      belongs_to_attribute_writer_for(attribute_name)
      belongs_to_attribute_reader_for(attribute_name)
    end
  end
end

.belongs_to_associationsObject

:nodoc:



38
39
40
# File 'lib/ad_gear/base.rb', line 38

def self.belongs_to_associations #:nodoc:
  @belongs_to_associations ||= {}
end

.has_many(*collections) ⇒ Object

Defines a managed sub-collection. Elements managed through a #has_many are ready for use by AttributeFu or ActiveRecord’s 2.3 nested_attributes.

Examples:

class AdGear::AdUnit < AdGear::Base
  has_many :ad_unit_files, :ad_unit_clicks
  has_many :ad_unit_interactions, :ad_unit_variables
end

AdGear::AdUnit.new(:ad_unit_files => [AdUnitFile.new(...)]).to_xml
<ad-unit>
  <ad-unit-file-attributes>
    <new>
      <n0>
        ...
      </n0>
    </new>
    <!-- If there were "old" records, they'd be here
  </ad-unit-file-attributes>
</ad-unit>


62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ad_gear/base.rb', line 62

def self.has_many(*collections)
  collections.flatten.compact.each do |collection|
    collection_name = collection.to_s

    # Remember what collections we are managing, for use in #to_xml
    has_many_collections << collection_name

    # Define a getter for the collection that transforms a plain Array into
    # a HasManyArray which knows about new and old records
    define_method(collection_name) do
      arr = @attributes[collection_name]
      return arr if arr.kind_of?(AdGear::HasManyArray)
      @attributes[collection_name] = AdGear::HasManyArray.new(collection_name.to_sym, AdGear.const_get(collection_name.classify), arr)
    end

    define_method("#{collection_name}=") do |arr|
      @attributes[collection_name] = if arr.kind_of?(AdGear::HasManyArray) then
                                       arr
                                     else
                                       AdGear::HasManyArray.new(collection_name.to_sym, AdGear.const_get(collection_name.classify), arr)
                                     end
    end
  end
end

.has_many_collectionsObject

:nodoc:



87
88
89
# File 'lib/ad_gear/base.rb', line 87

def self.has_many_collections #:nodoc:
  @has_many_collections ||= []
end

.ignorable_attributes(*attributes) ⇒ Object

Defines a list of attributes that should be ignored and not sent back to the server.

Examples:

class AdGear::Site < AdGear::Base
  ignorable_attributes :embed_code
end

AdGear::Site.find(13).to_xml
<site>
  ...
  <!-- no embed-code element -->
</site>


103
104
105
106
107
# File 'lib/ad_gear/base.rb', line 103

def self.ignorable_attributes(*attributes)
  @ignorable_attributes ||= []
  @ignorable_attributes += attributes.flatten.compact.map {|name| name.to_s}
  @ignorable_attributes
end

Instance Method Details

#encode(options = {}) ⇒ Object

Delegates to #to_xml, as this is the simplest thing that could possibly work



127
128
129
# File 'lib/ad_gear/base.rb', line 127

def encode(options={}) #:nodoc:
  self.to_xml
end

#to_xml(options = {}) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/ad_gear/base.rb', line 109

def to_xml(options={})
  self.class.has_many_collections.each do |name|
    send(name) # convert the @attributes value into a HasManyArray
  end

  hash = if self.class.ignorable_attributes.empty?
           @attributes
         else
           returning(@attributes.dup) do |hash|
             self.class.ignorable_attributes.each do |attr_name|
               hash.delete(attr_name)
             end
           end
         end
  hash.to_xml({:root => self.class.element_name}.merge(options))
end