kelredd-resourceful 0.5.11 → 0.6.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/resourceful/model/base.rb +7 -57
- data/lib/resourceful/model/embedded_associations.rb +55 -0
- data/lib/resourceful/model/external_associations.rb +68 -0
- data/lib/resourceful/model/findable.rb +44 -0
- data/lib/resourceful/model/json.rb +5 -0
- data/lib/resourceful/model/xml.rb +4 -0
- data/lib/resourceful/version.rb +2 -2
- metadata +5 -2
@@ -16,7 +16,10 @@ module Resourceful
|
|
16
16
|
block = opts.delete(:on_response)
|
17
17
|
(yield set_agent.get(path, opts, &block)).collect{|data| new(data)}
|
18
18
|
end
|
19
|
-
|
19
|
+
def self.find(id, opts, force)
|
20
|
+
raise NotImplementedError, "no find method has been defined"
|
21
|
+
end
|
22
|
+
|
20
23
|
def initialize(data)
|
21
24
|
end
|
22
25
|
|
@@ -66,7 +69,7 @@ module Resourceful
|
|
66
69
|
end
|
67
70
|
|
68
71
|
def self.attribute(name, type, config={})
|
69
|
-
clean_name = name
|
72
|
+
clean_name = cleanup_name(name)
|
70
73
|
add_to_attributes(name.to_s)
|
71
74
|
config ||= {}
|
72
75
|
config[:path] ||= clean_name
|
@@ -102,61 +105,8 @@ module Resourceful
|
|
102
105
|
end
|
103
106
|
end
|
104
107
|
|
105
|
-
def self.
|
106
|
-
|
107
|
-
config ||= {}
|
108
|
-
config[:path] ||= clean_name
|
109
|
-
define_method(name) do
|
110
|
-
instance_variable_get("@#{clean_name}") || \
|
111
|
-
instance_variable_set("@#{clean_name}", \
|
112
|
-
if (c = child(config))
|
113
|
-
config[:klass].constantize.new(c) rescue c
|
114
|
-
else
|
115
|
-
c
|
116
|
-
end
|
117
|
-
)
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def self.has_many(name, config={})
|
122
|
-
clean_name = name.to_s.gsub(/\W/,'')
|
123
|
-
config ||= {}
|
124
|
-
config[:path] ||= clean_name
|
125
|
-
define_method(name) do
|
126
|
-
instance_variable_get("@#{clean_name}") || \
|
127
|
-
instance_variable_set("@#{clean_name}", \
|
128
|
-
if ((c = child(config)) && c.respond_to?(:collect))
|
129
|
-
c.collect do |item|
|
130
|
-
config[:klass].constantize.new(item) rescue item
|
131
|
-
end
|
132
|
-
else
|
133
|
-
c
|
134
|
-
end
|
135
|
-
)
|
136
|
-
end
|
137
|
-
end
|
138
|
-
|
139
|
-
def self.belongs_to(name, config={})
|
140
|
-
clean_name = name.to_s.gsub(/\W/,'')
|
141
|
-
config ||= {}
|
142
|
-
config[:id] ||= "#{clean_name}_id"
|
143
|
-
define_method(name) do
|
144
|
-
instance_variable_get("@#{clean_name}") || \
|
145
|
-
instance_variable_set("@#{clean_name}", \
|
146
|
-
if ((k = config[:klass].constantize) && k.respond_to?(:find))
|
147
|
-
if self.respond_to?(config[:id]) && \
|
148
|
-
(bt_id = self.send(config[:id])) && \
|
149
|
-
!bt_id.nil? && \
|
150
|
-
!bt_id.empty?
|
151
|
-
k.find(bt_id)
|
152
|
-
else
|
153
|
-
nil
|
154
|
-
end
|
155
|
-
else
|
156
|
-
nil
|
157
|
-
end
|
158
|
-
)
|
159
|
-
end
|
108
|
+
def self.cleanup_name(name)
|
109
|
+
name.to_s.gsub(/\W/,'')
|
160
110
|
end
|
161
111
|
|
162
112
|
private
|
@@ -0,0 +1,55 @@
|
|
1
|
+
module Resourceful
|
2
|
+
module Model
|
3
|
+
module EmbeddedAssociations
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def contains_one(name, config={})
|
10
|
+
clean_name = cleanup_name(name)
|
11
|
+
config ||= {}
|
12
|
+
config[:path] ||= clean_name
|
13
|
+
define_method(name) do
|
14
|
+
instance_variable_get("@#{clean_name}") || \
|
15
|
+
instance_variable_set("@#{clean_name}", \
|
16
|
+
if (c = child(config))
|
17
|
+
config[:class].constantize.new(c) rescue c
|
18
|
+
else
|
19
|
+
c
|
20
|
+
end
|
21
|
+
)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def contains_many(name, config={})
|
26
|
+
clean_name = cleanup_name(name)
|
27
|
+
config ||= {}
|
28
|
+
config[:path] ||= clean_name
|
29
|
+
define_method(name) do
|
30
|
+
instance_variable_get("@#{clean_name}") || \
|
31
|
+
instance_variable_set("@#{clean_name}", \
|
32
|
+
if ((c = child(config)) && c.respond_to?(:collect))
|
33
|
+
c.collect do |item|
|
34
|
+
config[:class].constantize.new(item) rescue item
|
35
|
+
end
|
36
|
+
else
|
37
|
+
c
|
38
|
+
end
|
39
|
+
)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
module InstanceMethods
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.included(receiver)
|
49
|
+
receiver.extend ClassMethods
|
50
|
+
receiver.send :include, InstanceMethods
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
module Resourceful
|
2
|
+
module Model
|
3
|
+
module ExternalAssociations
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def has_one(name, config={})
|
10
|
+
has_many(name, config).first
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_many(name, config={})
|
14
|
+
clean_name = cleanup_name(name)
|
15
|
+
config ||= {}
|
16
|
+
raise ArgumentError, "has_many requires a :class option to be specified" unless config[:class]
|
17
|
+
klass = config.delete(:class)
|
18
|
+
force = config.delete(:force) || true
|
19
|
+
define_method(name) do
|
20
|
+
unless klass.to_s.constantize.respond_to?(:find)
|
21
|
+
raise NotImplementedError, "has_many expects #{klass} to be findable (ie mixin the Findable helper)"
|
22
|
+
end
|
23
|
+
instance_variable_get("@#{clean_name}") || \
|
24
|
+
instance_variable_set("@#{clean_name}", \
|
25
|
+
klass.to_s.constantize.find(:all, config, force)
|
26
|
+
)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def belongs_to(name, config={})
|
31
|
+
clean_name = cleanup_name(name)
|
32
|
+
config ||= {}
|
33
|
+
raise ArgumentError, "has_many requires a :class option to be specified" unless config[:class]
|
34
|
+
klass = config.delete(:class)
|
35
|
+
foreign_key = config.delete(:foreign_key) || "#{clean_name}_id"
|
36
|
+
force = config.delete(:force) || true
|
37
|
+
define_method(name) do
|
38
|
+
unless self.respond_to?(foreign_key)
|
39
|
+
raise ArgumentError, "belongs_to requires a '#{foreign_key}' method defined to return the foreign_key"
|
40
|
+
end
|
41
|
+
unless klass.to_s.constantize.respond_to?(:find)
|
42
|
+
raise NotImplementedError, "has_many expects #{klass} to be findable (ie mixin the Findable helper)"
|
43
|
+
end
|
44
|
+
fk = self.send(foreign_key)
|
45
|
+
if fk.nil? || fk.empty?
|
46
|
+
nil
|
47
|
+
else
|
48
|
+
instance_variable_get("@#{clean_name}") || \
|
49
|
+
instance_variable_set("@#{clean_name}", \
|
50
|
+
klass.to_s.constantize.find(fk, config, force)
|
51
|
+
)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
57
|
+
|
58
|
+
module InstanceMethods
|
59
|
+
end
|
60
|
+
|
61
|
+
def self.included(receiver)
|
62
|
+
receiver.extend ClassMethods
|
63
|
+
receiver.send :include, InstanceMethods
|
64
|
+
end
|
65
|
+
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
module Resourceful
|
2
|
+
module Model
|
3
|
+
module Findable
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
def find(id, opts={}, force=true)
|
8
|
+
opts ||= {}
|
9
|
+
opts = findable_default_opts.merge(opts) if respond_to?(:findable_default_opts)
|
10
|
+
case id
|
11
|
+
when :all
|
12
|
+
self.get_collection("#{findable_index}.#{format}", opts, force)
|
13
|
+
when :first
|
14
|
+
self.get_collection("#{findable_index}.#{format}", opts, force).first
|
15
|
+
else
|
16
|
+
self.get("#{findable_index}/#{id}.#{format}", opts, force)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def all
|
21
|
+
find(:all)
|
22
|
+
end
|
23
|
+
def first
|
24
|
+
find(:first)
|
25
|
+
end
|
26
|
+
|
27
|
+
def findable_index
|
28
|
+
raise NotImplementedError, "Findable expects a public class method 'findable_index'"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
|
33
|
+
module InstanceMethods
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
def self.included(receiver)
|
38
|
+
receiver.extend ClassMethods
|
39
|
+
receiver.send :include, InstanceMethods
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -35,6 +35,10 @@ module Resourceful
|
|
35
35
|
|
36
36
|
protected
|
37
37
|
|
38
|
+
def self.format
|
39
|
+
Resourceful::Resource::Json.to_s
|
40
|
+
end
|
41
|
+
|
38
42
|
def attribute(config)
|
39
43
|
begin
|
40
44
|
get_node(config[:path])
|
@@ -54,6 +58,7 @@ module Resourceful
|
|
54
58
|
def get_node(path_config)
|
55
59
|
self.class.get_node(@data, path_config)
|
56
60
|
end
|
61
|
+
|
57
62
|
end
|
58
63
|
|
59
64
|
end
|
data/lib/resourceful/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: kelredd-resourceful
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kelly Redding
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-09-01 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -93,6 +93,9 @@ files:
|
|
93
93
|
- lib/resourceful/extensions.rb
|
94
94
|
- lib/resourceful/model
|
95
95
|
- lib/resourceful/model/base.rb
|
96
|
+
- lib/resourceful/model/embedded_associations.rb
|
97
|
+
- lib/resourceful/model/external_associations.rb
|
98
|
+
- lib/resourceful/model/findable.rb
|
96
99
|
- lib/resourceful/model/json.rb
|
97
100
|
- lib/resourceful/model/xml.rb
|
98
101
|
- lib/resourceful/model.rb
|