kelredd-resourceful 0.7.7 → 0.7.9
Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,75 @@
|
|
1
|
+
module Resourceful
|
2
|
+
module Model
|
3
|
+
module ExternalAssociations
|
4
|
+
|
5
|
+
module ClassMethods
|
6
|
+
|
7
|
+
protected
|
8
|
+
|
9
|
+
def has_one_resourceful(name, config={})
|
10
|
+
has_many(name, config).first
|
11
|
+
end
|
12
|
+
|
13
|
+
def has_many_resourceful(name, config={})
|
14
|
+
clean_name = Resourceful::Model::Base.cleanup_name(name)
|
15
|
+
config ||= {}
|
16
|
+
raise ArgumentError, "has_many_resourceful requires a :class_name option to be specified" unless config[:class_name]
|
17
|
+
class_name = config.delete(:class_name).to_s
|
18
|
+
force = config.delete(:force) || true
|
19
|
+
define_method(name) do
|
20
|
+
klass = class_name.resourceful_constantize
|
21
|
+
raise ArgumentError, "has_many_resourceful :class_name '#{class_name}' is not defined" if klass.nil?
|
22
|
+
unless klass.respond_to?(:find)
|
23
|
+
raise NotImplementedError, "has_many_resourceful expects #{klass} to be findable (ie mixin the Findable helper)"
|
24
|
+
end
|
25
|
+
fk = config.delete(:foreign_key) || "#{self.class.name.demodulize.underscore}_id"
|
26
|
+
fk_method = config.delete(:foreign_key_method) || 'id'
|
27
|
+
config[fk] = self.send(fk_method)
|
28
|
+
instance_variable_get("@#{clean_name}") || \
|
29
|
+
instance_variable_set("@#{clean_name}", \
|
30
|
+
klass.find(:all, config, force)
|
31
|
+
)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def belongs_to_resourceful(name, config={})
|
36
|
+
clean_name = Resourceful::Model::Base.cleanup_name(name)
|
37
|
+
config ||= {}
|
38
|
+
raise ArgumentError, "belongs_to_resourceful requires a :class_name option to be specified" unless config[:class_name]
|
39
|
+
class_name = config.delete(:class_name).to_s
|
40
|
+
foreign_key = config.delete(:foreign_key) || "#{clean_name}_id"
|
41
|
+
force = config.delete(:force) || true
|
42
|
+
define_method(name) do
|
43
|
+
klass = class_name.resourceful_constantize
|
44
|
+
raise ArgumentError, "belongs_to_resourceful :class_name '#{class_name}' is not defined" if klass.nil?
|
45
|
+
unless self.respond_to?(foreign_key)
|
46
|
+
raise ArgumentError, "belongs_to_resourceful requires a '#{foreign_key}' method defined to return the foreign_key"
|
47
|
+
end
|
48
|
+
unless klass.respond_to?(:find)
|
49
|
+
raise NotImplementedError, "belongs_to_resourceful expects #{klass} to be findable (ie mixin the Findable helper)"
|
50
|
+
end
|
51
|
+
fk = self.send(foreign_key)
|
52
|
+
if fk.nil? || (fk.respond_to?('empty?') && fk.empty?)
|
53
|
+
nil
|
54
|
+
else
|
55
|
+
instance_variable_get("@#{clean_name}") || \
|
56
|
+
instance_variable_set("@#{clean_name}", \
|
57
|
+
klass.find(fk, config, force)
|
58
|
+
)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
65
|
+
module InstanceMethods
|
66
|
+
end
|
67
|
+
|
68
|
+
def self.included(receiver)
|
69
|
+
receiver.extend ClassMethods
|
70
|
+
receiver.send :include, InstanceMethods
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -81,6 +81,10 @@ module Resourceful
|
|
81
81
|
yield attributes(true)
|
82
82
|
@data = nil
|
83
83
|
end
|
84
|
+
|
85
|
+
def self.cleanup_name(name)
|
86
|
+
name.to_s.gsub(/\W/,'')
|
87
|
+
end
|
84
88
|
|
85
89
|
protected
|
86
90
|
|
@@ -110,10 +114,6 @@ module Resourceful
|
|
110
114
|
end
|
111
115
|
end
|
112
116
|
|
113
|
-
def self.cleanup_name(name)
|
114
|
-
name.to_s.gsub(/\W/,'')
|
115
|
-
end
|
116
|
-
|
117
117
|
private
|
118
118
|
|
119
119
|
def fetch_attribute(clean_name, config, content_method)
|
@@ -46,7 +46,7 @@ module Resourceful
|
|
46
46
|
raise ArgumentError, "belongs_to requires a '#{foreign_key}' method defined to return the foreign_key"
|
47
47
|
end
|
48
48
|
unless klass.respond_to?(:find)
|
49
|
-
raise NotImplementedError, "
|
49
|
+
raise NotImplementedError, "belongs_to expects #{klass} to be findable (ie mixin the Findable helper)"
|
50
50
|
end
|
51
51
|
fk = self.send(foreign_key)
|
52
52
|
if fk.nil? || (fk.respond_to?('empty?') && fk.empty?)
|
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.7.
|
4
|
+
version: 0.7.9
|
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-09-
|
12
|
+
date: 2009-09-21 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/resourceful/exceptions.rb
|
93
93
|
- lib/resourceful/extensions.rb
|
94
94
|
- lib/resourceful/model
|
95
|
+
- lib/resourceful/model/activerecord_associations.rb
|
95
96
|
- lib/resourceful/model/attribute_types.rb
|
96
97
|
- lib/resourceful/model/base.rb
|
97
98
|
- lib/resourceful/model/embedded_associations.rb
|