incline 0.2.21 → 0.2.22

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: 53d73c77b134ead5f325b1df5a39dae0cf712617
4
- data.tar.gz: 5de1a6150ce541052263e88a719148701a709c12
3
+ metadata.gz: 9a1e49384b7901f87a814ee60d71b0851ddc7f7b
4
+ data.tar.gz: 111391691fad01b40f655ee7cef3a998c1863068
5
5
  SHA512:
6
- metadata.gz: 8cbf94c22ec5fcca8a59e0d01891d8399a04ecd21a3a90de1145f9e21a9eb8f1e5ce44bf59f6e795a6a88e44e92b5ba911dd78e7893e2377e9d6be900d0530d5
7
- data.tar.gz: 3fe781aa459fdc03d1830b0ae4cee36e2ef4c6bd9b8acef262fd83363517f47aec8433f694b35cf4823f0ff9b9f6edf433a2b55e52d9bb05fe97541326946133
6
+ metadata.gz: 6fa77414d22418a5866ee05c1a3a6b4361ff2a4a48dfa889a63599999afb355503e492911e338f895490492ad132f02cc948c53eed0ac8917ef54a10fe37521b
7
+ data.tar.gz: c64ce83ca35c5132ba98e5e46f615327bddf7f00dbf4f523b510b7a0fe53118cbd97fafb66825124d08881a8fefbaeb5ced2dfb93a109891132a5287ed73f736
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- incline (0.2.21)
4
+ incline (0.2.22)
5
5
  ansi (~> 1.5.0)
6
6
  bcrypt
7
7
  bootstrap-sass
@@ -0,0 +1,93 @@
1
+ module Incline
2
+ class BitEnum < ConstantEnum
3
+
4
+ def self.name_for(value, set_nil = false)
5
+ ret = values.select{|(v,k)| v == value}.map{|(v,k)| k}
6
+ return ret unless ret.blank?
7
+
8
+ ret = []
9
+
10
+ values.each do |(v,k)|
11
+ if v.power_of_2? && value >= v
12
+ if set_nil
13
+ ret << k || v.to_s
14
+ else
15
+ ret << k
16
+ end
17
+ value -= v
18
+ end
19
+ end
20
+
21
+ if value > 0
22
+ ret << set_nil ? value.to_s : nil
23
+ end
24
+
25
+ ret.sort
26
+ end
27
+
28
+ def self.named?(value)
29
+ nm = name_for(value, false)
30
+ return false if nm.blank?
31
+ nm.select{|v| v.nil?}.blank?
32
+ end
33
+
34
+ def to_s
35
+ self.class.name_for(value, true).join(' | ')
36
+ end
37
+
38
+ ##
39
+ # Enable bit checking and bit setting/clearing.
40
+ #
41
+ # For instance, with a constant FLAG_1, this enables :flag_1? and :flag_1=(true|false).
42
+ def method_missing(m,*a,&b)
43
+ name = m.to_s
44
+ if name[-1] == '?'
45
+ name = name[0...-1].upcase.to_sym
46
+ if constants(false).include?(name)
47
+ v = const_get(name)
48
+ return (value & v) == v
49
+ end
50
+ elsif name[-1] == '='
51
+ name = name[0...-1].upcase.to_sym
52
+ if constants(false).include?(name)
53
+ v = const_get(name)
54
+ if a[0]
55
+ value |= v
56
+ else
57
+ value &= ~v
58
+ end
59
+ @name = self.class.name_for(@value)
60
+ return a[0]
61
+ end
62
+ end
63
+ super m, *a, &b
64
+ end
65
+
66
+ private
67
+
68
+ def self.values
69
+ @values ||=
70
+ begin
71
+ tmp = names.map{|k,v| [ v, k ]}.sort{|a,b| b[0] <=> a[0]}
72
+ max_bit = tmp.first[0]
73
+ unless max_bit.power_of_2?
74
+ pow = 1
75
+ while pow < max_bit
76
+ pow <<= 1
77
+ end
78
+ max_bit = pow >> 1
79
+ end
80
+
81
+ while max_bit > 0
82
+ unless tmp.index{|(v,k)| v == max_bit}
83
+ tmp << [ max_bit, nil ]
84
+ end
85
+ max_bit >>= 1
86
+ end
87
+
88
+ tmp.sort{|a,b| b[0] <=> a[0]}
89
+ end
90
+ end
91
+
92
+ end
93
+ end
@@ -0,0 +1,40 @@
1
+ module Incline
2
+ class ConstantEnum
3
+
4
+ attr_reader :value, :name
5
+
6
+ def initialize(value)
7
+ raise ArgumentError, 'Invalid value' unless self.class.named?(value)
8
+ @value = value
9
+ @name = self.class.name_for(value)
10
+ end
11
+
12
+ def to_s
13
+ name
14
+ end
15
+
16
+
17
+
18
+ def self.named?(value)
19
+ !name_for(value).blank?
20
+ end
21
+
22
+ def self.name_for(value)
23
+ names.key(value) || ''
24
+ end
25
+
26
+ private
27
+
28
+ def self.names
29
+ @names ||=
30
+ begin
31
+ ret = {}
32
+ constants(false).each do |nm|
33
+ ret[nm.to_s] = const_get(nm)
34
+ end
35
+ ret
36
+ end
37
+ end
38
+
39
+ end
40
+ end
@@ -8,6 +8,8 @@ require 'incline/data_tables_request'
8
8
  require 'incline/recaptcha'
9
9
  require 'incline/auth_engine_base'
10
10
  require 'incline/user_manager'
11
+ require 'incline/constant_enum'
12
+ require 'incline/bit_enum'
11
13
 
12
14
  # Preloaded gems
13
15
  require 'rails'
@@ -56,7 +56,13 @@ module Incline::Extensions
56
56
  def to_bool
57
57
  self != 0
58
58
  end
59
-
59
+
60
+ ##
61
+ # Is this value a power of two?
62
+ def power_of_2?
63
+ (self.to_i == self) && (self != 0) && ((self & (self - 1)) == 0)
64
+ end
65
+
60
66
  end
61
67
  end
62
68
 
@@ -1,3 +1,3 @@
1
1
  module Incline
2
- VERSION = "0.2.21"
2
+ VERSION = "0.2.22"
3
3
  end
@@ -0,0 +1,54 @@
1
+ require 'test_helper'
2
+
3
+ class BitEnumTest < ActiveSupport::TestCase
4
+
5
+ class MyEnum < ::Incline::BitEnum
6
+
7
+ # raw bits.
8
+ ALPHA = 1
9
+ BRAVO = 2
10
+ CHARLIE = 4
11
+ DELTA = 8
12
+ ECHO = 16
13
+
14
+ # combined bits.
15
+ FOXTROT = 5
16
+ GOLF = 17
17
+
18
+ end
19
+
20
+ test 'should allow initialization' do
21
+ item = MyEnum.new(6)
22
+ assert item
23
+ assert_equal 6, item.value
24
+ assert_equal %w(BRAVO CHARLIE), item.name
25
+ assert_equal 'BRAVO | CHARLIE', item.to_s
26
+ end
27
+
28
+ test 'should not allow invalid values' do
29
+ assert_raises(ArgumentError){ MyEnum.new(0) }
30
+ assert_raises(ArgumentError){ MyEnum.new(32) }
31
+ end
32
+
33
+ test 'should allow valid values' do
34
+ assert MyEnum.new(1)
35
+ assert MyEnum.new(7)
36
+ assert MyEnum.new(15)
37
+ assert MyEnum.new(31)
38
+ end
39
+
40
+ test 'should recognize names' do
41
+ assert MyEnum.named?(1)
42
+ assert MyEnum.named?(2)
43
+ assert MyEnum.named?(3)
44
+ assert MyEnum.named?(4)
45
+ assert MyEnum.named?(5)
46
+ assert_equal %w(ALPHA), MyEnum.name_for(1)
47
+ assert_equal %w(BRAVO), MyEnum.name_for(2)
48
+ assert_equal %w(ALPHA BRAVO), MyEnum.name_for(3)
49
+ assert_equal %w(CHARLIE), MyEnum.name_for(4)
50
+ assert_equal %w(FOXTROT), MyEnum.name_for(5)
51
+ end
52
+
53
+
54
+ end
@@ -0,0 +1,43 @@
1
+ require 'test_helper'
2
+
3
+ class ConstantEnumTest < ActiveSupport::TestCase
4
+
5
+ class MyEnum < ::Incline::ConstantEnum
6
+
7
+ ALPHA = 1
8
+ BRAVO = 2
9
+ CHARLIE = 3
10
+ DELTA = 4
11
+
12
+ end
13
+
14
+ test 'should allow initialization' do
15
+ item = MyEnum.new(2)
16
+ assert item
17
+ assert_equal 2, item.value
18
+ assert_equal 'BRAVO', item.name
19
+ assert_equal item.name, item.to_s
20
+ end
21
+
22
+ test 'should not allow invalid values' do
23
+ assert_raises ArgumentError do
24
+ MyEnum.new(0)
25
+ end
26
+ assert_raises ArgumentError do
27
+ MyEnum.new(5)
28
+ end
29
+ end
30
+
31
+ test 'should be able to identify named values' do
32
+ assert MyEnum.named?(3)
33
+ assert_not MyEnum.named?(42)
34
+ end
35
+
36
+ test 'should always return a non-nil name' do
37
+ assert_equal 'DELTA', MyEnum.name_for(4)
38
+ assert_equal '', MyEnum.name_for(100)
39
+ end
40
+
41
+
42
+
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: incline
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.21
4
+ version: 0.2.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Beau Barker
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-10-17 00:00:00.000000000 Z
11
+ date: 2017-10-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -395,6 +395,7 @@ files:
395
395
  - lib/generators/incline/templates/incline_version.rb
396
396
  - lib/incline.rb
397
397
  - lib/incline/auth_engine_base.rb
398
+ - lib/incline/bit_enum.rb
398
399
  - lib/incline/cli.rb
399
400
  - lib/incline/cli/errors.rb
400
401
  - lib/incline/cli/helpers/yaml.rb
@@ -416,6 +417,7 @@ files:
416
417
  - lib/incline/cli/prepare/update_system.rb
417
418
  - lib/incline/cli/usage.rb
418
419
  - lib/incline/cli/version.rb
420
+ - lib/incline/constant_enum.rb
419
421
  - lib/incline/data_tables_request.rb
420
422
  - lib/incline/date_time_formats.rb
421
423
  - lib/incline/engine.rb
@@ -562,6 +564,8 @@ files:
562
564
  - test/integration/incline/users_login_test.rb
563
565
  - test/integration/incline/users_signup_test.rb
564
566
  - test/integration/navigation_test.rb
567
+ - test/lib/bit_enum_test.rb
568
+ - test/lib/constant_enum_test.rb
565
569
  - test/lib/data_tables_request_test.rb
566
570
  - test/lib/date_time_formats_test.rb
567
571
  - test/lib/global_status_test.rb
@@ -606,7 +610,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
606
610
  version: '0'
607
611
  requirements: []
608
612
  rubyforge_project:
609
- rubygems_version: 2.6.11
613
+ rubygems_version: 2.5.2
610
614
  signing_key:
611
615
  specification_version: 4
612
616
  summary: A gem designed to get off to an even quicker start with Rails.