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:



60
61
62
63
64
65
66
67
68
# File 'lib/agree2/base.rb', line 60

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.



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

def container
  @container
end

#userObject

Returns the value of attribute user.



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

def user
  @user
end

Class Method Details

.attr_read_only(*attributes) ⇒ Object

:nodoc:



21
22
23
24
25
26
27
# File 'lib/agree2/base.rb', line 21

def attr_read_only(*attributes) #:nodoc:
  attributes.map!{|a|a.to_sym}
  write_inheritable_attribute("read_only_attributes", 
    Set.new(attributes) + 
    (read_only_attributes || []))
  protected *attributes.collect{|a| "#{a.to_s}=".to_sym }
end

.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:



42
43
44
# File 'lib/agree2/base.rb', line 42

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

.collection_pathObject

:nodoc:



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

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

.get(container, id) ⇒ Object

Gets an instance of a resource



51
52
53
54
# File 'lib/agree2/base.rb', line 51

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:



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

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

.read_only_attributesObject

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



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

def read_only_attributes # :nodoc:
  read_inheritable_attribute("read_only_attributes")
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:



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

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



110
111
112
113
114
115
116
# File 'lib/agree2/base.rb', line 110

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



81
82
83
# File 'lib/agree2/base.rb', line 81

def destroy
  user.delete(path)
end

#new_record?Boolean

Has this record been saved to the server yet?

Returns:

  • (Boolean)


71
72
73
# File 'lib/agree2/base.rb', line 71

def new_record?
  @from_wire!=true
end

#pathObject

Returns the relative path to the object



91
92
93
# File 'lib/agree2/base.rb', line 91

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

#reloadObject

Reloads the object from Agree2’s servers



76
77
78
# File 'lib/agree2/base.rb', line 76

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

#saveObject

Saves the record to the server



101
102
103
104
105
106
107
# File 'lib/agree2/base.rb', line 101

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



96
97
98
# File 'lib/agree2/base.rb', line 96

def to_param #:nodoc:
  id
end

#to_urlObject

Returns the full URL for the object



86
87
88
# File 'lib/agree2/base.rb', line 86

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