Class: Agree2::Base

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

Overview

The superclass of all Agree2 Resource objects.

Direct Known Subclasses

Agreement, Party

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(container, fields = {}) ⇒ Base

:nodoc:



47
48
49
50
51
52
53
54
55
# File 'lib/agree2/base.rb', line 47

def initialize(container,fields={})#:nodoc:
  @container=container
  @user=(container.is_a?(User) ? container : container.user)
  if fields.is_a?(Hash)
    load_attributes(fields)
  else
    load_json(fields)
  end
end

Instance Attribute Details

#containerObject

Returns the value of attribute container.



45
46
47
# File 'lib/agree2/base.rb', line 45

def container
  @container
end

#userObject

Returns the value of attribute user.



45
46
47
# File 'lib/agree2/base.rb', line 45

def user
  @user
end

Class Method Details

.attr_serializable(*attributes) ⇒ Object

:nodoc:



8
9
10
11
12
13
14
# File 'lib/agree2/base.rb', line 8

def attr_serializable(*attributes) #:nodoc:
  attributes.map!{|a|a.to_sym}
  write_inheritable_attribute("serializable_attributes", 
    Set.new(attributes) + 
    (serializable_attributes || []))
  attr_accessor *attributes
end

.collection_nameObject

:nodoc:



29
30
31
# File 'lib/agree2/base.rb', line 29

def collection_name #:nodoc:
  self.to_s.demodulize.tableize
end

.collection_pathObject

:nodoc:



21
22
23
# File 'lib/agree2/base.rb', line 21

def collection_path #:nodoc:
  "/#{collection_name}"
end

.get(container, id) ⇒ Object

Gets an instance of a resource



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

def get(container,id)
  user=(container.is_a?(User) ? container : container.user)
  new( container, user.get(container.path+instance_path(id)))
end

.instance_path(id) ⇒ Object

:nodoc:



25
26
27
# File 'lib/agree2/base.rb', line 25

def instance_path(id) #:nodoc:
  "#{collection_path}/#{id}"
end

.serializable_attributesObject

Returns an array of all the attributes that have been made accessible to mass-assignment.



17
18
19
# File 'lib/agree2/base.rb', line 17

def serializable_attributes # :nodoc:
  read_inheritable_attribute("serializable_attributes")
end

.singular_nameObject

:nodoc:



33
34
35
# File 'lib/agree2/base.rb', line 33

def singular_name #:nodoc:
  self.to_s.demodulize.underscore.singularize
end

Instance Method Details

#attributesObject

Get the primary attributes of an object as a hash



97
98
99
100
101
102
103
# File 'lib/agree2/base.rb', line 97

def attributes
  self.class.serializable_attributes.inject({}) do |h,field|
    value=self.send(field)
    h[field]=value if value
    h
  end
end

#destroyObject

Destroys the object from Agree2’s servers



68
69
70
# File 'lib/agree2/base.rb', line 68

def destroy
  user.delete(path)
end

#new_record?Boolean

Has this record been saved to the server yet?



58
59
60
# File 'lib/agree2/base.rb', line 58

def new_record?
  @from_wire!=true
end

#pathObject

Returns the relative path to the object



78
79
80
# File 'lib/agree2/base.rb', line 78

def path #:nodoc:
  self.container.path+self.class.instance_path(to_param)
end

#reloadObject

Reloads the object from Agree2’s servers



63
64
65
# File 'lib/agree2/base.rb', line 63

def reload
  load_json(user.get(path))
end

#saveObject

Saves the record to the server



88
89
90
91
92
93
94
# File 'lib/agree2/base.rb', line 88

def save
  if new_record?
    load_json(@user.post("#{self.container.path}/#{self.class.collection_name}",attributes_for_save))
  else
    load_json(@user.put("#{path}",attributes_for_save))
  end
end

#to_paramObject

The primary key of the object



83
84
85
# File 'lib/agree2/base.rb', line 83

def to_param #:nodoc:
  id
end

#to_urlObject

Returns the full URL for the object



73
74
75
# File 'lib/agree2/base.rb', line 73

def to_url
  "#{AGREE2_URL}#{path}"
end