Class: AdGear::Base
- Inherits:
-
ActiveResource::Base
- Object
- ActiveResource::Base
- AdGear::Base
- Defined in:
- lib/ad_gear/base.rb
Direct Known Subclasses
AdSpot, AdSpotMembership, AdUnit, AdUnitClick, AdUnitFile, AdUnitInteraction, AdUnitVariable, Advertiser, Click, File, Format, Interaction, PlacementMembership, PlacementRule, Publisher, Site, Template, Upload, Variable, WebCampaign, WebPlacement
Class Method Summary collapse
-
.belongs_to(*attributes) ⇒ Object
Declares a relationship where we store the ID, but accept and/or return the full object.
-
.belongs_to_associations ⇒ Object
:nodoc:.
-
.has_many(*collections) ⇒ Object
Defines a managed sub-collection.
-
.has_many_collections ⇒ Object
:nodoc:.
-
.ignorable_attributes(*attributes) ⇒ Object
Defines a list of attributes that should be ignored and not sent back to the server.
Instance Method Summary collapse
-
#encode(options = {}) ⇒ Object
Delegates to #to_xml, as this is the simplest thing that could possibly work.
-
#initialize(params = {}) ⇒ Base
constructor
A new instance of Base.
- #to_xml(options = {}) ⇒ Object
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.
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) = attributes. attributes.each do |attribute| attribute_name = attribute.to_s belongs_to_associations[attribute_name] = .reverse_merge(:as => attribute_name) if [: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_associations ⇒ Object
: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.
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_collections ⇒ Object
: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.
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(={}) #: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(={}) 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()) end |