Class: Harvest::BaseModel

Inherits:
Object
  • Object
show all
Defined in:
lib/harvest/base_model.rb

Overview

The parent class of all Harvest models. Contains useful inheritable methods

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ BaseModel

Initializes the model. You can pass the attributes as a hash in a ActiveRecord-like style

Examples

client = Harvest::Client.new(:name => 'John Doe')
client.name # returns 'John Doe'


10
11
12
# File 'lib/harvest/base_model.rb', line 10

def initialize(attributes = {})
  self.attributes = attributes
end

Class Method Details

.api_path(path = nil) ⇒ void

This method returns an undefined value.

This sets the API path so the API collections can use them in an agnostic way



68
69
70
# File 'lib/harvest/base_model.rb', line 68

def api_path(path = nil)
  @path ||= path
end

Instance Method Details

#==(other) ⇒ Boolean

Checks the equality of another model based on the id

Examples

client1 = Harvest::Client.new(:id => 1)
client2 = Harvest::Client.new(:id => 1)
client3 = Harvest::Client.new(:id => 2)

client1 == client2 # returns true
client1 == client3 # returns false

Returns:

  • (Boolean)


38
39
40
# File 'lib/harvest/base_model.rb', line 38

def ==(other)
  id == other.id
end

#attributes=(attributes) ⇒ void

This method returns an undefined value.

Given a hash, sets the corresponding attributes

Examples

client = Harvest::Client.new(:name => 'John Doe')
client.name # returns 'John Doe'
client.attributes = {:name => 'Terry Vaughn'}
client.name # returns 'Terry Vaughn'


23
24
25
# File 'lib/harvest/base_model.rb', line 23

def attributes=(attributes)
  attributes.each {|k,v| send("#{k}=", v)}
end

#to_iFixnum

Returns the id of the model

Examples

client = Harvest::Client.new(:id => 1)
client.to_i # returns 1

Returns:

  • (Fixnum)


49
50
51
# File 'lib/harvest/base_model.rb', line 49

def to_i
  id
end

#to_xmlString

Builds the XML of the model. Primarily used to interact with the Harvest API.

Returns:

  • (String)


56
57
58
59
60
61
62
63
# File 'lib/harvest/base_model.rb', line 56

def to_xml
  builder = Builder::XmlMarkup.new
  builder.tag!(self.class.tag_name) do |c|
    self.class.elements.each do |f|
      c.tag!(f.tag, send(f.name)) if send(f.name)
    end
  end
end