Class: Harvesting::Models::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/harvesting/models/base.rb

Direct Known Subclasses

Client, Contact, Project, Projects, Task, Tasks, TimeEntries, TimeEntry, User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs, opts = {}) ⇒ Base

Returns a new instance of Base.



7
8
9
10
# File 'lib/harvesting/models/base.rb', line 7

def initialize(attrs, opts = {})
  @attributes = attrs.dup
  @client = opts[:client] || Harvesting::Client.new(opts)
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



4
5
6
# File 'lib/harvesting/models/base.rb', line 4

def attributes
  @attributes
end

#clientObject (readonly)

Returns the value of attribute client.



5
6
7
# File 'lib/harvesting/models/base.rb', line 5

def client
  @client
end

Class Method Details

.attributed(*attribute_names) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/harvesting/models/base.rb', line 12

def self.attributed(*attribute_names)
  attribute_names.each do |attribute_name|
    Harvesting::Models::Base.send :define_method, attribute_name.to_s do
      @attributes[__method__.to_s]
    end
  end
end

Instance Method Details

#createObject



24
25
26
# File 'lib/harvesting/models/base.rb', line 24

def create
  @client.create(self)
end

#saveObject



20
21
22
# File 'lib/harvesting/models/base.rb', line 20

def save
  id.nil? ? create : update
end

#to_hashObject



32
33
34
# File 'lib/harvesting/models/base.rb', line 32

def to_hash
  @attributes
end

#updateObject



28
29
30
# File 'lib/harvesting/models/base.rb', line 28

def update
  @client.update(self)
end