brace_comb 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaeb1d4b8c9364abcf4faa7f520def42a3181a69
4
- data.tar.gz: 05e4fd1cee33718bdf03e001263d447a23ac2b58
3
+ metadata.gz: 8fe368a2abc127eb6eabb97dd00821f440d0e715
4
+ data.tar.gz: 31c9abbd499456fa9fa5a4d0c734166ead74d4cc
5
5
  SHA512:
6
- metadata.gz: 414d1aa676d6b862f2391be9a822515bae2d3627d6c2c6e8e71724da5534526729009929d0979d8ca66190f1563743b6e1dfc864f879e2a51794e756d3a1342f
7
- data.tar.gz: e48fd0c6d8320910b9ea27efcf09603258b46d2eaadd421ed786bcc5dd1d090618e561140002d7dc39a3195695a962b93505945f2324402787afa5bb3f3370a9
6
+ metadata.gz: 56f9d83c4244ba7d54317b750c0f25efb4615344cd4e855bd9fb461b426b64d78619d6ae799151bd32a7ea83720be202fd0a593e17771c7615ac9ec265944661
7
+ data.tar.gz: 8c3c00d2aa57ed9308a9d73f77da66f3f9234a6861ed9a172ca98504cf197b01390e8e0a8f642f8359e60e063f0319938c83d9bf0cfafca281c3f1fef58d0ead
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ #Ignore editor configs
12
+ .idea/
13
+ .vscode/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in brace_comb.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Ankita Gupta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'brace_comb/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'brace_comb'
8
+ spec.version = BraceComb::VERSION
9
+ spec.authors = ['Ankita Gupta']
10
+ spec.email = ['ankita.gupta@honestbee.com']
11
+
12
+ spec.summary = 'Allows creation of job dependencies'
13
+ spec.description = 'Allows setting of dependenices between jobs and setting rules for dependency resolution'
14
+ spec.homepage = 'https://github.com/honestbee/brace_comb'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files`.split("\n")
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_dependency 'activerecord', ['>= 4.0', '< 5.2']
23
+ spec.add_dependency 'dry-configurable', '~> 0.6'
24
+
25
+ spec.add_development_dependency 'activesupport', '>= 4.2.5'
26
+ spec.add_development_dependency 'bundler', '~> 1.15'
27
+ spec.add_development_dependency 'generator_spec', '~> 0.9.3'
28
+ spec.add_development_dependency 'pry'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rspec-rails', '~> 3.5'
31
+ spec.add_development_dependency 'sqlite3'
32
+ spec.add_development_dependency 'with_model', '~> 1.2'
33
+ end
@@ -1,3 +1,3 @@
1
1
  module BraceComb
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
@@ -11,16 +11,9 @@ module BraceComb
11
11
 
12
12
  def inject_model_content
13
13
  content = model_contents
14
-
15
- class_path = if namespaced?
16
- class_name.to_s.split('::')
17
- else
18
- [class_name]
19
- end
20
-
14
+ class_path = [dependency_model.camelize]
21
15
  indent_depth = class_path.size - 1
22
- content = content.split('\n').map { |line| ' ' * indent_depth + line } .join('\n') << '\n'
23
-
16
+ content = content.split('\n').map { |line| ' ' * indent_depth + line } .join('\n')
24
17
  inject_into_class(model_path, class_path.last, content) if model_exists?
25
18
  end
26
19
 
@@ -40,8 +33,8 @@ module BraceComb
40
33
 
41
34
  def model_contents
42
35
  <<-CONTENT
43
- enum status: { pending: 0, resolved: 2 }
36
+ enum status: { pending: 0, resolved: 1 }
44
37
  CONTENT
45
38
  end
46
39
  end
47
- end
40
+ end
@@ -0,0 +1,7 @@
1
+ # This migration adds associations between the dependency and dependent
2
+ class AddAssociations < ActiveRecord::Migration<%= migration_version %>
3
+ def change
4
+ add_foreign_key :<%= dependency_table_name%>, :<%= dependent_table_name %>, column: :source_id, dependent: :delete
5
+ add_foreign_key :<%= dependency_table_name%>, :<%= dependent_table_name %>, column: :destination_id, dependent: :delete
6
+ end
7
+ end
@@ -0,0 +1,13 @@
1
+ # This migration creates the `dependencies` table
2
+ class Create<%= dependency_table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
3
+ def change
4
+ create_table :<%= dependency_table_name %> do |t|
5
+ t.integer :dependency_type, null: false
6
+ t.integer :status
7
+ t.integer :source_id, null: false
8
+ t.integer :destination_id, null: false
9
+ t.timestamps null: false
10
+ end
11
+ add_index :<%= dependency_table_name %>, %i(source_id destination_id)
12
+ end
13
+ end
@@ -0,0 +1,8 @@
1
+ # This migration creates the dependent table
2
+ class Create<%= dependent_table_name.camelize %> < ActiveRecord::Migration<%= migration_version %>
3
+ def change
4
+ create_table :<%= dependent_table_name %> do |t|
5
+ t.timestamps null: false
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,110 @@
1
+ require 'spec_helper'
2
+ require 'brace_comb/dependency_helper'
3
+
4
+ describe BraceComb::Helper do
5
+ dependency_class = ::BraceComb.dependency_model
6
+ dependent_class = ::BraceComb.dependent_model
7
+ with_model dependency_class do
8
+ table do |t|
9
+ t.integer :dependency_type
10
+ t.integer :status
11
+ t.integer :source_id
12
+ t.integer :destination_id
13
+ t.timestamps null: false
14
+ end
15
+ end
16
+
17
+ with_model dependent_class do
18
+ table do |t|
19
+ t.timestamps null: false
20
+ end
21
+ end
22
+ context 'dependency_helper' do
23
+ let(:source) { dependent_class.constantize.create }
24
+ let(:destination) { dependent_class.constantize.create }
25
+
26
+ context '#declare_dependency' do
27
+ before do
28
+ dependency_class.constantize.class_eval do
29
+ include BraceComb::Helper
30
+
31
+ declare_dependency type: :shopping,
32
+ resolver: :shopping_complete,
33
+ before_resolved: [:completed_status?],
34
+ after_resolved: [:complete_job]
35
+ end
36
+ end
37
+ it 'can declare dependency' do
38
+ dependency_mapping = JobDependency.instance_variable_get(:@dependency_mapping)
39
+ expect(dependency_mapping).to match(
40
+ shopping: {
41
+ resolver: :shopping_complete,
42
+ before_resolved: [:completed_status?],
43
+ after_resolved: [:complete_job]
44
+ }
45
+ )
46
+ end
47
+ end
48
+
49
+ context '#initialize_dependency' do
50
+ before do
51
+ dependency_class.constantize.class_eval do
52
+ include BraceComb::Helper
53
+ enum dependency_type: {shopping: 0}
54
+
55
+ validates :source_id, presence: true
56
+ end
57
+ end
58
+
59
+ let(:new_dependency) do
60
+ dependency_class.constantize.new.initialize_dependency(
61
+ from: source.id,
62
+ to: destination.id,
63
+ dependency_type: :shopping
64
+ )
65
+ end
66
+
67
+ it 'creates a dependency' do
68
+ expect(new_dependency.present?).to eq(true)
69
+ expect(new_dependency.source_id).to eq(source.id)
70
+ expect(new_dependency.destination_id).to eq(destination.id)
71
+ expect(new_dependency.dependency_type).to eq('shopping')
72
+ end
73
+
74
+ it 'returns false if a dependency cannot be created' do
75
+ dependency = dependency_class.constantize.new.initialize_dependency
76
+ expect(dependency.valid?).to be false
77
+ end
78
+ end
79
+
80
+ context '#initialize_dependency!' do
81
+ before do
82
+ dependency_class.constantize.class_eval do
83
+ include BraceComb::Helper
84
+
85
+ enum dependency_type: {shopping: 0}
86
+ validates :source_id, presence: true
87
+ end
88
+ end
89
+
90
+ let(:new_dependency) do
91
+ dependency_class.constantize.new.initialize_dependency!(
92
+ from: source.id,
93
+ to: destination.id,
94
+ dependency_type: :shopping
95
+ )
96
+ end
97
+
98
+ it 'creates a dependency' do
99
+ expect(new_dependency.present?).to eq(true)
100
+ expect(new_dependency.source_id).to eq(source.id)
101
+ expect(new_dependency.destination_id).to eq(destination.id)
102
+ expect(new_dependency.dependency_type).to eq('shopping')
103
+ end
104
+
105
+ it 'raises error if a dependency cannot be created' do
106
+ expect { dependency_class.constantize.new.initialize_dependency! }.to raise_error
107
+ end
108
+ end
109
+ end
110
+ end
@@ -0,0 +1,156 @@
1
+ require 'spec_helper'
2
+ require 'brace_comb/dependency_helper'
3
+ require 'brace_comb/dependency_model'
4
+ require 'pry'
5
+ describe BraceComb::Model do
6
+ dependency_class = ::BraceComb.dependency_model
7
+ dependent_class = ::BraceComb.dependent_model
8
+ with_model dependency_class do
9
+ table do |t|
10
+ t.integer :dependency_type
11
+ t.integer :status
12
+ t.integer :source_id
13
+ t.integer :destination_id
14
+ t.timestamps null: false
15
+ end
16
+ end
17
+
18
+ with_model dependent_class do
19
+ table do |t|
20
+ t.timestamps null: false
21
+ end
22
+ end
23
+
24
+ before do
25
+ dependency_class.constantize.class_eval do
26
+ include BraceComb::Helper
27
+
28
+ enum dependency_type: { shopping: 0 }
29
+ enum status: { pending: 0, resolved: 1 }
30
+
31
+ declare_dependency type: :shopping,
32
+ resolver: :mark_as_complete,
33
+ before_resolved: [:before_resolve1?, :before_resolve2?],
34
+ after_resolved: [:complete_job]
35
+ end
36
+ end
37
+
38
+ let(:source) { dependent_class.constantize.create }
39
+ let(:destination) { dependent_class.constantize.create }
40
+ let(:dependency) do
41
+ dependency_class.constantize.new.initialize_dependency!(
42
+ from: source.id,
43
+ to: destination.id,
44
+ dependency_type: :shopping
45
+ )
46
+ end
47
+
48
+ context '#resolve' do
49
+ context 'resolve using a function' do
50
+ context 'before resolve callbacks not satisfied' do
51
+ before do
52
+ class JobDependency
53
+ def before_resolve1?
54
+ true
55
+ end
56
+
57
+ def before_resolve2?
58
+ false
59
+ end
60
+
61
+ def mark_as_complete
62
+ resolved!
63
+ end
64
+ end
65
+ end
66
+
67
+ it 'does not resolve dependency if all before resolve hooks are not satisfied' do
68
+ result = dependency.resolve
69
+ expect(dependency).not_to be_resolved
70
+ expect(result).to be false
71
+ end
72
+ end
73
+
74
+ context 'before resolve callbacks are satisfied' do
75
+ before do
76
+ class JobDependency
77
+ def before_resolve1?
78
+ true
79
+ end
80
+
81
+ def before_resolve2?
82
+ true
83
+ end
84
+
85
+ def mark_as_complete
86
+ resolved!
87
+ end
88
+
89
+ def complete_job
90
+ true
91
+ end
92
+ end
93
+ end
94
+
95
+ it 'resolves dependency if no exceptions are raised' do
96
+ result = dependency.resolve
97
+ expect(dependency).not_to receive(:complete_job).and_call_original
98
+ expect(dependency).to be_resolved
99
+ expect(result).to be true
100
+ end
101
+ end
102
+ end
103
+
104
+ context 'resolve using a proc' do
105
+ before do
106
+ dependency_class.constantize.class_eval do
107
+ declare_dependency type: :shopping,
108
+ resolver: ->(data) { data.resolved! },
109
+ before_resolved: [->(_) { true }, :before_resolve?],
110
+ after_resolved: [:complete_job]
111
+ end
112
+
113
+ class JobDependency
114
+ def before_resolve?
115
+ true
116
+ end
117
+
118
+ def complete_job
119
+ true
120
+ end
121
+ end
122
+ end
123
+
124
+ it 'resolves dependency if all before resolve hooks are satisfied' do
125
+ result = dependency.resolve
126
+ expect(dependency).not_to receive(:complete_job).and_call_original
127
+ expect(dependency).to be_resolved
128
+ expect(result).to be true
129
+ end
130
+ end
131
+ end
132
+
133
+ context 'resolve' do
134
+ context 'before resolve callbacks not satisfied' do
135
+ before do
136
+ class JobDependency
137
+ def before_resolve1?
138
+ true
139
+ end
140
+
141
+ def before_resolve2?
142
+ false
143
+ end
144
+
145
+ def mark_as_complete
146
+ resolved!
147
+ end
148
+ end
149
+ end
150
+
151
+ it 'does not resolve dependency if all before resolve hooks are not satisfied' do
152
+ expect { dependency.resolve! }.to raise_error(BraceComb::Exceptions::CallbackFailure)
153
+ end
154
+ end
155
+ end
156
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ class DependentTable
3
+ def moo
4
+ dependent_class = ::BraceComb.dependent_model
5
+ with_model dependent_class do
6
+ table do |t|
7
+ t.timestamps null: false
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,23 @@
1
+ require 'generator_spec/test_case'
2
+ require 'generators/brace_comb/initializer_generator'
3
+
4
+ describe BraceComb::InitializerGenerator, type: :generator do
5
+ include GeneratorSpec::TestCase
6
+ destination File.expand_path('../tmp', __FILE__)
7
+
8
+ after(:all) { prepare_destination }
9
+
10
+ context '#create_initializer' do
11
+ before(:all) do
12
+ prepare_destination
13
+ run_generator
14
+ end
15
+
16
+ it 'creates brace comb initializer' do
17
+ assert_file 'config/initializers/brace_comb.rb', "BraceComb.setup do |config|\n"\
18
+ " config.dependent_table_name = 'jobs'\n"\
19
+ " config.dependency_table_name = 'job_dependencies'\n"\
20
+ "end"
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,53 @@
1
+ require 'generator_spec/test_case'
2
+ require 'generators/brace_comb/migration_generator'
3
+
4
+ describe BraceComb::MigrationGenerator, type: :generator do
5
+ include GeneratorSpec::TestCase
6
+ destination File.expand_path('../tmp', __FILE__)
7
+
8
+ after(:all) { prepare_destination }
9
+
10
+ context '#create_migration_file' do
11
+ before(:all) do
12
+ prepare_destination
13
+ run_generator
14
+ end
15
+
16
+ it 'generates migrations' do
17
+ parent_class = lambda {
18
+ old_version = 'ActiveRecord::Migration'
19
+ new_version = ActiveRecord::VERSION
20
+ if new_version::MAJOR >= 5
21
+ format("%s[%d.%d]", old_version, new_version::MAJOR, new_version::MINOR)
22
+ else
23
+ old_version
24
+ end
25
+ }.call
26
+ expect(destination_root).to have_structure {
27
+ directory 'db' do
28
+ directory 'migrate' do
29
+ migration 'jobs' do
30
+ contains('class CreateJobs < ' + parent_class)
31
+ contains('create_table :jobs do |t|')
32
+ contains('t.timestamps null: false')
33
+ end
34
+ migration 'job_dependencies' do
35
+ contains('create_table :job_dependencies do |t|')
36
+ contains('class CreateJobDependencies < ' + parent_class)
37
+ contains('t.integer :dependency_type, null: false')
38
+ contains('t.integer :status')
39
+ contains('t.integer :source_id, null: false')
40
+ contains('t.integer :destination_id, null: false')
41
+ contains('t.timestamps null: false')
42
+ end
43
+ migration 'add_associations' do
44
+ contains('class AddAssociations < ' + parent_class)
45
+ contains('add_foreign_key :job_dependencies, :jobs, column: :source_id, dependent: :delete')
46
+ contains('add_foreign_key :job_dependencies, :jobs, column: :destination_id, dependent: :delete')
47
+ end
48
+ end
49
+ end
50
+ }
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,29 @@
1
+ require 'generator_spec/test_case'
2
+ require 'generators/brace_comb/model_generator'
3
+
4
+ describe BraceComb::ModelGenerator, type: :generator do
5
+ include GeneratorSpec::TestCase
6
+ destination File.expand_path('../tmp', __FILE__)
7
+
8
+ after(:all) { prepare_destination }
9
+
10
+ context '#generate_model' do
11
+ before(:all) do
12
+ prepare_destination
13
+ run_generator %w('')
14
+ end
15
+
16
+ it 'creates a model' do
17
+ expect(destination_root).to have_structure {
18
+ directory 'app' do
19
+ directory 'models' do
20
+ file 'job_dependency.rb' do
21
+ contains('class JobDependency < ApplicationRecord')
22
+ contains('enum status: { pending: 0, resolved: 1 }')
23
+ end
24
+ end
25
+ end
26
+ }
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require 'active_record'
3
+ require 'active_support'
4
+ require 'brace_comb'
5
+ require 'with_model'
6
+
7
+ RSpec.configure do |config|
8
+ config.extend WithModel
9
+ end
10
+
11
+ is_jruby = RUBY_PLATFORM == 'java'
12
+ adapter = is_jruby ? 'jdbcsqlite3' : 'sqlite3'
13
+
14
+ # WithModel requires ActiveRecord::Base.connection to be established.
15
+ # If ActiveRecord already has a connection, as in a Rails app, this is unnecessary.
16
+ ActiveRecord::Base.establish_connection(adapter: adapter, database: ':memory:')
17
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: brace_comb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ankita Gupta
@@ -164,10 +164,16 @@ executables: []
164
164
  extensions: []
165
165
  extra_rdoc_files: []
166
166
  files:
167
+ - ".gitignore"
168
+ - ".rspec"
167
169
  - CODE_OF_CONDUCT.md
170
+ - Gemfile
171
+ - LICENSE.txt
168
172
  - README.md
173
+ - Rakefile
169
174
  - bin/console
170
175
  - bin/setup
176
+ - brace_comb.gemspec
171
177
  - lib/brace_comb.rb
172
178
  - lib/brace_comb/config.rb
173
179
  - lib/brace_comb/dependency_helper.rb
@@ -178,6 +184,16 @@ files:
178
184
  - lib/generators/brace_comb/initializer_generator.rb
179
185
  - lib/generators/brace_comb/migration_generator.rb
180
186
  - lib/generators/brace_comb/model_generator.rb
187
+ - lib/generators/brace_comb/templates/add_associations.rb.erb
188
+ - lib/generators/brace_comb/templates/create_dependencies.rb.erb
189
+ - lib/generators/brace_comb/templates/create_dependent.rb.erb
190
+ - spec/brace_comb/dependency_helper_spec.rb
191
+ - spec/brace_comb/dependency_model_spec.rb
192
+ - spec/factories/dependency_table.rb
193
+ - spec/generators/brace_comb/initializer_generator_spec.rb
194
+ - spec/generators/brace_comb/migration_generator_spec.rb
195
+ - spec/generators/brace_comb/model_generator_spec.rb
196
+ - spec/spec_helper.rb
181
197
  homepage: https://github.com/honestbee/brace_comb
182
198
  licenses:
183
199
  - MIT