acts_in_relation 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 62be76e5be4d77787d8b0825f9af587e69a52ed6
4
+ data.tar.gz: 79967cc8c9a5a71a00c2992b2eeda72f5595d143
5
+ SHA512:
6
+ metadata.gz: d84eceb57ed1a4431c52c5a88762c2176ad3dd364abfd4bb0707b25475de8f4da25e4ca228b0efc68b875f3e487dacbdb4ef5f719e493b97d5134195381b1c94
7
+ data.tar.gz: 8761a5f98d9fd4a1f796e0e70d93fb03fa418a6ddcd4546b1f34028391aeb2edededbc73a5078f8e18dad6841805b9c34f319275745ef3bb840f06307b8d2c9f
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2015 kami
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'ActsInRelation'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ require 'rspec/core/rake_task'
18
+ RSpec::Core::RakeTask.new(:spec)
19
+ task default: :spec
20
+
21
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,14 @@
1
+ require 'acts_in_relation/version'
2
+
3
+ module ActsInRelation
4
+ autoload :Core, 'acts_in_relation/core'
5
+ autoload :Source, 'acts_in_relation/source'
6
+ autoload :Target, 'acts_in_relation/target'
7
+ autoload :Action, 'acts_in_relation/action'
8
+
9
+ module Supports
10
+ autoload :Verb, 'acts_in_relation/supports/verb'
11
+ end
12
+
13
+ require 'acts_in_relation/railtie' if defined?(Rails)
14
+ end
@@ -0,0 +1,10 @@
1
+ module ActsInRelation
2
+ module Action
3
+ def define
4
+ class_object.class_eval <<-RUBY
5
+ belongs_to :"#{source}"
6
+ belongs_to :"target_#{target}", class_name: "#{target.capitalize}", foreign_key: :"target_#{target}_id"
7
+ RUBY
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,58 @@
1
+ module ActsInRelation
2
+ module Core
3
+ def self.included(base)
4
+ base.extend ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def acts_in_relation(position = :self, params)
9
+ relation = Relation.new(position, params, class_name.call)
10
+ relation.define
11
+ end
12
+
13
+ private
14
+
15
+ # TODO: Implement this method to return reliable class name
16
+ def class_name
17
+ -> { caller_locations(3, 1)[0].label.match(/^\<class:(\w+)\>$/)[1].downcase.to_sym }
18
+ end
19
+ end
20
+
21
+ class Relation
22
+ def initialize(position, params, class_name)
23
+ @position = position
24
+ @params = params
25
+ @class_name = class_name
26
+ end
27
+
28
+ def define
29
+ positions = (@position == :self) ? [:source, :target] : [@position]
30
+ positions.each do |position|
31
+ extend "ActsInRelation::#{position.capitalize}".constantize
32
+ define
33
+ end
34
+ end
35
+
36
+ private
37
+
38
+ def source
39
+ @source ||= @params[:source] || @class_name
40
+ end
41
+
42
+ def target
43
+ @target ||= @params[:target] || @class_name
44
+ end
45
+
46
+ # TODO: Return instance if exists
47
+ def with
48
+ with = @params[:with]
49
+ with = with.kind_of?(Array) ? with : [with]
50
+ with.map(&:to_s)
51
+ end
52
+
53
+ def class_object
54
+ @class_object ||= @class_name.to_s.capitalize.constantize
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,11 @@
1
+ require 'rails'
2
+
3
+ module ActsInRelation
4
+ class Railtie < Rails::Railtie
5
+ initializer 'acts_in_relation' do |app|
6
+ ActiveSupport.on_load :active_record do
7
+ include ActsInRelation::Core
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,35 @@
1
+ module ActsInRelation
2
+ module Source
3
+ def define
4
+ with.each do |action|
5
+ action.extend ActsInRelation::Supports::Verb
6
+
7
+ class_object.class_eval <<-RUBY
8
+ has_many :"#{action.pluralize}",
9
+ foreign_key: :"#{source}_id",
10
+ dependent: :destroy
11
+
12
+ has_many :"#{action.progressize}",
13
+ through: :"#{action.pluralize}",
14
+ source: :"target_#{target}"
15
+
16
+ def #{action}(target)
17
+ return if #{action.progressize}?(target) || (self == target)
18
+
19
+ #{action.pluralize}.create(target_#{target}_id: target.id)
20
+ end
21
+
22
+ def un#{action}(target)
23
+ return unless #{action.progressize}?(target)
24
+
25
+ #{action.pluralize}.find_by(target_#{target}_id: target.id).destroy
26
+ end
27
+
28
+ def #{action.progressize}?(target)
29
+ #{action.pluralize}.exists?(target_#{target}_id: target.id)
30
+ end
31
+ RUBY
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,27 @@
1
+ require 'verbs'
2
+
3
+ module ActsInRelation
4
+ module Supports
5
+ module Verb
6
+ PATCHES = {
7
+ 'follow' => 'following'
8
+ }
9
+
10
+ def pastize
11
+ verb.conjugate(tense: :past).split(' ').last
12
+ end
13
+
14
+ def progressize
15
+ return PATCHES[self] if PATCHES.has_key?(self)
16
+
17
+ verb.conjugate(aspect: :progressive).split(' ').last
18
+ end
19
+
20
+ # TODO: Implement this method more logically
21
+ def peoplize
22
+ action = (last == 'e') ? chop : self
23
+ action + 'ers'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module ActsInRelation
2
+ module Target
3
+ def define
4
+ with.each do |action|
5
+ action.extend ActsInRelation::Supports::Verb
6
+
7
+ class_object.class_eval <<-RUBY
8
+ has_many :"#{action.pluralize}_as_target",
9
+ foreign_key: :"target_#{target}_id",
10
+ class_name: action.capitalize,
11
+ dependent: :destroy
12
+
13
+ has_many :"#{action.peoplize}",
14
+ through: :"#{action.pluralize}_as_target",
15
+ source: :"#{source}"
16
+
17
+ def #{action.pastize}_by?(source)
18
+ source.#{action.pluralize}.exists?(#{source}_id: source.id)
19
+ end
20
+ RUBY
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,3 @@
1
+ module ActsInRelation
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,112 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_in_relation
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - kami
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 4.2.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 4.2.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: verbs
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Rails plugin that adds a relational feature to Model, such as follow,
70
+ block, mute, or like and so on.
71
+ email:
72
+ - kami30k@gmail.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - MIT-LICENSE
78
+ - Rakefile
79
+ - lib/acts_in_relation.rb
80
+ - lib/acts_in_relation/action.rb
81
+ - lib/acts_in_relation/core.rb
82
+ - lib/acts_in_relation/railtie.rb
83
+ - lib/acts_in_relation/source.rb
84
+ - lib/acts_in_relation/supports/verb.rb
85
+ - lib/acts_in_relation/target.rb
86
+ - lib/acts_in_relation/version.rb
87
+ homepage: https://github.com/kami30k/acts_in_relation
88
+ licenses:
89
+ - MIT
90
+ metadata: {}
91
+ post_install_message:
92
+ rdoc_options: []
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ required_rubygems_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 2.2.2
108
+ signing_key:
109
+ specification_version: 4
110
+ summary: Rails plugin that adds a relational feature to Model, such as follow, block,
111
+ mute, or like and so on.
112
+ test_files: []