act_with_flags 0.0.4
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 +7 -0
- data/.rubocop.yml +79 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +19 -0
- data/.watchr +56 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +142 -0
- data/LICENSE +21 -0
- data/README.md +25 -0
- data/Rakefile +23 -0
- data/act_with_flags.gemspec +23 -0
- data/lib/act_with_flags.rb +54 -0
- data/lib/act_with_flags/admin.rb +66 -0
- data/lib/act_with_flags/define.rb +58 -0
- data/lib/act_with_flags/print.rb +51 -0
- data/lib/act_with_flags/utils.rb +52 -0
- data/lib/act_with_flags/version.rb +8 -0
- data/test/any_all_none_test.rb +41 -0
- data/test/clear_test.rb +30 -0
- data/test/internal_benchmark_test.rb +55 -0
- data/test/internal_check_add_test.rb +32 -0
- data/test/internal_null_test.rb +29 -0
- data/test/internal_one_test.rb +25 -0
- data/test/legacy_test.rb +39 -0
- data/test/mask_test.rb +28 -0
- data/test/null_test.rb +17 -0
- data/test/one_test.rb +33 -0
- data/test/origin_test.rb +37 -0
- data/test/remove_from_test.rb +18 -0
- data/test/reset_test.rb +17 -0
- data/test/test_helper.rb +36 -0
- metadata +116 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: 764b0dadf19a5ee7278da0a985cfa7e7484bac02bbc22dfce2fa7b50234f652c
|
|
4
|
+
data.tar.gz: 6b4b11c64ded7579b17a152a9927afe8fe4488fe71f1099186097ef14ee9c732
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fcf79af0016d1143daff843c18e9397656e09fad8afec300a1a9588c20371df8237515bf0104390b40bece385a3fc3e24cd72dc30f38de54c89c1550d8cb4c0c
|
|
7
|
+
data.tar.gz: 822850d797a7d1f9134e9ba6150fa6455d4a065c54b9653e7931d41336490d42e98d4fd77660ebf8dfcf57286e797e20c351ea396893b17a54effa7a7d56713e
|
data/.rubocop.yml
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# inherit_from: .rubocop_todo.yml
|
|
2
|
+
|
|
3
|
+
# rubocop --auto-gen-config
|
|
4
|
+
# rubocop
|
|
5
|
+
# rubocop:disable all
|
|
6
|
+
|
|
7
|
+
#inherit_from: .rubocop_todo.yml
|
|
8
|
+
|
|
9
|
+
AllCops:
|
|
10
|
+
Include:
|
|
11
|
+
- 'app/**/*.rb'
|
|
12
|
+
- 'bin/**/*.rb'
|
|
13
|
+
- 'lib/**/*.rb'
|
|
14
|
+
|
|
15
|
+
- 'config/**/*.rb'
|
|
16
|
+
- 'db/**/*.rb'
|
|
17
|
+
- 'lib/**/*.rb'
|
|
18
|
+
- 'test/**/*.rb' # 581 offenses
|
|
19
|
+
- '**/*.gemfile'
|
|
20
|
+
- '**/*.gemspec'
|
|
21
|
+
- '**/*.rake'
|
|
22
|
+
- '**/*.ru'
|
|
23
|
+
- '**/Gemfile'
|
|
24
|
+
# - '**/Rakefile'
|
|
25
|
+
|
|
26
|
+
#AllCops:
|
|
27
|
+
# Exclude:
|
|
28
|
+
# - 'db/**/*'
|
|
29
|
+
# - 'config/**/*'
|
|
30
|
+
# - 'script/**/*'
|
|
31
|
+
# - 'bin/{rails,rake}'
|
|
32
|
+
# - 'spec/**/*'
|
|
33
|
+
# - !ruby/regexp /old_and_unused\.rb$/
|
|
34
|
+
|
|
35
|
+
Bundler/OrderedGems:
|
|
36
|
+
Exclude:
|
|
37
|
+
- 'Gemfile'
|
|
38
|
+
|
|
39
|
+
Layout/AccessModifierIndentation:
|
|
40
|
+
Enabled: false
|
|
41
|
+
Layout/AlignArray:
|
|
42
|
+
Enabled: false
|
|
43
|
+
Layout/CommentIndentation:
|
|
44
|
+
Enabled: false
|
|
45
|
+
Layout/EmptyLinesAroundAccessModifier:
|
|
46
|
+
Enabled: false
|
|
47
|
+
Layout/EmptyLinesAroundBlockBody:
|
|
48
|
+
Enabled: false
|
|
49
|
+
Layout/EmptyLinesAroundClassBody:
|
|
50
|
+
Enabled: false
|
|
51
|
+
Layout/ExtraSpacing:
|
|
52
|
+
Enabled: false
|
|
53
|
+
Layout/IndentArray:
|
|
54
|
+
Enabled: false
|
|
55
|
+
Layout/IndentationWidth:
|
|
56
|
+
Enabled: false
|
|
57
|
+
Layout/LeadingCommentSpace:
|
|
58
|
+
Enabled: false
|
|
59
|
+
Layout/Tab:
|
|
60
|
+
Enabled: false
|
|
61
|
+
|
|
62
|
+
Lint/UnusedBlockArgument:
|
|
63
|
+
Enabled: false
|
|
64
|
+
|
|
65
|
+
Style/BlockDelimiters:
|
|
66
|
+
Enabled: false
|
|
67
|
+
Style/Documentation:
|
|
68
|
+
Enabled: false
|
|
69
|
+
Style/FormatString:
|
|
70
|
+
Enabled: false
|
|
71
|
+
Style/FrozenStringLiteralComment:
|
|
72
|
+
Enabled: false
|
|
73
|
+
Style/WhenThen:
|
|
74
|
+
Exclude:
|
|
75
|
+
- '.watchr'
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
data/.ruby-gemset
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
rails-5.2
|
data/.ruby-version
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ruby-2.6.1
|
data/.travis.yml
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
language: ruby
|
|
2
|
+
sudo: false
|
|
3
|
+
|
|
4
|
+
bundler_args: --without production
|
|
5
|
+
#before_install:
|
|
6
|
+
#- gem update bundler --no-document
|
|
7
|
+
#- gem update bundler --no-rdoc --no-ri
|
|
8
|
+
script: "bundle exec rake test"
|
|
9
|
+
|
|
10
|
+
rvm:
|
|
11
|
+
- 2.6.1
|
|
12
|
+
|
|
13
|
+
gemfile:
|
|
14
|
+
# - gemfiles/rails_5.2.gemfile
|
|
15
|
+
|
|
16
|
+
matrix:
|
|
17
|
+
|
|
18
|
+
notifications:
|
|
19
|
+
email: false
|
data/.watchr
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
HH = '#' * 22 unless defined?(HH)
|
|
2
|
+
H = '#' * 5 unless defined?(H)
|
|
3
|
+
|
|
4
|
+
def usage
|
|
5
|
+
puts <<-EOS
|
|
6
|
+
Ctrl-\\ or ctrl-4 Running all tests
|
|
7
|
+
Ctrl-C Exit
|
|
8
|
+
EOS
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def run(cmd)
|
|
12
|
+
puts "#{HH} #{Time.now} #{HH}"
|
|
13
|
+
puts "#{H} #{cmd}"
|
|
14
|
+
system "/usr/bin/time --format '#{HH} Elapsed time %E' #{cmd}"
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def run_it(type, file)
|
|
18
|
+
case type
|
|
19
|
+
# when 'test'; run %Q{ruby -I"lib:test" -rubygems #{file}}
|
|
20
|
+
when 'test'; run %Q{ruby -I test #{file}}
|
|
21
|
+
# when 'test'; run %Q{rails test #{file}}
|
|
22
|
+
# when 'spec'; run %Q{spring rspec -X #{file}}
|
|
23
|
+
else; puts "#{H} unknown type: #{type}, file: #{file}"
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def run_all_tests
|
|
28
|
+
puts "\n#{HH} Running all tests #{HH}\n"
|
|
29
|
+
# %w{test spec}.each { |dir| run "spring rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
|
30
|
+
# %w{test}.each { |dir| run "spring rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
|
31
|
+
%w{test}.each { |dir| run "rake #{dir} RAILS_ENV=test" if File.exists?(dir) }
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def run_matching_files(base)
|
|
35
|
+
base = base.split('_').first
|
|
36
|
+
%w{test spec}.each { |type|
|
|
37
|
+
files = Dir["#{type}/**/*.rb"].select { |file| file =~ /#{base}_.*\.rb/ }
|
|
38
|
+
run_it type, files.join(' ') unless files.empty?
|
|
39
|
+
}
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
%w{test spec}.each { |type|
|
|
43
|
+
watch("#{type}/#{type}_helper\.rb") { run_all_tests }
|
|
44
|
+
watch("#{type}/.*/*_#{type}\.rb") { |match| run_it type, match[0] }
|
|
45
|
+
}
|
|
46
|
+
%w{rb erb haml slim}.each { |type|
|
|
47
|
+
watch("app/.*/.*\.#{type}") { |m|
|
|
48
|
+
run_matching_files("#{m[0].split('/').at(2).split('.').first}")
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
# Ctrl-\ or ctrl-4
|
|
53
|
+
Signal.trap('QUIT') { run_all_tests }
|
|
54
|
+
# Ctrl-C
|
|
55
|
+
Signal.trap('INT') { abort("Interrupted\n") }
|
|
56
|
+
usage
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
PATH
|
|
2
|
+
remote: .
|
|
3
|
+
specs:
|
|
4
|
+
act_with_flags (0.0.3)
|
|
5
|
+
|
|
6
|
+
GEM
|
|
7
|
+
remote: https://rubygems.org/
|
|
8
|
+
specs:
|
|
9
|
+
actioncable (5.2.2)
|
|
10
|
+
actionpack (= 5.2.2)
|
|
11
|
+
nio4r (~> 2.0)
|
|
12
|
+
websocket-driver (>= 0.6.1)
|
|
13
|
+
actionmailer (5.2.2)
|
|
14
|
+
actionpack (= 5.2.2)
|
|
15
|
+
actionview (= 5.2.2)
|
|
16
|
+
activejob (= 5.2.2)
|
|
17
|
+
mail (~> 2.5, >= 2.5.4)
|
|
18
|
+
rails-dom-testing (~> 2.0)
|
|
19
|
+
actionpack (5.2.2)
|
|
20
|
+
actionview (= 5.2.2)
|
|
21
|
+
activesupport (= 5.2.2)
|
|
22
|
+
rack (~> 2.0)
|
|
23
|
+
rack-test (>= 0.6.3)
|
|
24
|
+
rails-dom-testing (~> 2.0)
|
|
25
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
|
26
|
+
actionview (5.2.2)
|
|
27
|
+
activesupport (= 5.2.2)
|
|
28
|
+
builder (~> 3.1)
|
|
29
|
+
erubi (~> 1.4)
|
|
30
|
+
rails-dom-testing (~> 2.0)
|
|
31
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
|
32
|
+
activejob (5.2.2)
|
|
33
|
+
activesupport (= 5.2.2)
|
|
34
|
+
globalid (>= 0.3.6)
|
|
35
|
+
activemodel (5.2.2)
|
|
36
|
+
activesupport (= 5.2.2)
|
|
37
|
+
activerecord (5.2.2)
|
|
38
|
+
activemodel (= 5.2.2)
|
|
39
|
+
activesupport (= 5.2.2)
|
|
40
|
+
arel (>= 9.0)
|
|
41
|
+
activestorage (5.2.2)
|
|
42
|
+
actionpack (= 5.2.2)
|
|
43
|
+
activerecord (= 5.2.2)
|
|
44
|
+
marcel (~> 0.3.1)
|
|
45
|
+
activesupport (5.2.2)
|
|
46
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
|
47
|
+
i18n (>= 0.7, < 2)
|
|
48
|
+
minitest (~> 5.1)
|
|
49
|
+
tzinfo (~> 1.1)
|
|
50
|
+
arel (9.0.0)
|
|
51
|
+
benchmark-ips (2.7.2)
|
|
52
|
+
builder (3.2.3)
|
|
53
|
+
concurrent-ruby (1.1.4)
|
|
54
|
+
crass (1.0.4)
|
|
55
|
+
docile (1.3.1)
|
|
56
|
+
erubi (1.8.0)
|
|
57
|
+
globalid (0.4.2)
|
|
58
|
+
activesupport (>= 4.2.0)
|
|
59
|
+
i18n (1.5.3)
|
|
60
|
+
concurrent-ruby (~> 1.0)
|
|
61
|
+
json (2.2.0)
|
|
62
|
+
loofah (2.2.3)
|
|
63
|
+
crass (~> 1.0.2)
|
|
64
|
+
nokogiri (>= 1.5.9)
|
|
65
|
+
mail (2.7.1)
|
|
66
|
+
mini_mime (>= 0.1.1)
|
|
67
|
+
marcel (0.3.3)
|
|
68
|
+
mimemagic (~> 0.3.2)
|
|
69
|
+
method_source (0.9.2)
|
|
70
|
+
mimemagic (0.3.3)
|
|
71
|
+
mini_mime (1.0.1)
|
|
72
|
+
mini_portile2 (2.4.0)
|
|
73
|
+
minitest (5.11.3)
|
|
74
|
+
nio4r (2.3.1)
|
|
75
|
+
nokogiri (1.10.1)
|
|
76
|
+
mini_portile2 (~> 2.4.0)
|
|
77
|
+
observr (1.0.5)
|
|
78
|
+
rack (2.0.6)
|
|
79
|
+
rack-test (1.1.0)
|
|
80
|
+
rack (>= 1.0, < 3)
|
|
81
|
+
rails (5.2.2)
|
|
82
|
+
actioncable (= 5.2.2)
|
|
83
|
+
actionmailer (= 5.2.2)
|
|
84
|
+
actionpack (= 5.2.2)
|
|
85
|
+
actionview (= 5.2.2)
|
|
86
|
+
activejob (= 5.2.2)
|
|
87
|
+
activemodel (= 5.2.2)
|
|
88
|
+
activerecord (= 5.2.2)
|
|
89
|
+
activestorage (= 5.2.2)
|
|
90
|
+
activesupport (= 5.2.2)
|
|
91
|
+
bundler (>= 1.3.0)
|
|
92
|
+
railties (= 5.2.2)
|
|
93
|
+
sprockets-rails (>= 2.0.0)
|
|
94
|
+
rails-dom-testing (2.0.3)
|
|
95
|
+
activesupport (>= 4.2.0)
|
|
96
|
+
nokogiri (>= 1.6)
|
|
97
|
+
rails-html-sanitizer (1.0.4)
|
|
98
|
+
loofah (~> 2.2, >= 2.2.2)
|
|
99
|
+
railties (5.2.2)
|
|
100
|
+
actionpack (= 5.2.2)
|
|
101
|
+
activesupport (= 5.2.2)
|
|
102
|
+
method_source
|
|
103
|
+
rake (>= 0.8.7)
|
|
104
|
+
thor (>= 0.19.0, < 2.0)
|
|
105
|
+
rake (12.3.2)
|
|
106
|
+
simplecov (0.16.1)
|
|
107
|
+
docile (~> 1.1)
|
|
108
|
+
json (>= 1.8, < 3)
|
|
109
|
+
simplecov-html (~> 0.10.0)
|
|
110
|
+
simplecov-html (0.10.2)
|
|
111
|
+
sprockets (3.7.2)
|
|
112
|
+
concurrent-ruby (~> 1.0)
|
|
113
|
+
rack (> 1, < 3)
|
|
114
|
+
sprockets-rails (3.2.1)
|
|
115
|
+
actionpack (>= 4.0)
|
|
116
|
+
activesupport (>= 4.0)
|
|
117
|
+
sprockets (>= 3.0.0)
|
|
118
|
+
sqlite3 (1.3.13)
|
|
119
|
+
thor (0.20.3)
|
|
120
|
+
thread_safe (0.3.6)
|
|
121
|
+
tzinfo (1.2.5)
|
|
122
|
+
thread_safe (~> 0.1)
|
|
123
|
+
websocket-driver (0.7.0)
|
|
124
|
+
websocket-extensions (>= 0.1.0)
|
|
125
|
+
websocket-extensions (0.1.3)
|
|
126
|
+
|
|
127
|
+
PLATFORMS
|
|
128
|
+
ruby
|
|
129
|
+
|
|
130
|
+
DEPENDENCIES
|
|
131
|
+
act_with_flags!
|
|
132
|
+
benchmark-ips
|
|
133
|
+
bundler (~> 1)
|
|
134
|
+
minitest
|
|
135
|
+
observr
|
|
136
|
+
rails
|
|
137
|
+
rake (~> 12)
|
|
138
|
+
simplecov
|
|
139
|
+
sqlite3 (!= 1.4.0)
|
|
140
|
+
|
|
141
|
+
BUNDLED WITH
|
|
142
|
+
1.17.2
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2019 Dittmar Krall (matique UG)
|
|
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 all
|
|
13
|
+
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 THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# ActWithFlags
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
gem 'act_with_flags'
|
|
10
|
+
|
|
11
|
+
And then execute:
|
|
12
|
+
|
|
13
|
+
$ bundle
|
|
14
|
+
|
|
15
|
+
Or install it yourself as:
|
|
16
|
+
|
|
17
|
+
$ gem install act_with_flags
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
## License MIT
|
|
21
|
+
|
|
22
|
+
ActWithFlags is Copyright (c) 2019 [Dittmar Krall](matique.com UG) and
|
|
23
|
+
is released under the MIT license:
|
|
24
|
+
|
|
25
|
+
* https://opensource.org/licenses/MIT
|
data/Rakefile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
require "bundler/setup"
|
|
2
|
+
|
|
3
|
+
require 'rake/testtask'
|
|
4
|
+
|
|
5
|
+
desc 'Test the act_with_flags plugin.'
|
|
6
|
+
Rake::TestTask.new(:test) do |t|
|
|
7
|
+
t.libs << 'lib'
|
|
8
|
+
t.libs << 'test'
|
|
9
|
+
t.pattern = 'test/**/*_test.rb'
|
|
10
|
+
t.verbose = false
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
task :default => :test
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
=begin
|
|
17
|
+
# Add your own tasks in files placed in lib/tasks ending in .rake,
|
|
18
|
+
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
|
|
19
|
+
|
|
20
|
+
require_relative 'config/application'
|
|
21
|
+
|
|
22
|
+
Rails.application.load_tasks
|
|
23
|
+
=end
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
lib = File.expand_path('lib', __dir__)
|
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
3
|
+
require 'act_with_flags/version'
|
|
4
|
+
|
|
5
|
+
Gem::Specification.new do |s|
|
|
6
|
+
s.name = 'act_with_flags'
|
|
7
|
+
s.version = ActWithFlags::VERSION
|
|
8
|
+
s.summary = %(act_with_flags gem)
|
|
9
|
+
s.description = %(Handles flags in a Rails model instance)
|
|
10
|
+
s.authors = ['Dittmar Krall']
|
|
11
|
+
s.email = ['dittmar.krall@matique.de']
|
|
12
|
+
s.homepage = 'http://matique.de'
|
|
13
|
+
|
|
14
|
+
s.license = 'MIT'
|
|
15
|
+
s.platform = Gem::Platform::RUBY
|
|
16
|
+
|
|
17
|
+
s.files = `git ls-files -z`.split("\x0")
|
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
|
19
|
+
s.require_paths = ['lib']
|
|
20
|
+
|
|
21
|
+
s.add_development_dependency 'bundler', '~> 1'
|
|
22
|
+
s.add_development_dependency 'rake', '~> 12'
|
|
23
|
+
end
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
# Principles:
|
|
5
|
+
# POLA
|
|
6
|
+
# KISS
|
|
7
|
+
# YAGNI
|
|
8
|
+
# POLS
|
|
9
|
+
# DEI
|
|
10
|
+
# TDD considered harmful
|
|
11
|
+
|
|
12
|
+
class << ActiveRecord::Base
|
|
13
|
+
|
|
14
|
+
attr_accessor :act_with_flags
|
|
15
|
+
|
|
16
|
+
def add_to_flags(*flags, origin: :flags, **hash)
|
|
17
|
+
#p "act_with_flags: add_to_flags #{flags.inspect}"
|
|
18
|
+
#p "act_with_flags: origin #{origin.inspect}"
|
|
19
|
+
#p "act_with_flags: hash #{hash.inspect}"
|
|
20
|
+
|
|
21
|
+
@act_with_flags ||= ActWithFlags::Admin.new self
|
|
22
|
+
if origin.is_a?(Integer)
|
|
23
|
+
hash[:origin] = origin
|
|
24
|
+
else
|
|
25
|
+
@act_with_flags.origin = origin
|
|
26
|
+
@act_with_flags.delete_mask_et_all
|
|
27
|
+
@act_with_flags.add_mask_et_all origin
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
flags.each { |name| @act_with_flags.add_accessor(name, nil) }
|
|
31
|
+
hash.each { |name, pos| @act_with_flags.add_accessor(name, pos) }
|
|
32
|
+
|
|
33
|
+
@act_with_flags
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def remove_from_flags(*flags)
|
|
37
|
+
#p "remove_from_flags #{flags.inspect}"
|
|
38
|
+
flags.each { |name| @act_with_flags.remove_accessor(name) }
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def clear_flags_at_save(*flags)
|
|
42
|
+
#p "clear_flags_at_save #{flags.inspect}"
|
|
43
|
+
flags.each { |name| @act_with_flags.add_to_delete_mask(name) }
|
|
44
|
+
@act_with_flags.before_save
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__)))
|
|
50
|
+
require 'act_with_flags/version'
|
|
51
|
+
require 'act_with_flags/utils'
|
|
52
|
+
require 'act_with_flags/define'
|
|
53
|
+
require 'act_with_flags/admin'
|
|
54
|
+
require 'act_with_flags/print'
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class ActWithFlags::Admin
|
|
5
|
+
|
|
6
|
+
attr_reader :model
|
|
7
|
+
attr_reader :origin
|
|
8
|
+
attr_reader :delete_mask
|
|
9
|
+
|
|
10
|
+
def initialize(model)
|
|
11
|
+
@model = model
|
|
12
|
+
@origin = :flags
|
|
13
|
+
@map = {}
|
|
14
|
+
@delete_mask = 0
|
|
15
|
+
@max_position = 128 - 1
|
|
16
|
+
@boolean_hash = {}
|
|
17
|
+
[true, 'true', 1, '1'].each { |x| @boolean_hash[x] = true }
|
|
18
|
+
[false, 'false', 0, '0'].each { |x| @boolean_hash[x] = false }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def reset_model(model)
|
|
22
|
+
initialize model
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def names
|
|
26
|
+
@map.keys.sort
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def to_boolean(value)
|
|
30
|
+
res = @boolean_hash[value]
|
|
31
|
+
return res unless res.nil?
|
|
32
|
+
|
|
33
|
+
raise "invalid boolean <#{value}>"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def origin=(name)
|
|
37
|
+
raise 'invalid update of origin' unless @map.empty? || (origin == name)
|
|
38
|
+
@origin = name
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def position(name)
|
|
42
|
+
@map[name]
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def mask(*names)
|
|
46
|
+
names.inject(0) { |msk, name| msk | ( 1 << position(name) ) }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def all?(*names)
|
|
50
|
+
names.inject(0) { |msk, name| msk | ( 1 << position(name) ) }
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def add(name, pos)
|
|
54
|
+
values = @map.values
|
|
55
|
+
pos ||= (0..@max_position).detect { |i| !values.include?(i) }
|
|
56
|
+
raise "invalid position '#{name} @ #{pos}'" unless pos
|
|
57
|
+
raise "name in use '#{name} @ #{pos}'" if @map.key?(name)
|
|
58
|
+
raise "position in use '#{name} @ #{pos}'" if @map.value?(pos)
|
|
59
|
+
@map[name] = pos
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def add_to_delete_mask(name)
|
|
63
|
+
@delete_mask |= mask(name)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class ActWithFlags::Admin
|
|
5
|
+
|
|
6
|
+
def add_accessors(origin, accessor, mask)
|
|
7
|
+
model.class_eval %(
|
|
8
|
+
def #{accessor}
|
|
9
|
+
#{accessor}?
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def #{accessor}?
|
|
13
|
+
!( self.#{origin} & #{mask} ).zero?
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def #{accessor}=(value)
|
|
17
|
+
self.#{origin} ||= 0
|
|
18
|
+
if self.class.act_with_flags.to_boolean(value)
|
|
19
|
+
self.#{origin} |= #{mask}
|
|
20
|
+
true
|
|
21
|
+
else
|
|
22
|
+
self.#{origin} &= ~#{mask}
|
|
23
|
+
false
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def remove_accessor(accessor)
|
|
30
|
+
my_undef model, accessor, "#{accessor}?", "#{accessor}="
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def validate_accessor(*names)
|
|
34
|
+
names.each { |acc|
|
|
35
|
+
raise "redefining #{acc} rejected" if model.method_defined?(acc)
|
|
36
|
+
}
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def my_undef(*names)
|
|
40
|
+
names.each { |name|
|
|
41
|
+
model.class_eval %(
|
|
42
|
+
begin
|
|
43
|
+
undef #{name}
|
|
44
|
+
rescue
|
|
45
|
+
end
|
|
46
|
+
)
|
|
47
|
+
}
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def before_save
|
|
51
|
+
model.class_eval %(
|
|
52
|
+
before_save do |row|
|
|
53
|
+
row.#{origin} &= ~row.class.act_with_flags.delete_mask
|
|
54
|
+
end
|
|
55
|
+
)
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
# frozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class ActWithFlags::Admin
|
|
5
|
+
|
|
6
|
+
def to_s
|
|
7
|
+
res = []
|
|
8
|
+
res << title('Variables')
|
|
9
|
+
res << variables(:origin, :max_position, :boolean_hash)
|
|
10
|
+
res << variables(:delete_mask)
|
|
11
|
+
|
|
12
|
+
res << title('Flags sorted alfabetically')
|
|
13
|
+
@map.sort.each { |key, pos| res << "#{key} #{position(key)}" }
|
|
14
|
+
|
|
15
|
+
res << title('Flags sorted by position')
|
|
16
|
+
@map.sort.sort_by(&:last).each { |key, pos|
|
|
17
|
+
res << "#{key} #{position(key)}"
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
res << title('Flags and mask; sorted alfabetically')
|
|
21
|
+
@map.sort.each { |key, pos|
|
|
22
|
+
res << "#{key} #{sprintf('0x%08X', mask(key))}"
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
res << title('FLAG assignment; sorted alfabetically')
|
|
26
|
+
@map.sort.each { |key, pos|
|
|
27
|
+
res << "FLAG_#{key.upcase} = #{sprintf('0x%08X', mask(key))}"
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
res << title('FLAG assignment; sorted by position')
|
|
31
|
+
@map.sort.sort_by(&:last).each { |key, pos|
|
|
32
|
+
res << "FLAG_#{key.upcase} = #{sprintf('0x%08X', mask(key))}"
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
res.flatten.join("\n")
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
def title(msg)
|
|
40
|
+
sep = '#' * 10
|
|
41
|
+
['', "#{sep} #{msg} #{sep}"]
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def variables(*names)
|
|
45
|
+
names.collect { |name|
|
|
46
|
+
value = instance_variable_get(:"@#{name}")
|
|
47
|
+
"#{name} #{value}"
|
|
48
|
+
}
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
end
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# xrubocop:disable all
|
|
2
|
+
# xfrozen_string_literal: true
|
|
3
|
+
|
|
4
|
+
class ActWithFlags::Admin
|
|
5
|
+
|
|
6
|
+
def add_accessor(name, pos)
|
|
7
|
+
#p "** act_with_flags: add_accessor '#{name} @ #{pos}'"
|
|
8
|
+
accessor = name.to_sym
|
|
9
|
+
validate_accessor accessor, "#{accessor}?", "#{accessor}="
|
|
10
|
+
|
|
11
|
+
add accessor, pos
|
|
12
|
+
mask = mask(accessor)
|
|
13
|
+
origin = self.origin
|
|
14
|
+
add_accessors(origin, accessor, mask)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def add_mask_et_all(origin)
|
|
18
|
+
model.class_eval %(
|
|
19
|
+
def flags_mask(*names)
|
|
20
|
+
self.class.act_with_flags.mask(*names)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def flags_any?(*names)
|
|
24
|
+
mask = self.class.act_with_flags.mask(*names)
|
|
25
|
+
!( self.#{origin} & mask ).zero?
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def flags_all?(*names)
|
|
29
|
+
mask = self.class.act_with_flags.mask(*names)
|
|
30
|
+
( self.#{origin} & mask ) == mask
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def flags_none?(*names)
|
|
34
|
+
mask = self.class.act_with_flags.mask(*names)
|
|
35
|
+
( self.#{origin} & mask ).zero?
|
|
36
|
+
end
|
|
37
|
+
)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def delete_mask_et_all
|
|
41
|
+
my_undef :flags_mask, :flags_any?, :flags_all?, :flags_none?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def reset
|
|
45
|
+
delete_mask_et_all
|
|
46
|
+
names.each { |name|
|
|
47
|
+
remove_accessor name
|
|
48
|
+
}
|
|
49
|
+
self.reset_model model
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'any? all? and none?' do
|
|
4
|
+
let(:order) { Order.new }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'checks any?' do
|
|
12
|
+
order.a = true
|
|
13
|
+
assert order.flags_any?(:a, :b)
|
|
14
|
+
order.a = false
|
|
15
|
+
refute order.flags_any?(:a, :b)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'checks any? #2' do
|
|
19
|
+
order.b = true
|
|
20
|
+
assert order.flags_any?(:a, :b)
|
|
21
|
+
order.b = false
|
|
22
|
+
refute order.flags_any?(:a, :b)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'checks all?' do
|
|
26
|
+
order.a = order.b = true
|
|
27
|
+
assert order.flags_all?(:a, :b)
|
|
28
|
+
order.a = false
|
|
29
|
+
refute order.flags_all?(:a, :b)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
it 'checks none? #2' do
|
|
33
|
+
order.a = order.b = true
|
|
34
|
+
refute order.flags_none?(:a, :b)
|
|
35
|
+
order.a = false
|
|
36
|
+
refute order.flags_none?(:a, :b)
|
|
37
|
+
order.b = false
|
|
38
|
+
assert order.flags_none?(:a, :b)
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
end
|
data/test/clear_test.rb
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Clear Flags at Save' do
|
|
4
|
+
let(:admin) { Order.act_with_flags }
|
|
5
|
+
let(:order) { Order.new }
|
|
6
|
+
|
|
7
|
+
def setup
|
|
8
|
+
reset_order
|
|
9
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
10
|
+
order.a = order.b = order.c = true
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'checks sanity' do
|
|
14
|
+
assert_equal 0x8a, order.flags
|
|
15
|
+
assert order.b
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'clear flags during save' do
|
|
19
|
+
Order.clear_flags_at_save :b
|
|
20
|
+
order.save
|
|
21
|
+
order.reload
|
|
22
|
+
assert_equal 0x0a, order.flags
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
it 'does not remove accessor' do
|
|
26
|
+
Order.clear_flags_at_save :b
|
|
27
|
+
Order.respond_to? :b
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# For development purposes; do not waste your tine reading it!
|
|
2
|
+
# YAGNI
|
|
3
|
+
# rubocop:disable all
|
|
4
|
+
|
|
5
|
+
require 'test_helper'
|
|
6
|
+
require 'benchmark'
|
|
7
|
+
require 'benchmark/ips'
|
|
8
|
+
# ENV['MORE'] = 'true'
|
|
9
|
+
|
|
10
|
+
describe 'Internal timings' do
|
|
11
|
+
let(:order) { Order.new }
|
|
12
|
+
|
|
13
|
+
def setup
|
|
14
|
+
reset_order
|
|
15
|
+
Order.add_to_flags :blocked
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'times ips' do
|
|
19
|
+
return unless ENV['MORE']
|
|
20
|
+
|
|
21
|
+
Order.add_to_flags :blocked, reset: :hard
|
|
22
|
+
order = Order.new
|
|
23
|
+
|
|
24
|
+
Benchmark.ips do |x|
|
|
25
|
+
x.report('assign true : ') { order.blocked = true }
|
|
26
|
+
x.report('assign false: ') { order.blocked = false }
|
|
27
|
+
x.report('assign "false": ') { order.blocked = 'false' }
|
|
28
|
+
x.report('x = order.blocked? ') { x = order.blocked? }
|
|
29
|
+
x.report('x = order.blocked ') { x = order.blocked }
|
|
30
|
+
|
|
31
|
+
x.compare!
|
|
32
|
+
end
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
class BenchFoo < Minitest::Benchmark
|
|
38
|
+
|
|
39
|
+
def bench_order_blocked
|
|
40
|
+
return unless ENV['MORE']
|
|
41
|
+
|
|
42
|
+
n = 1_000_000
|
|
43
|
+
n = 100_000
|
|
44
|
+
n = 10_000
|
|
45
|
+
Order.add_to_flags :blocked, reset: :hard
|
|
46
|
+
order = Order.new
|
|
47
|
+
assert_performance_constant do |input|
|
|
48
|
+
n.times do
|
|
49
|
+
order.blocked = true
|
|
50
|
+
order.blocked = !order.blocked
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
end
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Internal check add flag' do
|
|
4
|
+
let(:admin) { Order.act_with_flags }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'skip reserved position' do
|
|
12
|
+
Order.add_to_flags :xx
|
|
13
|
+
assert_equal 0, admin.position(:xx)
|
|
14
|
+
Order.add_to_flags :yy
|
|
15
|
+
assert_equal 2, admin.position(:yy)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'rejects redefinition' do
|
|
19
|
+
Order.add_to_flags :z
|
|
20
|
+
assert_raises { Order.add_to_flags :z }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'rejects reuse of position' do
|
|
24
|
+
assert_raises { Order.add_to_flags qq: 1 }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
it 'coverage to_s' do
|
|
28
|
+
res = admin.to_s
|
|
29
|
+
puts res if ENV['MORE']
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Internal Null' do
|
|
4
|
+
let(:admin) { Order.act_with_flags }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'respond to act_with_flags' do
|
|
12
|
+
assert Order.respond_to?(:act_with_flags)
|
|
13
|
+
refute_nil Order.act_with_flags
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'tests to_boolean' do
|
|
17
|
+
assert admin.to_boolean(true)
|
|
18
|
+
refute admin.to_boolean(false)
|
|
19
|
+
assert_raises { flags.to_boolean(nil) }
|
|
20
|
+
assert_raises { flags.to_boolean(2) }
|
|
21
|
+
assert_raises { flags.to_boolean('unknown') }
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'tests a simple administration: names' do
|
|
25
|
+
refute_nil admin.names
|
|
26
|
+
assert_equal [], admin.names
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
end
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Internal One Flag' do
|
|
4
|
+
let(:flag) { :blocked }
|
|
5
|
+
let(:admin) { Order.act_with_flags }
|
|
6
|
+
let(:order) { Order.new }
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
reset_order
|
|
10
|
+
Order.add_to_flags flag
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'test Order.act_with_flags' do
|
|
14
|
+
refute_nil admin
|
|
15
|
+
assert_equal admin, order.class.act_with_flags
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
it 'checks definition of methods for flag' do
|
|
19
|
+
msg = "method '#{flag}' not defined'
|
|
20
|
+
assert order.respond_to?("#{flag}"), msg
|
|
21
|
+
assert order.respond_to?("#{flag}?"), msg
|
|
22
|
+
assert order.respond_to?("#{flag}="), msg
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
end
|
data/test/legacy_test.rb
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
require 'test_helper'
|
|
3
|
+
|
|
4
|
+
describe 'Legacy Flag' do
|
|
5
|
+
let(:order) { Order.new }
|
|
6
|
+
|
|
7
|
+
def setup
|
|
8
|
+
reset_order
|
|
9
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'set true' do
|
|
13
|
+
test3 true, true, true
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'set false' do
|
|
17
|
+
test3 false, false, false
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
it 'set mixture' do
|
|
21
|
+
test3 false, true, false
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
it 'set mixture #2' do
|
|
25
|
+
test3 true, false, true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
private
|
|
29
|
+
def test3(a, b, c)
|
|
30
|
+
order.a = a
|
|
31
|
+
order.b = b
|
|
32
|
+
order.c = c
|
|
33
|
+
|
|
34
|
+
assert_equal a, order.a
|
|
35
|
+
assert_equal b, order.b
|
|
36
|
+
assert_equal c, order.c
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
end
|
data/test/mask_test.rb
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'mask' do
|
|
4
|
+
let(:admin) { Order.act_with_flags }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'mask empty' do
|
|
12
|
+
assert_equal 0x00, admin.mask
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'mask of one flag' do
|
|
16
|
+
assert_equal 0x80, admin.mask(:b)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
it 'mask of several flags' do
|
|
20
|
+
assert_equal 0x8a, admin.mask(:a, :b, :c)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'order is not relevant' do
|
|
24
|
+
mask = admin.mask(:a, :b, :c)
|
|
25
|
+
assert_equal mask, admin.mask(:c, :b, :a)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
end
|
data/test/null_test.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'No Flags' do
|
|
4
|
+
|
|
5
|
+
def setup
|
|
6
|
+
reset_order
|
|
7
|
+
Order.add_to_flags
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
it 'respond to ActWithFlags API' do
|
|
11
|
+
assert Order.respond_to?(:add_to_flags)
|
|
12
|
+
assert Order.respond_to?(:remove_from_flags)
|
|
13
|
+
assert Order.respond_to?(:clear_flags_at_save)
|
|
14
|
+
assert Order.respond_to?(:act_with_flags)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
data/test/one_test.rb
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
require 'test_helper'
|
|
3
|
+
|
|
4
|
+
describe 'One Flag' do
|
|
5
|
+
let(:flag) { :blocked }
|
|
6
|
+
let(:order) { Order.new }
|
|
7
|
+
|
|
8
|
+
def setup
|
|
9
|
+
reset_order
|
|
10
|
+
Order.add_to_flags flag
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
it 'set flag (:blocked)' do
|
|
14
|
+
order.blocked = true
|
|
15
|
+
assert_equal true, order.blocked
|
|
16
|
+
assert_equal true, order.blocked?
|
|
17
|
+
|
|
18
|
+
order.blocked = 'false'
|
|
19
|
+
assert_equal false, order.blocked
|
|
20
|
+
assert_equal false, order.blocked?
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
it 'rejects redefining' do
|
|
24
|
+
assert_raises { Order.add_to_flags :id }
|
|
25
|
+
assert_raises { Order.add_to_flags flag }
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
it 'rejects redefining #2' do
|
|
29
|
+
Order.add_to_flags :berta
|
|
30
|
+
assert_raises { Order.add_to_flags :berta }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
end
|
data/test/origin_test.rb
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Testing origin' do
|
|
4
|
+
let(:admin) { Order.act_with_flags }
|
|
5
|
+
let(:order) { Order.new }
|
|
6
|
+
|
|
7
|
+
def setup
|
|
8
|
+
reset_order
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'origin category' do
|
|
12
|
+
Order.add_to_flags :x, origin: :category
|
|
13
|
+
assert_equal :category, admin.origin
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
it 'origin category #2' do
|
|
17
|
+
Order.add_to_flags :x, origin: :category
|
|
18
|
+
assert_raises { Order.add_to_flags origin: :category2 }
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
it 'origin category #3' do
|
|
22
|
+
Order.add_to_flags :x, origin: :category
|
|
23
|
+
Order.add_to_flags origin: :category
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
it 'origin default' do
|
|
27
|
+
Order.add_to_flags :x
|
|
28
|
+
assert_equal :flags, admin.origin
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
it 'origin 1' do
|
|
32
|
+
Order.add_to_flags origin: 1
|
|
33
|
+
assert_equal :flags, admin.origin
|
|
34
|
+
assert order.respond_to?(:origin)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Delete from Flags' do
|
|
4
|
+
let(:order) { Order.new }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags a: 1, b: 7, c: 3
|
|
9
|
+
order.a = order.b = order.c = true
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
it 'remove accessors' do
|
|
13
|
+
Order.remove_from_flags :b
|
|
14
|
+
assert_raises { order.b }
|
|
15
|
+
refute Order.respond_to?(:b)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
end
|
data/test/reset_test.rb
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
describe 'Testing reset' do
|
|
4
|
+
let(:order) { Order.new }
|
|
5
|
+
|
|
6
|
+
def setup
|
|
7
|
+
reset_order
|
|
8
|
+
Order.add_to_flags :a
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
it 'reset hard' do
|
|
12
|
+
refute_equal [], Order.add_to_flags.names
|
|
13
|
+
Order.act_with_flags.reset if Order.act_with_flags
|
|
14
|
+
assert_equal [], Order.add_to_flags.names
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
end
|
data/test/test_helper.rb
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
# rubocop:disable all
|
|
2
|
+
|
|
3
|
+
if ENV['MORE']
|
|
4
|
+
require 'simplecov'
|
|
5
|
+
SimpleCov.start do
|
|
6
|
+
add_filter 'test'
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
require 'rubygems'
|
|
11
|
+
require 'minitest/autorun'
|
|
12
|
+
require 'minitest/benchmark'
|
|
13
|
+
require 'active_record'
|
|
14
|
+
require_relative '../lib/act_with_flags.rb'
|
|
15
|
+
|
|
16
|
+
ENV['RAILS_ENV'] ||= 'test'
|
|
17
|
+
|
|
18
|
+
ActiveRecord::Base.establish_connection(
|
|
19
|
+
adapter: 'sqlite3',
|
|
20
|
+
database: 'db/flags.sqlite3'
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
ActiveRecord::Schema.define do
|
|
24
|
+
create_table 'orders', force: true do |t|
|
|
25
|
+
t.integer :flags
|
|
26
|
+
t.string :bigflags
|
|
27
|
+
t.integer :category
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
class Order < ActiveRecord::Base
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def reset_order
|
|
35
|
+
Order.act_with_flags.reset if Order.act_with_flags
|
|
36
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: act_with_flags
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.0.4
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dittmar Krall
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2019-03-03 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '12'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '12'
|
|
41
|
+
description: Handles flags in a Rails model instance
|
|
42
|
+
email:
|
|
43
|
+
- dittmar.krall@matique.de
|
|
44
|
+
executables: []
|
|
45
|
+
extensions: []
|
|
46
|
+
extra_rdoc_files: []
|
|
47
|
+
files:
|
|
48
|
+
- ".rubocop.yml"
|
|
49
|
+
- ".ruby-gemset"
|
|
50
|
+
- ".ruby-version"
|
|
51
|
+
- ".travis.yml"
|
|
52
|
+
- ".watchr"
|
|
53
|
+
- Gemfile
|
|
54
|
+
- Gemfile.lock
|
|
55
|
+
- LICENSE
|
|
56
|
+
- README.md
|
|
57
|
+
- Rakefile
|
|
58
|
+
- act_with_flags.gemspec
|
|
59
|
+
- lib/act_with_flags.rb
|
|
60
|
+
- lib/act_with_flags/admin.rb
|
|
61
|
+
- lib/act_with_flags/define.rb
|
|
62
|
+
- lib/act_with_flags/print.rb
|
|
63
|
+
- lib/act_with_flags/utils.rb
|
|
64
|
+
- lib/act_with_flags/version.rb
|
|
65
|
+
- test/any_all_none_test.rb
|
|
66
|
+
- test/clear_test.rb
|
|
67
|
+
- test/internal_benchmark_test.rb
|
|
68
|
+
- test/internal_check_add_test.rb
|
|
69
|
+
- test/internal_null_test.rb
|
|
70
|
+
- test/internal_one_test.rb
|
|
71
|
+
- test/legacy_test.rb
|
|
72
|
+
- test/mask_test.rb
|
|
73
|
+
- test/null_test.rb
|
|
74
|
+
- test/one_test.rb
|
|
75
|
+
- test/origin_test.rb
|
|
76
|
+
- test/remove_from_test.rb
|
|
77
|
+
- test/reset_test.rb
|
|
78
|
+
- test/test_helper.rb
|
|
79
|
+
homepage: http://matique.de
|
|
80
|
+
licenses:
|
|
81
|
+
- MIT
|
|
82
|
+
metadata: {}
|
|
83
|
+
post_install_message:
|
|
84
|
+
rdoc_options: []
|
|
85
|
+
require_paths:
|
|
86
|
+
- lib
|
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
88
|
+
requirements:
|
|
89
|
+
- - ">="
|
|
90
|
+
- !ruby/object:Gem::Version
|
|
91
|
+
version: '0'
|
|
92
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - ">="
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0'
|
|
97
|
+
requirements: []
|
|
98
|
+
rubygems_version: 3.0.1
|
|
99
|
+
signing_key:
|
|
100
|
+
specification_version: 4
|
|
101
|
+
summary: act_with_flags gem
|
|
102
|
+
test_files:
|
|
103
|
+
- test/any_all_none_test.rb
|
|
104
|
+
- test/clear_test.rb
|
|
105
|
+
- test/internal_benchmark_test.rb
|
|
106
|
+
- test/internal_check_add_test.rb
|
|
107
|
+
- test/internal_null_test.rb
|
|
108
|
+
- test/internal_one_test.rb
|
|
109
|
+
- test/legacy_test.rb
|
|
110
|
+
- test/mask_test.rb
|
|
111
|
+
- test/null_test.rb
|
|
112
|
+
- test/one_test.rb
|
|
113
|
+
- test/origin_test.rb
|
|
114
|
+
- test/remove_from_test.rb
|
|
115
|
+
- test/reset_test.rb
|
|
116
|
+
- test/test_helper.rb
|