resonance 0.4.0 → 0.4.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9d54325b0019a5cea69f4f76ceb55217e55e86f
4
- data.tar.gz: 866b1fd32593d8e279cc1f6a3b2e7bc6ff21d35c
3
+ metadata.gz: 8aaf9254ebb18d91512b8629574264f00bd3ff67
4
+ data.tar.gz: 0ac11e80af1d688b2c600af28631f94b5af67d6d
5
5
  SHA512:
6
- metadata.gz: c2fdb2ffd19c860f8ad5e16c96b244d6868d6e7173268ac2bc6e82305d870d9da3196792501069b489d94c4d1ea17b71e91cb6dc7bb137c0e40a37868617695b
7
- data.tar.gz: 8d696bccc8430b5d9e2ffc29c77c964e42f42890a8e7ac132aaa750505874e5fa12badff7f289f562aba5c0f5f04b0664cd559647b9c4b37716a0df4a26a3171
6
+ metadata.gz: da5488b26c318374c5b2a432729b74e92db5d73135c1fbe3cdba40698eb679ab90fc99e843cbcef664ce93cd65c859e6e64f3ff8a4ddb0d86089ef58ae2fb6bd
7
+ data.tar.gz: d6c4d2027d4b7fb5cc4df3ef8189c38ce3132cd890a8e05e4b4e39285453059194533179ec3f529c9a99e81a76b4247dc4b5e8560f745cda18ebe540bef8b5c5
@@ -1,84 +1,91 @@
1
- require 'resonance/errors/argument_error'
2
- require 'resonance/supports/converter'
1
+ require 'inflexion'
3
2
 
4
3
  module Resonance
5
- class << self
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].each do |role|
8
+ roles = [source, target, action]
9
+
10
+ roles.each do |role|
16
11
  if role.nil?
17
- raise Resonance::Errors::ArgumentError, 'Passed argument is not a valid'
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
- classify(source).class_eval <<-EOS
31
- has_many :#{pluralize(action)},
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(action)},
36
- through: :#{pluralize(action)},
36
+ has_many :#{action.progressize},
37
+ through: :#{action.pluralize},
37
38
  source: :target_#{target}
38
39
 
39
40
  def #{action}(target)
40
- if #{progressize(action)}?(target) || (self == target)
41
+ if #{action.progressize}?(target) || (self == target)
41
42
  return
42
43
  end
43
44
 
44
- #{pluralize(action)}.create(#{foreign_key[:target]}: target.id)
45
+ #{action.pluralize}.create(#{foreign_key[:target]}: target.id)
45
46
  end
46
47
 
47
48
  def un#{action}(target)
48
- unless #{progressize(action)}?(target)
49
+ unless #{action.progressize}?(target)
49
50
  return
50
51
  end
51
52
 
52
- #{pluralize(action)}.find_by(#{foreign_key[:target]}: target.id).destroy
53
+ #{action.pluralize}.find_by(#{foreign_key[:target]}: target.id).destroy
53
54
  end
54
55
 
55
- def #{progressize(action)}?(target)
56
- #{pluralize(action)}.exists?(#{foreign_key[:target]}: target.id)
56
+ def #{action.progressize}?(target)
57
+ #{action.pluralize}.exists?(#{foreign_key[:target]}: target.id)
57
58
  end
58
59
  EOS
59
60
 
60
- classify(target).class_eval <<-EOS
61
- has_many :#{pluralize(action)}_as_target,
61
+ classize(target).class_eval <<-EOS
62
+ has_many :#{action.pluralize}_as_target,
62
63
  foreign_key: :#{foreign_key[:target]},
63
- class_name: '#{classify(action)}',
64
+ class_name: '#{classize(action)}',
64
65
  dependent: :destroy
65
66
 
66
- has_many :#{peoplize(action)},
67
- through: :#{pluralize(action)}_as_target,
67
+ has_many :#{action.peopleize},
68
+ through: :#{action.pluralize}_as_target,
68
69
  source: :#{source}
69
70
 
70
- def #{pastize(action)}_by?(source)
71
- source.#{pluralize(action)}.exists?(#{foreign_key[:target]}: id)
71
+ def #{action.pastize}_by?(source)
72
+ source.#{action.pluralize}.exists?(#{foreign_key[:target]}: id)
72
73
  end
73
74
  EOS
74
75
 
75
- classify(action).class_eval <<-EOS
76
+ classize(action).class_eval <<-EOS
76
77
  belongs_to :#{source}
77
78
 
78
79
  belongs_to :target_#{target},
79
- class_name: '#{classify(target)}',
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
@@ -1,3 +1,3 @@
1
1
  module Resonance
2
- VERSION = '0.4.0'
2
+ VERSION = '0.4.1'
3
3
  end
@@ -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 'activesupport'
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.0
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-03 00:00:00.000000000 Z
11
+ date: 2016-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: activesupport
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,5 +0,0 @@
1
- module Resonance
2
- module Errors
3
- class ArgumentError < StandardError; end
4
- end
5
- end
@@ -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
@@ -1,3 +0,0 @@
1
- patches:
2
- progressize:
3
- follow: following