her 0.2.6 → 0.3
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/.gitignore +1 -0
- data/README.md +78 -39
- data/lib/her/model/introspection.rb +27 -0
- data/lib/her/model/orm.rb +3 -5
- data/lib/her/model/paths.rb +6 -2
- data/lib/her/model/relationships.rb +44 -32
- data/lib/her/version.rb +1 -1
- data/spec/model/hooks_spec.rb +105 -105
- data/spec/model/http_spec.rb +44 -33
- data/spec/model/introspection_spec.rb +27 -6
- data/spec/model/orm_spec.rb +19 -20
- data/spec/model/paths_spec.rb +43 -43
- data/spec/model/relationships_spec.rb +48 -51
- data/spec/spec_helper.rb +28 -13
- metadata +4 -4
@@ -4,61 +4,61 @@ require File.join(File.dirname(__FILE__), "../spec_helper.rb")
|
|
4
4
|
describe Her::Model::Relationships do
|
5
5
|
context "setting relationships without details" do
|
6
6
|
before do # {{{
|
7
|
-
spawn_model
|
7
|
+
spawn_model "Foo::User"
|
8
8
|
end # }}}
|
9
9
|
|
10
10
|
it "handles a single 'has_many' relationship" do # {{{
|
11
|
-
User.has_many :comments
|
12
|
-
User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment" }]
|
11
|
+
Foo::User.has_many :comments
|
12
|
+
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment", :path => "/comments" }]
|
13
13
|
end # }}}
|
14
14
|
|
15
15
|
it "handles multiples 'has_many' relationship" do # {{{
|
16
|
-
User.has_many :comments
|
17
|
-
User.has_many :posts
|
18
|
-
User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment" }, { :name => :posts, :class_name => "Post" }]
|
16
|
+
Foo::User.has_many :comments
|
17
|
+
Foo::User.has_many :posts
|
18
|
+
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Comment", :path => "/comments" }, { :name => :posts, :class_name => "Post", :path => "/posts" }]
|
19
19
|
end # }}}
|
20
20
|
|
21
21
|
it "handles a single 'has_one' relationship" do # {{{
|
22
|
-
User.has_one :category
|
23
|
-
User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category" }]
|
22
|
+
Foo::User.has_one :category
|
23
|
+
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category", :path => "/category" }]
|
24
24
|
end # }}}
|
25
25
|
|
26
26
|
it "handles multiples 'has_one' relationship" do # {{{
|
27
|
-
User.has_one :category
|
28
|
-
User.has_one :role
|
29
|
-
User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category" }, { :name => :role, :class_name => "Role"
|
27
|
+
Foo::User.has_one :category
|
28
|
+
Foo::User.has_one :role
|
29
|
+
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Category", :path => "/category" }, { :name => :role, :class_name => "Role", :path => "/role" }]
|
30
30
|
end # }}}
|
31
31
|
|
32
32
|
it "handles a single belongs_to relationship" do # {{{
|
33
|
-
User.belongs_to :organization
|
34
|
-
User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id" }]
|
33
|
+
Foo::User.belongs_to :organization
|
34
|
+
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id", :path => "/organizations/:id" }]
|
35
35
|
end # }}}
|
36
36
|
|
37
37
|
it "handles multiples 'belongs_to' relationship" do # {{{
|
38
|
-
User.belongs_to :organization
|
39
|
-
User.belongs_to :family
|
40
|
-
User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id" }, { :name => :family, :class_name => "Family", :foreign_key => "family_id" }]
|
38
|
+
Foo::User.belongs_to :organization
|
39
|
+
Foo::User.belongs_to :family
|
40
|
+
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Organization", :foreign_key => "organization_id", :path => "/organizations/:id" }, { :name => :family, :class_name => "Family", :foreign_key => "family_id", :path => "/families/:id" }]
|
41
41
|
end # }}}
|
42
42
|
end
|
43
43
|
|
44
44
|
context "setting relationships with details" do
|
45
45
|
before do # {{{
|
46
|
-
spawn_model
|
46
|
+
spawn_model "Foo::User"
|
47
47
|
end # }}}
|
48
48
|
|
49
49
|
it "handles a single 'has_many' relationship" do # {{{
|
50
|
-
User.has_many :comments, :class_name => "Post"
|
51
|
-
User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Post" }]
|
50
|
+
Foo::User.has_many :comments, :class_name => "Post"
|
51
|
+
Foo::User.relationships[:has_many].should == [{ :name => :comments, :class_name => "Post", :path => "/comments" }]
|
52
52
|
end # }}}
|
53
53
|
|
54
54
|
it "handles a single 'has_one' relationship" do # {{{
|
55
|
-
User.has_one :category, :class_name => "Topic", :foreign_key => "topic_id"
|
56
|
-
User.relationships[:has_one].should == [{ :name => :category, :class_name => "Topic", :foreign_key => "topic_id" }]
|
55
|
+
Foo::User.has_one :category, :class_name => "Topic", :foreign_key => "topic_id"
|
56
|
+
Foo::User.relationships[:has_one].should == [{ :name => :category, :class_name => "Topic", :foreign_key => "topic_id", :path => "/category" }]
|
57
57
|
end # }}}
|
58
58
|
|
59
59
|
it "handles a single belongs_to relationship" do # {{{
|
60
|
-
User.belongs_to :organization, :class_name => "Business", :foreign_key => "
|
61
|
-
User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Business", :foreign_key => "
|
60
|
+
Foo::User.belongs_to :organization, :class_name => "Business", :foreign_key => "org_id"
|
61
|
+
Foo::User.relationships[:belongs_to].should == [{ :name => :organization, :class_name => "Business", :foreign_key => "org_id", :path => "/organizations/:id" }]
|
62
62
|
end # }}}
|
63
63
|
end
|
64
64
|
|
@@ -76,54 +76,54 @@ describe Her::Model::Relationships do
|
|
76
76
|
end
|
77
77
|
end
|
78
78
|
|
79
|
-
spawn_model
|
79
|
+
spawn_model "Foo::User" do
|
80
80
|
has_many :comments
|
81
81
|
has_one :role
|
82
82
|
belongs_to :organization
|
83
83
|
end
|
84
84
|
|
85
|
-
spawn_model
|
86
|
-
spawn_model
|
87
|
-
spawn_model
|
85
|
+
spawn_model "Foo::Organization"
|
86
|
+
spawn_model "Foo::Comment"
|
87
|
+
spawn_model "Foo::Role"
|
88
88
|
|
89
|
-
@user_with_included_data = User.find(1)
|
90
|
-
@user_without_included_data = User.find(2)
|
89
|
+
@user_with_included_data = Foo::User.find(1)
|
90
|
+
@user_without_included_data = Foo::User.find(2)
|
91
91
|
end # }}}
|
92
92
|
|
93
93
|
it "maps an array of included data through has_many" do # {{{
|
94
|
-
@user_with_included_data.comments.first.
|
94
|
+
@user_with_included_data.comments.first.should be_a(Foo::Comment)
|
95
95
|
@user_with_included_data.comments.length.should == 2
|
96
96
|
@user_with_included_data.comments.first.id.should == 2
|
97
97
|
@user_with_included_data.comments.first.body.should == "Tobias, you blow hard!"
|
98
98
|
end # }}}
|
99
99
|
|
100
100
|
it "fetches data that was not included through has_many" do # {{{
|
101
|
-
@user_without_included_data.comments.first.
|
101
|
+
@user_without_included_data.comments.first.should be_a(Foo::Comment)
|
102
102
|
@user_without_included_data.comments.length.should == 2
|
103
103
|
@user_without_included_data.comments.first.id.should == 4
|
104
104
|
@user_without_included_data.comments.first.body.should == "They're having a FIRESALE?"
|
105
105
|
end # }}}
|
106
106
|
|
107
107
|
it "maps an array of included data through has_one" do # {{{
|
108
|
-
@user_with_included_data.role.
|
108
|
+
@user_with_included_data.role.should be_a(Foo::Role)
|
109
109
|
@user_with_included_data.role.id.should == 1
|
110
110
|
@user_with_included_data.role.body.should == "Admin"
|
111
111
|
end # }}}
|
112
112
|
|
113
113
|
it "fetches data that was not included through has_one" do # {{{
|
114
|
-
@user_without_included_data.role.
|
114
|
+
@user_without_included_data.role.should be_a(Foo::Role)
|
115
115
|
@user_without_included_data.role.id.should == 2
|
116
116
|
@user_without_included_data.role.body.should == "User"
|
117
117
|
end # }}}
|
118
118
|
|
119
119
|
it "maps an array of included data through belongs_to" do # {{{
|
120
|
-
@user_with_included_data.organization.
|
120
|
+
@user_with_included_data.organization.should be_a(Foo::Organization)
|
121
121
|
@user_with_included_data.organization.id.should == 1
|
122
122
|
@user_with_included_data.organization.name.should == "Bluth Company"
|
123
123
|
end # }}}
|
124
124
|
|
125
125
|
it "fetches data that was not included through belongs_to" do # {{{
|
126
|
-
@user_without_included_data.organization.
|
126
|
+
@user_without_included_data.organization.should be_a(Foo::Organization)
|
127
127
|
@user_without_included_data.organization.id.should == 1
|
128
128
|
@user_without_included_data.organization.name.should == "Bluth Company"
|
129
129
|
end # }}}
|
@@ -138,28 +138,25 @@ describe Her::Model::Relationships do
|
|
138
138
|
stub.get("/users/1") { |env| [200, {}, { :id => 1, :name => "Tobias Fünke", :organization => { :id => 1, :name => "Bluth Company" }, :organization_id => 1 }.to_json] }
|
139
139
|
stub.get("/users/2") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization_id => 1 }.to_json] }
|
140
140
|
stub.get("/users/3") { |env| [200, {}, { :id => 2, :name => "Lindsay Fünke", :organization => nil }.to_json] }
|
141
|
-
stub.get("/
|
141
|
+
stub.get("/companies/1") { |env| [200, {}, { :id => 1, :name => "Bluth Company" }.to_json] }
|
142
142
|
end
|
143
143
|
end
|
144
144
|
|
145
|
-
|
146
|
-
|
147
|
-
belongs_to :organization, :class_name => "Business"
|
145
|
+
spawn_model "Foo::User" do
|
146
|
+
belongs_to :company, :path => "/organizations/:id", :foreign_key => :organization_id
|
148
147
|
end
|
149
148
|
|
150
|
-
spawn_model
|
151
|
-
collection_path "/organizations"
|
152
|
-
end
|
149
|
+
spawn_model "Foo::Company"
|
153
150
|
|
154
|
-
@user_with_included_data = User.find(1)
|
155
|
-
@user_without_included_data = User.find(2)
|
156
|
-
@user_with_included_nil_data = User.find(3)
|
151
|
+
@user_with_included_data = Foo::User.find(1)
|
152
|
+
@user_without_included_data = Foo::User.find(2)
|
153
|
+
@user_with_included_nil_data = Foo::User.find(3)
|
157
154
|
end # }}}
|
158
155
|
|
159
156
|
it "maps an array of included data through belongs_to" do # {{{
|
160
|
-
@user_with_included_data.
|
161
|
-
@user_with_included_data.
|
162
|
-
@user_with_included_data.
|
157
|
+
@user_with_included_data.company.should be_a(Foo::Company)
|
158
|
+
@user_with_included_data.company.id.should == 1
|
159
|
+
@user_with_included_data.company.name.should == "Bluth Company"
|
163
160
|
end # }}}
|
164
161
|
|
165
162
|
it "does not map included data if it’s nil" do # {{{
|
@@ -167,9 +164,9 @@ describe Her::Model::Relationships do
|
|
167
164
|
end # }}}
|
168
165
|
|
169
166
|
it "fetches data that was not included through belongs_to" do # {{{
|
170
|
-
@user_without_included_data.
|
171
|
-
@user_without_included_data.
|
172
|
-
@user_without_included_data.
|
167
|
+
@user_without_included_data.company.should be_a(Foo::Company)
|
168
|
+
@user_without_included_data.company.id.should == 1
|
169
|
+
@user_without_included_data.company.name.should == "Bluth Company"
|
173
170
|
end # }}}
|
174
171
|
end
|
175
172
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,20 @@
|
|
1
1
|
$:.unshift File.join(File.dirname(__FILE__), *%w[.. lib])
|
2
2
|
|
3
3
|
require "her"
|
4
|
-
require "
|
5
|
-
|
6
|
-
module Helpers
|
7
|
-
end
|
4
|
+
require "mocha_standalone"
|
8
5
|
|
9
6
|
RSpec.configure do |c|
|
10
|
-
c.
|
7
|
+
c.mock_with :mocha
|
8
|
+
|
9
|
+
c.before :each do
|
10
|
+
@globals = []
|
11
|
+
end
|
12
|
+
|
13
|
+
c.after :each do
|
14
|
+
@globals.each do |global|
|
15
|
+
Object.instance_eval { remove_const global } if Object.const_defined?(global)
|
16
|
+
end
|
17
|
+
end
|
11
18
|
end
|
12
19
|
|
13
20
|
class Hash
|
@@ -19,12 +26,20 @@ class Array
|
|
19
26
|
end
|
20
27
|
|
21
28
|
def spawn_model(klass, &block)
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
29
|
+
if klass =~ /::/
|
30
|
+
base, submodel = klass.split(/::/).map{ |s| s.to_sym }
|
31
|
+
Object.const_set(base, Module.new) unless Object.const_defined?(base)
|
32
|
+
Object.const_get(base).module_eval do
|
33
|
+
remove_const submodel if constants.include?(submodel)
|
34
|
+
submodel = const_set(submodel, Class.new)
|
35
|
+
submodel.send(:include, Her::Model)
|
36
|
+
submodel.class_eval(&block) if block_given?
|
37
|
+
end
|
38
|
+
@globals << base
|
39
|
+
else
|
40
|
+
Object.instance_eval { remove_const klass } if Object.const_defined?(klass)
|
41
|
+
Object.const_set(klass, Class.new).send(:include, Her::Model)
|
42
|
+
Object.const_get(klass).class_eval(&block) if block_given?
|
43
|
+
@globals << klass.to_sym
|
44
|
+
end
|
30
45
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: her
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-07-
|
12
|
+
date: 2012-07-20 00:00:00.000000000Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -271,7 +271,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
271
271
|
version: '0'
|
272
272
|
segments:
|
273
273
|
- 0
|
274
|
-
hash: -
|
274
|
+
hash: -87674710700490998
|
275
275
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
276
276
|
none: false
|
277
277
|
requirements:
|
@@ -280,7 +280,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
280
280
|
version: '0'
|
281
281
|
segments:
|
282
282
|
- 0
|
283
|
-
hash: -
|
283
|
+
hash: -87674710700490998
|
284
284
|
requirements: []
|
285
285
|
rubyforge_project:
|
286
286
|
rubygems_version: 1.8.18
|