simple_flaggable_column 0.0.3 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5519375f06815a7e44b9158d7c775fc9008c2d6a
4
- data.tar.gz: e72384692c2244b58e966e9df850d398cb526c0f
3
+ metadata.gz: 3c89c0eff135320abf0a53dc93707c6c85fa0656
4
+ data.tar.gz: 64b94f3228a1b211cedcb76a5f98f03b13059b61
5
5
  SHA512:
6
- metadata.gz: 10cd8c9d6d416277da37f339be37e875427b8ee76ecb86c53eda34cfb152e7ed96a0da5f4228d6a492db2cde6b998c6ca1afd8f018d5e8808fe656641041e0ce
7
- data.tar.gz: d726167cc47139e3e9a4e98b30527b02f3814043cd6e909020893c40b4dd8901979d265e9007157dc93499b7e2d72e640fe60f0bf7e2555e1b06e940603d4433
6
+ metadata.gz: 5681dd1893fcb24894fb4a171e888eb0491563d9ca589ffa58645dbe1e869c76e7d63bbfb65d6cc1193c1ca5751d8ac1234d61b3df36dcf64fe16f965dacae78
7
+ data.tar.gz: be74c4d1388699fc686a2d0d47a916be8d6cf19f50147a6505540305d3f248283cfb8bbb610ecad0586e3209a26ce51b61f5cbdd5aa79f7912c7f5389884cba4
data/README.md CHANGED
@@ -54,6 +54,38 @@ game.read_attribute(:platforms).to_s(2)
54
54
 
55
55
  Push/Pop and other arrays operations won't work, just simple writing and reading.
56
56
 
57
+ ### :throw_on_missing
58
+
59
+ If you try to set a non-existing flag it will throw an ArgumentError
60
+
61
+ ```ruby
62
+ game = Game.new
63
+ game.platforms = [:potato]
64
+ # => <ArgumentError: Flag potato doesn't exists>
65
+ ```
66
+
67
+ To disable this and silently ignore invalid flags define the flags with :throw_on_missing set to false:
68
+
69
+ ```ruby
70
+ class Game < ActiveRecord::Base
71
+ include SimpleFlaggableColumn
72
+
73
+ flag_column :platforms, {
74
+ win: 0b001,
75
+ mac: 0b010,
76
+ linux: 0b100
77
+ }, throw_on_missing: false
78
+ end
79
+ ```
80
+
81
+ Now invalid flags will be ignored
82
+
83
+ ```ruby
84
+ game = Game.new
85
+ game.platforms = [:win, :potato]
86
+ # => [:win]
87
+ ```
88
+
57
89
  ## Contributing
58
90
 
59
91
  1. Fork it ( https://github.com/Zequez/simple_flaggable_column/fork )
@@ -4,8 +4,13 @@ require 'simple_flaggable_column/version'
4
4
  module SimpleFlaggableColumn
5
5
  extend ActiveSupport::Concern
6
6
 
7
- def self.symbols_to_flags(symbols, symbols_flags)
8
- symbols.map{|s| symbols_flags[s]}.compact.reduce(:|) || 0
7
+ def self.symbols_to_flags(symbols, symbols_flags, throw_on_missing = true)
8
+ symbols.map do |s|
9
+ if throw_on_missing && !symbols_flags[s]
10
+ throw ArgumentError.new("Flag #{s} doesn't exists")
11
+ end
12
+ symbols_flags[s]
13
+ end.compact.reduce(:|) || 0
9
14
  end
10
15
 
11
16
  def self.flags_to_symbols(flags, symbols_flags)
@@ -13,14 +18,22 @@ module SimpleFlaggableColumn
13
18
  end
14
19
 
15
20
  module ClassMethods
16
- def flag_column(name, symbols_flags)
21
+ def flag_column(name, symbols_flags, options = {})
22
+ options = {
23
+ throw_on_missing: true
24
+ }.merge(options)
25
+
17
26
  flags_symbols = symbols_flags.invert
18
27
 
19
28
  define_singleton_method :"#{name}_flags" do |*symbols|
20
29
  if symbols.empty?
21
30
  symbols_flags
22
31
  else
23
- SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
32
+ SimpleFlaggableColumn.symbols_to_flags(
33
+ symbols,
34
+ symbols_flags,
35
+ options[:throw_on_missing]
36
+ )
24
37
  end
25
38
  end
26
39
 
@@ -32,7 +45,11 @@ module SimpleFlaggableColumn
32
45
  if symbols.nil?
33
46
  write_attribute name, 0
34
47
  elsif symbols.kind_of? Array
35
- write_attribute name, SimpleFlaggableColumn.symbols_to_flags(symbols, symbols_flags)
48
+ write_attribute name, SimpleFlaggableColumn.symbols_to_flags(
49
+ symbols,
50
+ symbols_flags,
51
+ options[:throw_on_missing]
52
+ )
36
53
  else # numeric, or anything else
37
54
  write_attribute name, symbols
38
55
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleFlaggableColumn
2
- VERSION = "0.0.3"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -4,6 +4,7 @@ describe SimpleFlaggableColumn do
4
4
  def change
5
5
  create_table :games do |t|
6
6
  t.integer :platforms, default: 0, null: false
7
+ t.integer :softie, default: 0, null: false
7
8
  end
8
9
  end
9
10
  end
@@ -14,10 +15,15 @@ describe SimpleFlaggableColumn do
14
15
  include SimpleFlaggableColumn
15
16
 
16
17
  flag_column :platforms, {
17
- win: 0b001,
18
- mac: 0b010,
18
+ win: 0b1,
19
+ mac: 0b10,
19
20
  linux: 0b100
20
21
  }
22
+
23
+ flag_column :softie, {
24
+ vanilla: 0b1,
25
+ chocolate: 0b10
26
+ }, throw_on_missing: false
21
27
  end
22
28
  end
23
29
 
@@ -32,13 +38,6 @@ describe SimpleFlaggableColumn do
32
38
  expect(game.platforms).to match_array [:mac, :linux]
33
39
  end
34
40
 
35
- it "should ignore items that aren't on the list" do
36
- game = Game.new
37
- game.platforms = [:mac, :linux, :potato]
38
- expect(game.read_attribute(:platforms)).to eq 0b110
39
- expect(game.platforms).to match_array [:mac, :linux]
40
- end
41
-
42
41
  it 'should set the value to 0 when nil' do
43
42
  game = Game.new
44
43
  game.platforms = [:mac, :linux]
@@ -53,6 +52,25 @@ describe SimpleFlaggableColumn do
53
52
  expect(game.platforms).to eq [:win, :linux]
54
53
  end
55
54
 
55
+ context 'with throw_on_missing set to false' do
56
+ it 'should throw an exception when setting a non-existing flags' do
57
+ game = Game.new
58
+ expect{ game.platforms = [:potato] }.to raise_error ArgumentError
59
+ end
60
+ end
61
+
62
+ context 'with throw_on_missing set to true' do
63
+ it 'should not throw an exception when setting a non-existant flags' do
64
+ game = Game.new
65
+ expect{ game.softie = [:chocolate, :potato] }.to_not raise_error
66
+ expect(game.read_attribute(:softie)).to eq 0b10
67
+ expect(game.softie).to match_array [:chocolate]
68
+ end
69
+ end
70
+
71
+
72
+
73
+
56
74
  describe 'flags lists' do
57
75
  it 'should define the list of flags' do
58
76
  expect(Game.platforms_flags).to eq({
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_flaggable_column
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Zequez
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-23 00:00:00.000000000 Z
11
+ date: 2017-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport