blob: 11064454d769d1525c34098e2b60018a389adb6a (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
require 'active_resource/connection'
module ActiveResource
class Base
class << self
def site=(site)
@@site = site.is_a?(URI) ? site : URI.parse(site)
end
def site
@@site
end
def connection(refresh = false)
@connection = Connection.new(site) if refresh || @connection.nil?
@connection
end
def element_name
self.to_s.underscore
end
def collection_name
element_name.pluralize
end
def element_path(id)
"/#{collection_name}/#{id}.xml"
end
def collection_path
"/#{collection_name}.xml"
end
def find(*arguments)
scope = arguments.slice!(0)
case scope
when Fixnum
# { :person => person1 }
new(connection.get(element_path(scope)).values.first)
when :all
# { :people => { :person => [ person1, person2 ] } }
connection.get(collection_path).values.first.values.first.collect { |element| new(element) }
when :first
find(:all, *arguments).first
end
end
end
attr_accessor :attributes
def initialize(attributes = {})
@attributes = attributes
end
def id
attributes["id"]
end
def id=(id)
attributes["id"] = id
end
def save
update
end
def destroy
connection.delete(self.class.element_path(id))
end
def to_xml
attributes.to_xml(:root => self.class.element_name)
end
protected
def connection(refresh = false)
self.class.connection(refresh)
end
def update
connection.put(self.class.element_path(id), to_xml)
end
def method_missing(method_symbol, *arguments)
method_name = method_symbol.to_s
case method_name.last
when "="
attributes[method_name.first(-1)] = arguments.first
when "?"
# TODO
else
attributes[method_name] || super
end
end
end
end
|