sequel-bit_fields 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/sequel/plugins/bit_fields.rb +35 -1
- metadata +3 -3
@@ -3,19 +3,47 @@ require 'sequel'
|
|
3
3
|
module Sequel::Plugins
|
4
4
|
module BitFields
|
5
5
|
def self.configure(model, bit_field_column, bit_fields, options = {})
|
6
|
+
model.class.instance_eval do
|
7
|
+
@@bit_fields ||= {}
|
8
|
+
@@bit_fields[bit_field_column] = bit_fields
|
9
|
+
|
10
|
+
unless self.respond_to?(:bit_fields)
|
11
|
+
define_method("bit_fields") do |*args|
|
12
|
+
if column_name = [*args].first
|
13
|
+
@@bit_fields[column_name]
|
14
|
+
else
|
15
|
+
@@bit_fields
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
6
21
|
bit_fields.each_with_index do |bit_field_name, i|
|
7
22
|
index = 2**i
|
8
23
|
|
9
24
|
model.class.instance_eval do
|
25
|
+
# inject the sql generator methods
|
26
|
+
#
|
27
|
+
# example:
|
28
|
+
# MyModel.where(MyModel.finished_sql(true)).all
|
29
|
+
# and would return
|
30
|
+
# "status_bits & 1 = 1"
|
31
|
+
#
|
10
32
|
define_method("#{bit_field_name}_sql") do |*args|
|
11
33
|
value = [*args].first
|
12
34
|
value = true if value.nil?
|
13
35
|
|
14
|
-
"
|
36
|
+
"`#{self.table_name.to_s}`.`#{bit_field_column}` & #{index} #{'!' unless value}= #{index}"
|
15
37
|
end
|
16
38
|
end
|
17
39
|
|
18
40
|
model.instance_eval do
|
41
|
+
# inject the setter methods
|
42
|
+
#
|
43
|
+
# example:
|
44
|
+
# model = MyModel.create()
|
45
|
+
# model.finished = false
|
46
|
+
#
|
19
47
|
define_method("#{bit_field_name}=") do |value|
|
20
48
|
self[bit_field_column] = if value
|
21
49
|
self[bit_field_column] | index
|
@@ -24,6 +52,12 @@ module Sequel::Plugins
|
|
24
52
|
end
|
25
53
|
end
|
26
54
|
|
55
|
+
# inject the getter methods
|
56
|
+
#
|
57
|
+
# example:
|
58
|
+
# model = MyModel.create()
|
59
|
+
# model.finished? # == false
|
60
|
+
#
|
27
61
|
define_method("#{bit_field_name}?") do
|
28
62
|
self[bit_field_column] & index == index
|
29
63
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-bit_fields
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-12 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sequel
|
@@ -49,7 +49,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
49
49
|
version: '0'
|
50
50
|
segments:
|
51
51
|
- 0
|
52
|
-
hash:
|
52
|
+
hash: 3721716346876275602
|
53
53
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
54
|
none: false
|
55
55
|
requirements:
|