masked_attribute 0.1.0 → 0.1.1

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
  SHA256:
3
- metadata.gz: 8b97ca1bc7469412e6d7a070de7849835b70ceb26043ec583a8243c2f4aef82f
4
- data.tar.gz: 7baaef798a14dffd01b8bcb779db3fe5fcbaf9f8304f979c1b3ceb800f431efa
3
+ metadata.gz: 78d882c9d9f2b6ed899d94d244f03c4b9c48fb7ab9652c3471a38e6ae8691078
4
+ data.tar.gz: 299bbe53d8fddb93c0f311e1dd5b61af42c6c7f3bb18013a26f9a589fef72cb2
5
5
  SHA512:
6
- metadata.gz: e1f91990606ed4e137c5a5329644cefd4101a514e2aeb5263a1f1aa1ab3007e78c101bbf2f800c5f7584e64c6dc62f26406af16f66f794b953a43cbb3ed5f88a
7
- data.tar.gz: 2e1da713675c6ab35745fbfe7334cfbe1da1993b47b35e39fef7d889857e0835855c251b35ee090babfb212b2db39879839a4414d3ba74b51495616705de9a48
6
+ metadata.gz: 03e4819f7d46b1f578b2777b539e3afd3a5b2ae463a4473a8a25511e0185f3ed1b7fd920c864e75500b2b098252450fccc536e65419c7e87932e20a38469062c
7
+ data.tar.gz: ef9e1a129a34d50dd56556d5f8b59966f899e193d04904f3d072dd5bb511e48844d537b5d9285b2c5631ef56ee44769a6d4f951fe56bfcec770e3d67fda630c2
data/README.md CHANGED
@@ -80,6 +80,12 @@ def add_sysadmin!
80
80
  # Add remove_MASK! methods. Updates the backing field with the MASK value in the name.
81
81
  def remove_admin!
82
82
  def remove_sysadmin!
83
+
84
+ # Add a MASK from the mask_attribute_name
85
+ def add_role(mask)
86
+
87
+ # Remove a MASK from the mask_attribute_name
88
+ def remove_role(mask)
83
89
  ```
84
90
 
85
91
  ## Installation
data/Rakefile CHANGED
@@ -1,3 +1,7 @@
1
1
  require "bundler/setup"
2
2
 
3
3
  require "bundler/gem_tasks"
4
+
5
+ task :test do
6
+ sh("ruby -I test test/*_test.rb test/**/*_test.rb")
7
+ end
@@ -1,3 +1,3 @@
1
1
  module MaskedAttribute
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -39,6 +39,9 @@ module MaskedAttribute
39
39
  # def roles=(array of masks)
40
40
  # def roles -> array of masks
41
41
  #
42
+ # def add_role(ROLE)
43
+ # def remove_role(ROLE)
44
+ #
42
45
  # Class::ROLES = masks
43
46
  # Class::INDEXED_ROLES = {mask_value => mask, ...}
44
47
  def masked_attribute attribute_name, masks
@@ -50,16 +53,16 @@ module MaskedAttribute
50
53
  indexed_masks = masks.map { |v| [2**masks.index(v), v] }.to_h.freeze
51
54
 
52
55
  const_set masks_const_name, masks
53
- const_set "INDEXED_#{masks_const_name}", indexed_masks
56
+ const_set :"INDEXED_#{masks_const_name}", indexed_masks
54
57
 
55
58
  # Add a scope with_ATTRIBUTE_NAME, which returns records that match ALL given masks
56
- self.class.define_method("with_#{attribute_name}") do |*values|
59
+ self.class.define_method(:"with_#{attribute_name}") do |*values|
57
60
  ok_mask = mask_from(masks, values)
58
61
  where("role_mask & ? = ?", ok_mask, ok_mask)
59
62
  end
60
63
 
61
64
  # Add a scope with_any_ATTRIBUTE_NAME, which returns records that match ANY given masks
62
- self.class.define_method("with_any_#{attribute_name}") do |*values|
65
+ self.class.define_method(:"with_any_#{attribute_name}") do |*values|
63
66
  ok_mask = mask_from(masks, values)
64
67
  where("role_mask & ? != 0", ok_mask)
65
68
  end
@@ -68,31 +71,31 @@ module MaskedAttribute
68
71
  value_int = 2**masks.index(value)
69
72
 
70
73
  # Add MASK? method. Returns true if the mask is set
71
- define_method("#{value}?") do
74
+ define_method(:"#{value}?") do
72
75
  has = __send__(mask_attribute_name)
73
76
  has & value_int > 0
74
77
  end
75
78
 
76
79
  # Add add_MASK! method. Updates the MASKS_mask with the MASK in the name
77
- define_method("add_#{value}!") do
80
+ define_method(:"add_#{value}!") do
78
81
  has = __send__(mask_attribute_name)
79
82
  update!("#{mask_attribute_name}": (has | value_int))
80
83
  end
81
84
 
82
85
  # Add remove_MASK! method. Updates the MASKS_mask without the MASK in the name
83
- define_method("remove_#{value}!") do
86
+ define_method(:"remove_#{value}!") do
84
87
  has = __send__(mask_attribute_name)
85
88
  update!("#{mask_attribute_name}": (has & ~value_int))
86
89
  end
87
90
 
88
91
  # Add scope MASK, to return records with the MASK in the name
89
- scope value.to_s.pluralize, -> { with_roles(value) }
92
+ scope value.to_s.pluralize, -> { __send__(:"with_#{attribute_name}", value) }
90
93
  end
91
94
 
92
95
  # Add attr_writer for the attribute_name
93
- define_method("#{attribute_name}=") do |value|
96
+ define_method(:"#{attribute_name}=") do |value|
94
97
  value = Array.wrap(value).map(&:to_sym)
95
- __send__("#{mask_attribute_name}=", self.class.mask_from(masks, value))
98
+ __send__(:"#{mask_attribute_name}=", self.class.mask_from(masks, value))
96
99
  end
97
100
 
98
101
  # Add attr_reader for the attribute_name
@@ -101,6 +104,22 @@ module MaskedAttribute
101
104
  ((__send__(mask_attribute_name).to_i || 0) & 2**masks.index(r)).zero?
102
105
  end
103
106
  end
107
+
108
+ # Add a MASK from the mask_attribute_name
109
+ # params: value = the mask to add
110
+ define_method(:"add_#{attribute_name.singularize}") do |value|
111
+ value_int = 2**masks.index(value)
112
+ has = __send__(mask_attribute_name)
113
+ __send__(:"#{mask_attribute_name}=", has | value_int)
114
+ end
115
+
116
+ # Remove a MASK from the mask_attribute_name
117
+ # params: value = the mask to remove
118
+ define_method(:"remove_#{attribute_name.singularize}") do |value|
119
+ value_int = 2**masks.index(value)
120
+ has = __send__(mask_attribute_name)
121
+ __send__(:"#{mask_attribute_name}=", has & ~value_int)
122
+ end
104
123
  end
105
124
  end
106
125
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masked_attribute
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - candland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-05-09 00:00:00.000000000 Z
11
+ date: 2024-01-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 7.0.4.2
19
+ version: '7.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
- version: 7.0.4.2
26
+ version: '7.0'
27
27
  description: Adds methods for a bit masked field
28
28
  email:
29
29
  - candland@gmail.com