arusarka-dynamic-active-resource 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/History.txt +3 -5
- data/README +89 -4
- metadata +1 -1
data/History.txt
CHANGED
data/README
CHANGED
@@ -1,10 +1,95 @@
|
|
1
|
-
=
|
1
|
+
= dynamic-active-resource
|
2
2
|
|
3
|
-
|
3
|
+
http://github.com/arusarka/dynamic-active-resource
|
4
4
|
|
5
5
|
== DESCRIPTION:
|
6
6
|
|
7
|
-
|
7
|
+
ActiveResource makes connecting to rest resources very easy. However it has one problem
|
8
|
+
and a big one at that. If you try setting the authentication credentials or the site or
|
9
|
+
collection name, element name for the class for the second time it doesn't work. E.g.
|
10
|
+
|
11
|
+
class Person < ActiveResource::Base
|
12
|
+
self.site = 'http://localhost:9090/'
|
13
|
+
end
|
14
|
+
|
15
|
+
After sometime you change it to
|
16
|
+
|
17
|
+
Person.site = 'https://org-server/my_proj/'
|
18
|
+
Person.user = 'admin'
|
19
|
+
Person.password = 'secret'
|
20
|
+
|
21
|
+
Then you do
|
22
|
+
|
23
|
+
Person.find(:all) => It bombs
|
24
|
+
|
25
|
+
This class provides a mechanism by which you can get rid of this problem. Extend DynamicActiveResource::Base
|
26
|
+
class in the actual class itself. Do not extend the extended class from ActiveResource::Base.
|
27
|
+
|
28
|
+
E.g.
|
29
|
+
|
30
|
+
class Person < DynamicActiveResource::Base
|
31
|
+
end
|
32
|
+
|
33
|
+
set the credentials
|
34
|
+
|
35
|
+
Person.site = 'http://localhost:8080'
|
36
|
+
Person.user = 'foo'
|
37
|
+
Person.password = 'bar'
|
38
|
+
|
39
|
+
Thats it. Now create some objects
|
40
|
+
|
41
|
+
asur = Person.new(:name => 'Asur', :job => 'fooling around', :status => 'Single and ready 2 mingle')
|
42
|
+
asur.save
|
43
|
+
|
44
|
+
Now change the class attributes
|
45
|
+
|
46
|
+
Person.site = 'https://org-server/mingle'
|
47
|
+
Person.collection_name = 'boring_people'
|
48
|
+
|
49
|
+
Now instantiate an object
|
50
|
+
|
51
|
+
rakhshas = Person.new(:name => 'Rakhshas', :job => 'eating people', :status => 'just woke up and hungry')
|
52
|
+
rakhshas.save => Voila !!!!!!! it works
|
53
|
+
|
54
|
+
CUSTOMIZATIONS
|
55
|
+
--------------
|
56
|
+
|
57
|
+
No amount of wrapping can provide very detailed customizations. Either you have a lot of methods
|
58
|
+
that are not being used or there is hardly anything at all. To oversome this problem this module
|
59
|
+
was written to provide only those methods which are common to most active resource objects.
|
60
|
+
However if you want to have a little more control over your active resource objects its very easy.
|
61
|
+
Here's how you would do it normally
|
62
|
+
|
63
|
+
class Person < ActiveResource::Base
|
64
|
+
def self.count
|
65
|
+
find(:all).size
|
66
|
+
end
|
67
|
+
|
68
|
+
def occupation
|
69
|
+
return job if job
|
70
|
+
'Unemployed'
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
To do the same thing, here's how you do it using this library
|
75
|
+
|
76
|
+
class Person < DynamicActiveResource::Base
|
77
|
+
module ClassMethods
|
78
|
+
def count
|
79
|
+
find(:all).size
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
module InstanceMethods
|
84
|
+
def occupation
|
85
|
+
return job if job
|
86
|
+
'Unemployed'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
The instance methods will be available as instance methods in the objects created, class methods
|
92
|
+
will be available as class methods in the class of the object.
|
8
93
|
|
9
94
|
== FEATURES/PROBLEMS:
|
10
95
|
|
@@ -26,7 +111,7 @@ FIX (describe your package)
|
|
26
111
|
|
27
112
|
(The MIT License)
|
28
113
|
|
29
|
-
Copyright (c) 2009
|
114
|
+
Copyright (c) 2009 Arusarka Haldar
|
30
115
|
|
31
116
|
Permission is hereby granted, free of charge, to any person obtaining
|
32
117
|
a copy of this software and associated documentation files (the
|