resonance 0.4.0 → 0.4.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/resonance.rb +41 -34
- data/lib/resonance/version.rb +1 -1
- data/resonance.gemspec +1 -2
- metadata +3 -20
- data/lib/resonance/errors/argument_error.rb +0 -5
- data/lib/resonance/supports/converter.rb +0 -42
- data/lib/resonance/supports/patches.yml +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 8aaf9254ebb18d91512b8629574264f00bd3ff67
|
4
|
+
data.tar.gz: 0ac11e80af1d688b2c600af28631f94b5af67d6d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: da5488b26c318374c5b2a432729b74e92db5d73135c1fbe3cdba40698eb679ab90fc99e843cbcef664ce93cd65c859e6e64f3ff8a4ddb0d86089ef58ae2fb6bd
|
7
|
+
data.tar.gz: d6c4d2027d4b7fb5cc4df3ef8189c38ce3132cd890a8e05e4b4e39285453059194533179ec3f529c9a99e81a76b4247dc4b5e8560f745cda18ebe540bef8b5c5
|
data/lib/resonance.rb
CHANGED
@@ -1,84 +1,91 @@
|
|
1
|
-
require '
|
2
|
-
require 'resonance/supports/converter'
|
1
|
+
require 'inflexion'
|
3
2
|
|
4
3
|
module Resonance
|
5
|
-
class
|
6
|
-
def included(base)
|
7
|
-
base.extend(ClassMethods)
|
8
|
-
end
|
9
|
-
end
|
4
|
+
class ArgumentError < StandardError; end
|
10
5
|
|
11
6
|
module ClassMethods
|
12
|
-
include Resonance::Supports::Converter
|
13
|
-
|
14
7
|
def resonate(source, target: nil, action: nil, foreign_key: {})
|
15
|
-
[source, target, action]
|
8
|
+
roles = [source, target, action]
|
9
|
+
|
10
|
+
roles.each do |role|
|
16
11
|
if role.nil?
|
17
|
-
raise Resonance::
|
12
|
+
raise Resonance::ArgumentError, 'Passed argument is not a valid'
|
18
13
|
end
|
19
14
|
end
|
20
15
|
|
21
|
-
source = source.to_s
|
22
|
-
target = target.to_s
|
23
|
-
action = action.to_s
|
24
|
-
|
25
16
|
foreign_key.tap do |key|
|
26
17
|
key[:source] = :"#{source}_id" if key[:source].nil?
|
27
18
|
key[:target] = :"target_#{target}_id" if key[:target].nil?
|
28
19
|
end
|
29
20
|
|
30
|
-
|
31
|
-
|
21
|
+
Resonance.define(*roles.map(&:to_s), foreign_key)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def included(base)
|
27
|
+
base.extend(ClassMethods)
|
28
|
+
end
|
29
|
+
|
30
|
+
def define(source, target, action, foreign_key)
|
31
|
+
classize(source).class_eval <<-EOS
|
32
|
+
has_many :#{action.pluralize},
|
32
33
|
foreign_key: :#{source}_id,
|
33
34
|
dependent: :destroy
|
34
35
|
|
35
|
-
has_many :#{progressize
|
36
|
-
through: :#{pluralize
|
36
|
+
has_many :#{action.progressize},
|
37
|
+
through: :#{action.pluralize},
|
37
38
|
source: :target_#{target}
|
38
39
|
|
39
40
|
def #{action}(target)
|
40
|
-
if #{progressize
|
41
|
+
if #{action.progressize}?(target) || (self == target)
|
41
42
|
return
|
42
43
|
end
|
43
44
|
|
44
|
-
#{pluralize
|
45
|
+
#{action.pluralize}.create(#{foreign_key[:target]}: target.id)
|
45
46
|
end
|
46
47
|
|
47
48
|
def un#{action}(target)
|
48
|
-
unless #{progressize
|
49
|
+
unless #{action.progressize}?(target)
|
49
50
|
return
|
50
51
|
end
|
51
52
|
|
52
|
-
#{pluralize
|
53
|
+
#{action.pluralize}.find_by(#{foreign_key[:target]}: target.id).destroy
|
53
54
|
end
|
54
55
|
|
55
|
-
def #{progressize
|
56
|
-
#{pluralize
|
56
|
+
def #{action.progressize}?(target)
|
57
|
+
#{action.pluralize}.exists?(#{foreign_key[:target]}: target.id)
|
57
58
|
end
|
58
59
|
EOS
|
59
60
|
|
60
|
-
|
61
|
-
has_many :#{pluralize
|
61
|
+
classize(target).class_eval <<-EOS
|
62
|
+
has_many :#{action.pluralize}_as_target,
|
62
63
|
foreign_key: :#{foreign_key[:target]},
|
63
|
-
class_name: '#{
|
64
|
+
class_name: '#{classize(action)}',
|
64
65
|
dependent: :destroy
|
65
66
|
|
66
|
-
has_many :#{
|
67
|
-
through: :#{pluralize
|
67
|
+
has_many :#{action.peopleize},
|
68
|
+
through: :#{action.pluralize}_as_target,
|
68
69
|
source: :#{source}
|
69
70
|
|
70
|
-
def #{pastize
|
71
|
-
source.#{pluralize
|
71
|
+
def #{action.pastize}_by?(source)
|
72
|
+
source.#{action.pluralize}.exists?(#{foreign_key[:target]}: id)
|
72
73
|
end
|
73
74
|
EOS
|
74
75
|
|
75
|
-
|
76
|
+
classize(action).class_eval <<-EOS
|
76
77
|
belongs_to :#{source}
|
77
78
|
|
78
79
|
belongs_to :target_#{target},
|
79
|
-
class_name: '#{
|
80
|
+
class_name: '#{classize(target)}',
|
80
81
|
foreign_key: :#{foreign_key[:target]}
|
81
82
|
EOS
|
82
83
|
end
|
84
|
+
|
85
|
+
private
|
86
|
+
|
87
|
+
def classize(str)
|
88
|
+
str.camelize.constantize
|
89
|
+
end
|
83
90
|
end
|
84
91
|
end
|
data/lib/resonance/version.rb
CHANGED
data/resonance.gemspec
CHANGED
@@ -15,8 +15,7 @@ Gem::Specification.new do |s|
|
|
15
15
|
|
16
16
|
s.files = `git ls-files -z`.split("\x0")
|
17
17
|
|
18
|
-
s.add_dependency '
|
19
|
-
s.add_dependency 'verbs'
|
18
|
+
s.add_dependency 'inflexion'
|
20
19
|
|
21
20
|
s.add_development_dependency 'rspec-rails'
|
22
21
|
s.add_development_dependency 'actionpack'
|
metadata
CHANGED
@@ -1,31 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: resonance
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- kami
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-06-
|
11
|
+
date: 2016-06-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
15
|
-
requirement: !ruby/object:Gem::Requirement
|
16
|
-
requirements:
|
17
|
-
- - ">="
|
18
|
-
- !ruby/object:Gem::Version
|
19
|
-
version: '0'
|
20
|
-
type: :runtime
|
21
|
-
prerelease: false
|
22
|
-
version_requirements: !ruby/object:Gem::Requirement
|
23
|
-
requirements:
|
24
|
-
- - ">="
|
25
|
-
- !ruby/object:Gem::Version
|
26
|
-
version: '0'
|
27
|
-
- !ruby/object:Gem::Dependency
|
28
|
-
name: verbs
|
14
|
+
name: inflexion
|
29
15
|
requirement: !ruby/object:Gem::Requirement
|
30
16
|
requirements:
|
31
17
|
- - ">="
|
@@ -107,9 +93,6 @@ files:
|
|
107
93
|
- README.md
|
108
94
|
- Rakefile
|
109
95
|
- lib/resonance.rb
|
110
|
-
- lib/resonance/errors/argument_error.rb
|
111
|
-
- lib/resonance/supports/converter.rb
|
112
|
-
- lib/resonance/supports/patches.yml
|
113
96
|
- lib/resonance/version.rb
|
114
97
|
- resonance.gemspec
|
115
98
|
- spec/dummy/application.rb
|
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'verbs'
|
2
|
-
|
3
|
-
module Resonance
|
4
|
-
module Supports
|
5
|
-
module Converter
|
6
|
-
private
|
7
|
-
|
8
|
-
def classify(str)
|
9
|
-
str.camelize.constantize
|
10
|
-
end
|
11
|
-
|
12
|
-
def pluralize(str)
|
13
|
-
str.pluralize
|
14
|
-
end
|
15
|
-
|
16
|
-
def progressize(str)
|
17
|
-
if patches['progressize'].key?(str)
|
18
|
-
return patches['progressize'][str]
|
19
|
-
end
|
20
|
-
|
21
|
-
str.verb.conjugate(aspect: :progressive).split(' ').last
|
22
|
-
end
|
23
|
-
|
24
|
-
def peoplize(str)
|
25
|
-
str = (str.last == 'e') ? str.chop : str
|
26
|
-
str + 'ers'
|
27
|
-
end
|
28
|
-
|
29
|
-
def pastize(str)
|
30
|
-
str.verb.conjugate(tense: :past).split(' ').last
|
31
|
-
end
|
32
|
-
|
33
|
-
def patches
|
34
|
-
@patches ||= YAML.load_file(patches_path)['patches']
|
35
|
-
end
|
36
|
-
|
37
|
-
def patches_path
|
38
|
-
File.expand_path('../patches.yml', __FILE__)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|