act_with_flags 0.0.4 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +7 -0
  3. data/.rubocop.yml +3 -74
  4. data/.ruby-gemset +1 -1
  5. data/.ruby-version +1 -1
  6. data/.travis.yml +5 -1
  7. data/.watchr +6 -14
  8. data/Appraisals +7 -0
  9. data/Gemfile +1 -4
  10. data/Gemfile.lock +121 -76
  11. data/MIT-LICENSE +20 -0
  12. data/README.md +3 -1
  13. data/Rakefile +3 -15
  14. data/act_with_flags.gemspec +7 -2
  15. data/gemfiles/rails_5.2.gemfile +12 -0
  16. data/gemfiles/rails_6.0.gemfile +12 -0
  17. data/lib/act_with_flags/admin.rb +8 -6
  18. data/lib/act_with_flags/define.rb +28 -7
  19. data/lib/act_with_flags/print.rb +1 -1
  20. data/lib/act_with_flags/utils.rb +2 -3
  21. data/lib/act_with_flags/version.rb +13 -4
  22. data/test/any_all_none_test.rb +4 -2
  23. data/test/clear_test.rb +1 -1
  24. data/test/inheritance_any_test.rb +56 -0
  25. data/test/inheritance_test.rb +73 -0
  26. data/test/internal/app/assets/config/manifest.js +1 -0
  27. data/test/internal/app/controllers/application_controller.rb +3 -0
  28. data/test/internal/app/controllers/orders_controller.rb +16 -0
  29. data/test/internal/app/models/application_record.rb +3 -0
  30. data/test/internal/app/models/order.rb +9 -0
  31. data/test/internal/app/views/layouts/application.html.erb +12 -0
  32. data/test/internal/config/database.yml +3 -0
  33. data/test/internal/config/routes.rb +4 -0
  34. data/test/internal/config/secrets.yml +8 -0
  35. data/test/internal/db/schema.rb +13 -0
  36. data/test/internal_benchmark_test.rb +5 -8
  37. data/test/internal_check_add_test.rb +4 -0
  38. data/test/internal_null_test.rb +3 -3
  39. data/test/internal_one_test.rb +2 -0
  40. data/test/legacy_test.rb +1 -1
  41. data/test/one_test.rb +1 -1
  42. data/test/origin_test.rb +2 -0
  43. data/test/remove_from_test.rb +1 -1
  44. data/test/reset_test.rb +1 -1
  45. data/test/string_test.rb +26 -0
  46. data/test/test_helper.rb +8 -25
  47. metadata +92 -6
  48. data/LICENSE +0 -21
@@ -0,0 +1,20 @@
1
+ Copyright 2019-2020 Dittmar Krall - www.matique.com
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/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # ActWithFlags
2
+ [![Gem Version](https://badge.fury.io/rb/act_with_flags.png)](http://badge.fury.io/rb/act_with_flags)
3
+ [![Build Status](https://travis-ci.org/matique/act_with_flags.png?branch=master)](https://travis-ci.org/matique/act_with_flags)
2
4
 
3
-
5
+ Required by key.matique.
4
6
 
5
7
  ## Installation
6
8
 
data/Rakefile CHANGED
@@ -1,23 +1,11 @@
1
- require "bundler/setup"
2
-
3
1
  require 'rake/testtask'
4
2
 
5
- desc 'Test the act_with_flags plugin.'
6
- Rake::TestTask.new(:test) do |t|
3
+ desc 'Run the tests.'
4
+ Rake::TestTask.new do |t|
7
5
  t.libs << 'lib'
8
6
  t.libs << 'test'
9
7
  t.pattern = 'test/**/*_test.rb'
10
8
  t.verbose = false
11
9
  end
12
10
 
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
11
+ task default: :test
@@ -18,6 +18,11 @@ Gem::Specification.new do |s|
18
18
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
19
19
  s.require_paths = ['lib']
20
20
 
21
- s.add_development_dependency 'bundler', '~> 1'
22
- s.add_development_dependency 'rake', '~> 12'
21
+ s.add_development_dependency 'bundler', '~>1'
22
+ s.add_development_dependency 'rake', '~>13'
23
+ s.add_development_dependency 'appraisal', '~> 2'
24
+ s.add_development_dependency 'combustion', '~> 1'
25
+
26
+ s.add_development_dependency 'minitest', '~> 5.0'
27
+ s.add_development_dependency 'sqlite3', '~> 1'
23
28
  end
@@ -0,0 +1,12 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 5.2.0"
6
+
7
+ group :test do
8
+ gem "observr"
9
+ gem "benchmark-ips"
10
+ end
11
+
12
+ gemspec path: "../"
@@ -0,0 +1,12 @@
1
+ # This file was generated by Appraisal
2
+
3
+ source "https://rubygems.org"
4
+
5
+ gem "rails", "~> 6.0.0"
6
+
7
+ group :test do
8
+ gem "observr"
9
+ gem "benchmark-ips"
10
+ end
11
+
12
+ gemspec path: "../"
@@ -12,7 +12,7 @@ class ActWithFlags::Admin
12
12
  @origin = :flags
13
13
  @map = {}
14
14
  @delete_mask = 0
15
- @max_position = 128 - 1
15
+ @max_position = 512 - 1
16
16
  @boolean_hash = {}
17
17
  [true, 'true', 1, '1'].each { |x| @boolean_hash[x] = true }
18
18
  [false, 'false', 0, '0'].each { |x| @boolean_hash[x] = false }
@@ -39,14 +39,16 @@ class ActWithFlags::Admin
39
39
  end
40
40
 
41
41
  def position(name)
42
- @map[name]
43
- end
42
+ pos = @map[name]
43
+ return pos if pos
44
44
 
45
- def mask(*names)
46
- names.inject(0) { |msk, name| msk | ( 1 << position(name) ) }
45
+ parent = self.model.superclass.act_with_flags
46
+ return parent.position(name) if parent
47
+
48
+ raise "unknown flag '#{model}##{name}'"
47
49
  end
48
50
 
49
- def all?(*names)
51
+ def mask(*names)
50
52
  names.inject(0) { |msk, name| msk | ( 1 << position(name) ) }
51
53
  end
52
54
 
@@ -4,24 +4,45 @@
4
4
  class ActWithFlags::Admin
5
5
 
6
6
  def add_accessors(origin, accessor, mask)
7
+ #p ["act_with_flags#add_accessors:", model, origin, accessor, mask]
8
+ unless model.method_defined?(:act_with_flags)
9
+ model.class_eval %(
10
+ def act_with_flags
11
+ #{model}.act_with_flags
12
+ end
13
+ )
14
+ end
15
+
7
16
  model.class_eval %(
8
17
  def #{accessor}
9
18
  #{accessor}?
10
19
  end
11
20
 
12
21
  def #{accessor}?
13
- !( self.#{origin} & #{mask} ).zero?
22
+ raise "Uninitialized '#{model}.#{origin}'" if #{origin}.nil?
23
+ if #{origin}.is_a?(String)
24
+ flags = self.#{origin}.to_i
25
+ !( flags & #{mask} ).zero?
26
+ else
27
+ !( self.#{origin} & #{mask} ).zero?
28
+ end
14
29
  end
15
30
 
16
31
  def #{accessor}=(value)
17
- self.#{origin} ||= 0
18
- if self.class.act_with_flags.to_boolean(value)
19
- self.#{origin} |= #{mask}
20
- true
32
+ raise "Uninitialized '#{model}.#{origin}'" if #{origin}.nil?
33
+ is_a_string = #{origin}.is_a?(String)
34
+ flags = is_a_string ? self.#{origin}.to_i : self.#{origin}
35
+ flags ||= 0
36
+
37
+ result = self.act_with_flags.to_boolean(value)
38
+ if result
39
+ flags |= #{mask}
21
40
  else
22
- self.#{origin} &= ~#{mask}
23
- false
41
+ flags &= ~#{mask}
24
42
  end
43
+ self.#{origin} = is_a_string ? flags.to_s : flags
44
+
45
+ result
25
46
  end
26
47
  )
27
48
  end
@@ -6,7 +6,7 @@ class ActWithFlags::Admin
6
6
  def to_s
7
7
  res = []
8
8
  res << title('Variables')
9
- res << variables(:origin, :max_position, :boolean_hash)
9
+ res << variables(:origin, :boolean_hash)
10
10
  res << variables(:delete_mask)
11
11
 
12
12
  res << title('Flags sorted alfabetically')
@@ -1,5 +1,4 @@
1
- # xrubocop:disable all
2
- # xfrozen_string_literal: true
1
+ # rubocop: disable all
3
2
 
4
3
  class ActWithFlags::Admin
5
4
 
@@ -46,7 +45,7 @@ class ActWithFlags::Admin
46
45
  names.each { |name|
47
46
  remove_accessor name
48
47
  }
49
- self.reset_model model
48
+ reset_model model
50
49
  end
51
50
 
52
51
  end
@@ -1,8 +1,17 @@
1
1
  # frozen_string_literal: true
2
+ # rubocop: disable all
2
3
 
3
4
  module ActWithFlags
4
- VERSION = '0.0.4' # 2019-03-03
5
- # VERSION = '0.0.3' # 2019-03-01
6
- # VERSION = '0.0.2' # 2019-02-28
7
- # VERSION = '0.0.1' # 2019-02-24
5
+ VERSION = '0.2.3' # 2020-07-14
6
+ # VERSION = '0.2.2' # 2020-04-27
7
+ # VERSION = '0.2.1' # 2020-03-01
8
+ # VERSION = '0.2.0' # 2019-10-04
9
+ # VERSION = '0.1.0' # 2019-04-07
10
+ # VERSION = '0.0.7' # 2019-03-23
11
+ # VERSION = '0.0.6' # 2019-03-08
12
+ # VERSION = '0.0.5' # 2019-03-05
13
+ # VERSION = '0.0.4' # 2019-03-03
14
+ # VERSION = '0.0.3' # 2019-03-01
15
+ # VERSION = '0.0.2' # 2019-02-28
16
+ # VERSION = '0.0.1' # 2019-02-24
8
17
  end
@@ -1,7 +1,9 @@
1
+ # rubocop: disable all
2
+
1
3
  require 'test_helper'
2
4
 
3
5
  describe 'any? all? and none?' do
4
- let(:order) { Order.new }
6
+ let(:order) { Order.create }
5
7
 
6
8
  def setup
7
9
  reset_order
@@ -30,7 +32,7 @@ describe 'any? all? and none?' do
30
32
  end
31
33
 
32
34
  it 'checks none? #2' do
33
- order.a = order.b = true
35
+ order.a = order.b = true
34
36
  refute order.flags_none?(:a, :b)
35
37
  order.a = false
36
38
  refute order.flags_none?(:a, :b)
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  describe 'Clear Flags at Save' do
4
4
  let(:admin) { Order.act_with_flags }
5
- let(:order) { Order.new }
5
+ let(:order) { Order.create }
6
6
 
7
7
  def setup
8
8
  reset_order
@@ -0,0 +1,56 @@
1
+ # rubocop: disable all
2
+
3
+ require 'test_helper'
4
+
5
+ class A < Order
6
+ add_to_flags y: 2
7
+ end
8
+
9
+ describe 'inheritance' do
10
+ let(:a) { A.create }
11
+
12
+ def setup
13
+ reset_order
14
+ Order.add_to_flags x: 1
15
+ end
16
+
17
+ it 'consistency' do
18
+ assert a.respond_to?(:x)
19
+ assert a.respond_to?(:y)
20
+ assert_equal false, a.x
21
+ assert_equal false, a.y
22
+ a.y = true
23
+ assert_equal true, a.y
24
+ end
25
+
26
+ it 'checks any?' do
27
+ a.x = true
28
+ assert a.flags_any?(:x, :y)
29
+ a.x = false
30
+ refute a.flags_any?(:x, :y)
31
+ end
32
+
33
+ it 'checks any? #2' do
34
+ a.y = true
35
+ assert a.flags_any?(:x, :y)
36
+ a.y = false
37
+ refute a.flags_any?(:x, :y)
38
+ end
39
+
40
+ it 'checks all?' do
41
+ a.x = a.y = true
42
+ assert a.flags_all?(:x, :y)
43
+ a.x = false
44
+ refute a.flags_all?(:x, :y)
45
+ end
46
+
47
+ it 'checks none? #2' do
48
+ a.x = a.y = true
49
+ refute a.flags_none?(:x, :y)
50
+ a.x = false
51
+ refute a.flags_none?(:x, :y)
52
+ a.y = false
53
+ assert a.flags_none?(:x, :y)
54
+ end
55
+
56
+ end
@@ -0,0 +1,73 @@
1
+ # rubocop: disable all
2
+
3
+ require 'test_helper'
4
+
5
+ class A < Order
6
+ end
7
+
8
+ class B < Order
9
+ add_to_flags y: 2
10
+ end
11
+
12
+ class C < Order
13
+ before_create { |row| row.flags2 = 0 }
14
+
15
+ attr_accessor :flags2
16
+ add_to_flags z: 3, origin: :flags2
17
+ end
18
+
19
+ describe 'inheritance' do
20
+ let(:admina) { A.act_with_flags }
21
+ let(:adminb) { B.act_with_flags }
22
+
23
+ let(:a) { A.create }
24
+ let(:b) { B.create }
25
+ let(:c) { C.create }
26
+
27
+ def setup
28
+ reset_order
29
+ Order.add_to_flags x: 1
30
+ end
31
+
32
+ it 'inheritance #1' do
33
+ assert a.respond_to?(:flags)
34
+ assert a.respond_to?(:x)
35
+ assert_equal 0, a.flags
36
+ assert_equal false, a.x
37
+ end
38
+
39
+ it 'inheritance #2' do
40
+ a.x = false
41
+ assert_equal false, a.x
42
+ a.x = true
43
+ assert_equal true, a.x
44
+ assert_equal 0x02, a.flags
45
+ end
46
+
47
+ it 'inheritance #3' do
48
+ assert b.respond_to?(:flags)
49
+ assert b.respond_to?(:x)
50
+ assert b.respond_to?(:y)
51
+ assert_equal 0, b.flags
52
+ assert_equal false, b.x
53
+ assert_equal false, b.y
54
+ b.x = true
55
+ assert_equal true, b.x
56
+ b.y = true
57
+ assert_equal true, b.y
58
+ assert_equal 0x06, b.flags
59
+ end
60
+
61
+ it 'inheritance #4' do
62
+ assert c.respond_to?(:x)
63
+ assert c.respond_to?(:z)
64
+ assert_equal false, c.z
65
+ c.x = true
66
+ assert_equal true, c.x
67
+ c.z = true
68
+ assert_equal true, c.z
69
+ assert_equal 0x02, c.flags
70
+ assert_equal 0x08, c.flags2
71
+ end
72
+
73
+ end
@@ -0,0 +1 @@
1
+ //= link_tree ../images
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery with: :exception
3
+ end
@@ -0,0 +1,16 @@
1
+ class OrdersController < ApplicationController
2
+
3
+ def index
4
+ @orders = Order.all
5
+ end
6
+
7
+ def new
8
+ @order = Order.new
9
+ end
10
+
11
+ def show
12
+ Order.create :name => 'hugo'
13
+ @order = Order.find(params[:id])
14
+ end
15
+
16
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,9 @@
1
+ class Order < ApplicationRecord
2
+
3
+ before_save do |row|
4
+ row.flags ||= 0
5
+ row.bigflags ||= ''
6
+ row.errors.add :base, 'panic' if row.name == 'error'
7
+ end
8
+
9
+ end
@@ -0,0 +1,12 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%#= stylesheet_link_tag "application" %>
6
+ <%#= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+ <%= yield %>
11
+ </body>
12
+ </html>