must_be_ordered 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9a1a7de42fce71b462168b877fea343b390e652444fd108b600acab085e698ef
4
+ data.tar.gz: 8f922bf53233ad23f302c92ea2a14cc6424d32c47cdbe016257c910ab14cad96
5
+ SHA512:
6
+ metadata.gz: 74b85a2342e60b86b7046e6b7af40bb0edc51654ac993c620b1bca194df1374652a9bb7868f241f437711edcf97ec8027e7ebb0912a666e858bdd5746caf73f7
7
+ data.tar.gz: 3aff39148e50ae49c271f548726804ac67f68b1eef9937e20e67e2b9cc431ef89ab96e6265ea25965a30bbbe537d36fa2ba340dd67b64209e584a1c0c48f2a6f
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /log/
10
+
11
+ # rspec failure tracking
12
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,11 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ - 2.4.4
6
+ - 2.3.6
7
+ before_install: gem install bundler -v 1.16.2
8
+ gemfile:
9
+ - Gemfile.rails-5.2
10
+ - Gemfile.rails-5.1
11
+ - Gemfile.rails-5.0
data/Gemfile ADDED
@@ -0,0 +1,8 @@
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 must_be_ordered.gemspec
6
+ gemspec
7
+
8
+ gem 'activerecord', '~> 5.2'
@@ -0,0 +1,68 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ must_be_ordered (0.1.0)
5
+ activerecord (>= 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (5.2.1)
11
+ activesupport (= 5.2.1)
12
+ activerecord (5.2.1)
13
+ activemodel (= 5.2.1)
14
+ activesupport (= 5.2.1)
15
+ arel (>= 9.0)
16
+ activesupport (5.2.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ arel (9.0.0)
22
+ byebug (10.0.2)
23
+ coderay (1.1.2)
24
+ concurrent-ruby (1.0.5)
25
+ diff-lcs (1.3)
26
+ i18n (1.1.0)
27
+ concurrent-ruby (~> 1.0)
28
+ method_source (0.9.0)
29
+ minitest (5.11.3)
30
+ pry (0.11.3)
31
+ coderay (~> 1.1.0)
32
+ method_source (~> 0.9.0)
33
+ pry-byebug (3.6.0)
34
+ byebug (~> 10.0)
35
+ pry (~> 0.10)
36
+ rake (10.5.0)
37
+ rspec (3.8.0)
38
+ rspec-core (~> 3.8.0)
39
+ rspec-expectations (~> 3.8.0)
40
+ rspec-mocks (~> 3.8.0)
41
+ rspec-core (3.8.0)
42
+ rspec-support (~> 3.8.0)
43
+ rspec-expectations (3.8.1)
44
+ diff-lcs (>= 1.2.0, < 2.0)
45
+ rspec-support (~> 3.8.0)
46
+ rspec-mocks (3.8.0)
47
+ diff-lcs (>= 1.2.0, < 2.0)
48
+ rspec-support (~> 3.8.0)
49
+ rspec-support (3.8.0)
50
+ sqlite3 (1.3.13)
51
+ thread_safe (0.3.6)
52
+ tzinfo (1.2.5)
53
+ thread_safe (~> 0.1)
54
+
55
+ PLATFORMS
56
+ ruby
57
+
58
+ DEPENDENCIES
59
+ activerecord (~> 5.2)
60
+ bundler (~> 1.16)
61
+ must_be_ordered!
62
+ pry-byebug
63
+ rake (~> 10.0)
64
+ rspec (~> 3.0)
65
+ sqlite3
66
+
67
+ BUNDLED WITH
68
+ 1.16.2
@@ -0,0 +1,8 @@
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 must_be_ordered.gemspec
6
+ gemspec
7
+
8
+ gem 'activerecord', '~> 5.0'
@@ -0,0 +1,8 @@
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 must_be_ordered.gemspec
6
+ gemspec
7
+
8
+ gem 'activerecord', '~> 5.1'
@@ -0,0 +1,8 @@
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 must_be_ordered.gemspec
6
+ gemspec
7
+
8
+ gem 'activerecord', '~> 5.2'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 pi-chan
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.
@@ -0,0 +1,48 @@
1
+ # MustBeOrdered
2
+
3
+ [![Build Status](https://travis-ci.org/pi-chan/must_be_ordered.svg?branch=master)](https://travis-ci.org/pi-chan/must_be_ordered)
4
+
5
+ `must_be_ordered` gem is to warn or raise error if `order` is not set to ActiveRecord's Relation.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'must_be_ordered'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+
20
+ ## Usage
21
+
22
+ Configuration:
23
+
24
+ ```ruby
25
+ # config/initializers/must_be_ordered.rb
26
+ MustBeOrdered.enabled = !Rails.env.production?
27
+
28
+ # choose action you need
29
+ MustBeOrdered.raise = true
30
+ MustBeOrdered.must_be_ordered_logger = true
31
+ MustBeOrdered.rails_logger = true
32
+ ```
33
+
34
+ Models:
35
+
36
+ ```ruby
37
+ class Item < ActoveRecord::Base
38
+ must_be_ordered
39
+ end
40
+ ```
41
+
42
+ ## Contributing
43
+
44
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pi-chan/must_be_ordered.
45
+
46
+ ## License
47
+
48
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "must_be_ordered"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,42 @@
1
+ require "active_record"
2
+
3
+ require "must_be_ordered/version"
4
+ require "must_be_ordered/notifier"
5
+ require "must_be_ordered/stack_trace_filter"
6
+ require "must_be_ordered/relation_check"
7
+
8
+ module MustBeOrdered
9
+ class << self
10
+ def enabled=(value)
11
+ @enabled = value
12
+ end
13
+
14
+ def enabled?
15
+ !!@enabled
16
+ end
17
+
18
+ def raise=(should_raise)
19
+ Notifier.exception_class = (should_raise ? MustBeOrdered::OrderNotApplied : nil)
20
+ end
21
+
22
+ def rails_logger=(active)
23
+ Notifier.rails_logger = active
24
+ end
25
+
26
+ def must_be_ordered_logger=(active)
27
+ Notifier.customized_logger = active
28
+ end
29
+ end
30
+
31
+ def self.extended(klass)
32
+ klass.class_eval do
33
+ class_attribute :__must_be_ordered__
34
+ end
35
+ end
36
+
37
+ def must_be_ordered
38
+ self.__must_be_ordered__ = true
39
+ end
40
+ end
41
+
42
+ ActiveRecord::Base.extend MustBeOrdered
@@ -0,0 +1,33 @@
1
+ module MustBeOrdered
2
+ module Notifier
3
+ module_function
4
+
5
+ def exception_class=(should_raise)
6
+ @exception_class = (should_raise ? MustBeOrdered::OrderNotApplied : nil)
7
+ end
8
+
9
+ def customized_logger=(active)
10
+ return unless active
11
+ require 'fileutils'
12
+ require 'logger'
13
+ root_path = (defined?(::Rails) ? Rails.root.to_s : Dir.pwd).to_s
14
+ FileUtils.mkdir_p(root_path + '/log')
15
+ must_be_ordered_log_file = File.open("#{root_path}/log/must_be_ordered.log", 'a+')
16
+ must_be_ordered_log_file.sync = true
17
+ @customized_logger = Logger.new(must_be_ordered_log_file)
18
+ def @customized_logger.format_message(severity, timestamp, _progname, msg)
19
+ "#{timestamp.strftime('%Y-%m-%d %H:%M:%S')}[#{severity}] #{msg}"
20
+ end
21
+ end
22
+
23
+ def rails_logger=(active)
24
+ @rails_logger = active
25
+ end
26
+
27
+ def notify(message)
28
+ @customized_logger.warn(message) if @customized_logger
29
+ Rails.logger.warn(message) if @rails_logger
30
+ raise @exception_class, message if @exception_class
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,21 @@
1
+ module MustBeOrdered
2
+ class OrderNotApplied < StandardError; end
3
+
4
+ module RelationCheck
5
+ include StackTraceFilter
6
+
7
+ private
8
+
9
+ def exec_queries(&block)
10
+ return super unless MustBeOrdered.enabled?
11
+
12
+ if klass.__must_be_ordered__? && order_values.empty?
13
+ trace = caller_in_project.join("\n")
14
+ Notifier.notify("MustBeOrdered order not applied #{klass.to_s}:\n#{trace}")
15
+ end
16
+ super
17
+ end
18
+ end
19
+ end
20
+
21
+ ActiveRecord::Relation.prepend MustBeOrdered::RelationCheck
@@ -0,0 +1,63 @@
1
+ module MustBeOrdered
2
+ module StackTraceFilter
3
+ VENDOR_PATH = '/vendor'
4
+
5
+ def caller_in_project
6
+ app_root = defined?(::Rails) ? Rails.root.to_s : Dir.pwd
7
+ vendor_root = app_root + VENDOR_PATH
8
+ bundler_path = Bundler.bundle_path.to_s
9
+ select_caller_locations do |location|
10
+ caller_path = location_as_path(location)
11
+ caller_path.include?(app_root) && !caller_path.include?(vendor_root) && !caller_path.include?(bundler_path)
12
+ end
13
+ end
14
+
15
+ private
16
+
17
+ def pattern_matches?(location, pattern)
18
+ path = location_as_path(location)
19
+ case pattern
20
+ when Array
21
+ pattern_path = pattern.first
22
+ filter = pattern.last
23
+ return false unless pattern_matches?(location, pattern_path)
24
+
25
+ case filter
26
+ when Range
27
+ filter.include?(location.lineno)
28
+ when Integer
29
+ filter == location.lineno
30
+ when String
31
+ filter == location.base_label
32
+ end
33
+ when String
34
+ path.include?(pattern)
35
+ when Regexp
36
+ path =~ pattern
37
+ end
38
+ end
39
+
40
+ def location_as_path(location)
41
+ ruby_19? ? location : location.absolute_path.to_s
42
+ end
43
+
44
+ def select_caller_locations
45
+ if ruby_19?
46
+ caller.select do |caller_path|
47
+ yield caller_path
48
+ end
49
+ else
50
+ caller_locations.select do |location|
51
+ yield location
52
+ end
53
+ end
54
+ end
55
+
56
+ def ruby_19?
57
+ if @ruby_19.nil?
58
+ @ruby_19 = Gem::Version.new(RUBY_VERSION) < Gem::Version.new('2.0.0')
59
+ end
60
+ @ruby_19
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module MustBeOrdered
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,33 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "must_be_ordered/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "must_be_ordered"
8
+ spec.version = MustBeOrdered::VERSION
9
+ spec.authors = ["pi-chan", "nof"]
10
+ spec.email = ["hiromasa.ohno.1007@gmail.com"]
11
+
12
+ spec.summary = "`must_be_ordered` gem is to warn or raise error if `order` is not set to ActiveRecord's Relation."
13
+ spec.description = ""
14
+ spec.homepage = "https://github.com/pi-chan/must_be_ordered"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_runtime_dependency 'activerecord', ['>= 5.0']
27
+
28
+ spec.add_development_dependency 'bundler', "~> 1.16"
29
+ spec.add_development_dependency 'rake', "~> 10.0"
30
+ spec.add_development_dependency 'rspec', "~> 3.0"
31
+ spec.add_development_dependency 'sqlite3'
32
+ spec.add_development_dependency 'pry-byebug'
33
+ end
metadata ADDED
@@ -0,0 +1,149 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: must_be_ordered
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - pi-chan
8
+ - nof
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-08-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '5.0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '5.0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.16'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.16'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: rspec
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '3.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '3.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: sqlite3
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - ">="
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: pry-byebug
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ description: ''
99
+ email:
100
+ - hiromasa.ohno.1007@gmail.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - Gemfile
109
+ - Gemfile.lock
110
+ - Gemfile.rails-5.0
111
+ - Gemfile.rails-5.1
112
+ - Gemfile.rails-5.2
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/must_be_ordered.rb
119
+ - lib/must_be_ordered/notifier.rb
120
+ - lib/must_be_ordered/relation_check.rb
121
+ - lib/must_be_ordered/stack_trace_filter.rb
122
+ - lib/must_be_ordered/version.rb
123
+ - must_be_ordered.gemspec
124
+ homepage: https://github.com/pi-chan/must_be_ordered
125
+ licenses:
126
+ - MIT
127
+ metadata: {}
128
+ post_install_message:
129
+ rdoc_options: []
130
+ require_paths:
131
+ - lib
132
+ required_ruby_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ version: '0'
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 2.7.7
145
+ signing_key:
146
+ specification_version: 4
147
+ summary: "`must_be_ordered` gem is to warn or raise error if `order` is not set to
148
+ ActiveRecord's Relation."
149
+ test_files: []