interest 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.
- checksums.yaml +4 -4
- data/lib/interest/definition.rb +1 -1
- data/lib/interest/utils.rb +48 -11
- data/lib/interest/version.rb +1 -1
- data/spec/blockable_spec.rb +10 -0
- data/spec/follow_requestable_spec.rb +10 -0
- data/spec/followable_spec.rb +10 -0
- data/spec/support/models/deeply_nested_namespace_stuff.rb +12 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db3f3e5e177d1f9cadc4cc8e9e47db3da053fb8c
|
4
|
+
data.tar.gz: 7f519de87a569330f0912e46e0ccb86fac2aa836
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5c2b439fadd5dc295e4c65f20e1b33863195149694bc885c989d84a6665d4a967ea5aed2ecc20e80900bcadea66cf01164ae5aa2460a51a0e8557ab37a041f3f
|
7
|
+
data.tar.gz: aa3732ae39b50117f043adb9a2355fafd03d4532ee055c64d3f08a9708bc9c81105710fb064561041445cbeda2e8a2581e3441d5c42f65b3f76ab023744571d0
|
data/lib/interest/definition.rb
CHANGED
@@ -14,7 +14,7 @@ module Interest
|
|
14
14
|
define_method :method_missing do |name, *args, &block|
|
15
15
|
return super(name, *args, &block) unless matches = /\A#{target}_(?<type>.+)\Z/.match(name.to_s)
|
16
16
|
|
17
|
-
self.class.__send__ :"define_#{source}_association_method", Interest::Utils.source_type_of(matches[:type])
|
17
|
+
self.class.__send__ :"define_#{source}_association_method", Interest::Utils.source_type_of(matches[:type], self.class.model_name.to_s)
|
18
18
|
|
19
19
|
__send__ name, *args, &block
|
20
20
|
end
|
data/lib/interest/utils.rb
CHANGED
@@ -1,27 +1,64 @@
|
|
1
1
|
require "active_support"
|
2
2
|
require "active_support/core_ext/string/inflections"
|
3
3
|
require "active_record"
|
4
|
+
require "active_model/naming"
|
4
5
|
|
5
6
|
module Interest
|
6
7
|
module Utils
|
7
8
|
class << self
|
8
9
|
def symbolic_name_of(object)
|
9
|
-
if object
|
10
|
-
object
|
11
|
-
|
12
|
-
object.
|
10
|
+
if model_class_or_instance(object)
|
11
|
+
ActiveModel::Naming.plural(object)
|
12
|
+
else
|
13
|
+
object.to_s.underscore.gsub("/", "_").pluralize
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def source_type_of(object, prefix = nil)
|
18
|
+
case model_class_or_instance(object)
|
19
|
+
when :instance
|
20
|
+
object.class.model_name.to_s
|
21
|
+
when :class
|
22
|
+
object.model_name.to_s
|
13
23
|
else
|
14
|
-
object
|
15
|
-
end
|
24
|
+
source_type_from_letter_of(object, prefix)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def source_types_from_letter_of(object, prefix = nil)
|
31
|
+
return to_enum(:source_types_from_letter_of, object, prefix) unless block_given?
|
32
|
+
|
33
|
+
parts = object.to_s.split("_")
|
34
|
+
|
35
|
+
["", "::"].repeated_permutation(parts.size - 1).each do |permutation|
|
36
|
+
name = permutation.map.with_index {|value, i| parts[i].camelize + value }.join
|
37
|
+
|
38
|
+
yield(name << parts.last.classify)
|
39
|
+
yield("#{prefix}::#{name}") unless prefix.nil?
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def source_type_from_letter_of(object, prefix = nil)
|
44
|
+
source_types_from_letter_of(object, prefix).each do |source_type|
|
45
|
+
begin
|
46
|
+
source_type.constantize
|
47
|
+
return source_type
|
48
|
+
rescue
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
raise "Could not determine source type from #{object.inspect}"
|
16
53
|
end
|
17
54
|
|
18
|
-
def
|
55
|
+
def model_class_or_instance(object)
|
19
56
|
if object.is_a?(ActiveRecord::Base)
|
20
|
-
|
21
|
-
elsif object.is_a?(Class)
|
22
|
-
|
57
|
+
:instance
|
58
|
+
elsif object.is_a?(Class) && object < ActiveRecord::Base
|
59
|
+
:class
|
23
60
|
else
|
24
|
-
|
61
|
+
nil
|
25
62
|
end
|
26
63
|
end
|
27
64
|
end
|
data/lib/interest/version.rb
CHANGED
data/spec/blockable_spec.rb
CHANGED
@@ -124,4 +124,14 @@ describe "Blockable" do
|
|
124
124
|
expect(user.blocker_relationships.of(BlockableUser)).to have_exactly(2).items
|
125
125
|
expect(user.blocker_relationships.of(Collection)).to have_exactly(1).items
|
126
126
|
end
|
127
|
+
|
128
|
+
it "should be defined association method for namespaced class" do
|
129
|
+
user = BlockableUser.create!
|
130
|
+
stuff = Deeply::Nested::Namespace::Stuff.create!
|
131
|
+
|
132
|
+
user.block(stuff)
|
133
|
+
|
134
|
+
expect { user.blocking_deeply_nested_namespace_stuffs }.not_to raise_error
|
135
|
+
expect { stuff.blocker_blockable_users }.not_to raise_error
|
136
|
+
end
|
127
137
|
end
|
@@ -123,4 +123,14 @@ describe "FollowRequestable" do
|
|
123
123
|
expect(user.incoming_follow_requests.of(FollowRequestableUser)).to have_exactly(2).items
|
124
124
|
expect(user.incoming_follow_requests.of(Collection)).to have_exactly(1).items
|
125
125
|
end
|
126
|
+
|
127
|
+
it "should be defined association method for namespaced class" do
|
128
|
+
user = FollowRequestableUser.create!
|
129
|
+
stuff = Deeply::Nested::Namespace::Stuff.create!
|
130
|
+
|
131
|
+
user.request_to_follow(stuff)
|
132
|
+
|
133
|
+
expect { user.follow_requestee_deeply_nested_namespace_stuffs }.not_to raise_error
|
134
|
+
expect { stuff.follow_requester_follow_requestable_users }.not_to raise_error
|
135
|
+
end
|
126
136
|
end
|
data/spec/followable_spec.rb
CHANGED
@@ -168,4 +168,14 @@ describe "Followable" do
|
|
168
168
|
expect(user.follower_relationships.of(FollowableUser)).to have_exactly(2).items
|
169
169
|
expect(user.follower_relationships.of(Collection)).to have_exactly(1).items
|
170
170
|
end
|
171
|
+
|
172
|
+
it "should be defined association method for namespaced class" do
|
173
|
+
user = FollowableUser.create!
|
174
|
+
stuff = Deeply::Nested::Namespace::Stuff.create!
|
175
|
+
|
176
|
+
user.follow(stuff)
|
177
|
+
|
178
|
+
expect { user.following_deeply_nested_namespace_stuffs }.not_to raise_error
|
179
|
+
expect { stuff.follower_followable_users }.not_to raise_error
|
180
|
+
end
|
171
181
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: interest
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- tatat
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -159,6 +159,7 @@ files:
|
|
159
159
|
- spec/support/models/blockable_user.rb
|
160
160
|
- spec/support/models/blocking.rb
|
161
161
|
- spec/support/models/collection.rb
|
162
|
+
- spec/support/models/deeply_nested_namespace_stuff.rb
|
162
163
|
- spec/support/models/follow_requestable_and_blockable_user.rb
|
163
164
|
- spec/support/models/follow_requestable_user.rb
|
164
165
|
- spec/support/models/followable_and_blockable_user.rb
|
@@ -202,6 +203,7 @@ test_files:
|
|
202
203
|
- spec/support/models/blockable_user.rb
|
203
204
|
- spec/support/models/blocking.rb
|
204
205
|
- spec/support/models/collection.rb
|
206
|
+
- spec/support/models/deeply_nested_namespace_stuff.rb
|
205
207
|
- spec/support/models/follow_requestable_and_blockable_user.rb
|
206
208
|
- spec/support/models/follow_requestable_user.rb
|
207
209
|
- spec/support/models/followable_and_blockable_user.rb
|