sequel-bit_fields 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/sequel/plugins/bit_fields.rb +31 -0
  2. metadata +65 -0
@@ -0,0 +1,31 @@
1
+ require 'sequel'
2
+
3
+ module Sequel::Plugins
4
+ module BitFields
5
+ def self.configure(model, bit_field_column, bit_fields, options = {})
6
+ bit_fields.each_with_index do |bit_field_name, i|
7
+ index = 2**i
8
+
9
+ model.class.instance_eval do
10
+ define_method("#{bit_field_name}_sql") do |value=true|
11
+ "#{bit_field_column} & #{index} #{'!' unless value}= #{index}"
12
+ end
13
+ end
14
+
15
+ model.instance_eval do
16
+ define_method("#{bit_field_name}=") do |value|
17
+ self[bit_field_column] = if value
18
+ self[bit_field_column] | index
19
+ else
20
+ self[bit_field_column] ^ index
21
+ end
22
+ end
23
+
24
+ define_method("#{bit_field_name}?") do
25
+ self[bit_field_column] & index == index
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-bit_fields
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Sascha Depold
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-19 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: sequel
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ description: Use this plugin to add flags/bits to your Sequel based models.
31
+ email: sascha@depold.com
32
+ executables: []
33
+ extensions: []
34
+ extra_rdoc_files: []
35
+ files:
36
+ - lib/sequel/plugins/bit_fields.rb
37
+ homepage: https://github.com/sdepold/sequel-bit_fields
38
+ licenses:
39
+ - MIT
40
+ post_install_message:
41
+ rdoc_options: []
42
+ require_paths:
43
+ - lib
44
+ required_ruby_version: !ruby/object:Gem::Requirement
45
+ none: false
46
+ requirements:
47
+ - - ! '>='
48
+ - !ruby/object:Gem::Version
49
+ version: '0'
50
+ segments:
51
+ - 0
52
+ hash: 1135270008148119974
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ! '>='
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubyforge_project:
61
+ rubygems_version: 1.8.24
62
+ signing_key:
63
+ specification_version: 3
64
+ summary: A Sequel compatible version of common bit field technique.
65
+ test_files: []