reallycare_utils 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.textile CHANGED
@@ -1 +1,19 @@
1
- Hello
1
+ h1. ReallyCare_utils
2
+
3
+ Functions that are used (or likely to be used) by more than one ReallyCare CIC product
4
+
5
+ h2. Bitmask
6
+
7
+ A module to make handling bit masks easy.
8
+
9
+ Usage:
10
+
11
+ class X
12
+ bitmaps :status => [:current, :pending], :flags => [:flag1, :flag2, :flag3]
13
+ end
14
+
15
+ x = X.new
16
+ x.set_status_current
17
+ x.is_status_current?
18
+ x.show_status
19
+ x.unset_status_current
@@ -1,48 +1,42 @@
1
1
  module ReallycareUtils
2
2
  module Bitmask
3
3
  module ClassMethods
4
- def has_bitmask(field, statuses)
5
- @@has_bitmask_field = field
6
- @@has_bitmask_values = statuses
7
-
8
- define_bitmask_methods
9
- end
10
-
11
- def bitmask_value(status_const)
12
- return 2**@@has_bitmask_values.index(status_const.to_sym)
13
- end
14
-
15
- def define_bitmask_methods
16
- define_method "has_#{@@has_bitmask_field}?" do |bitmask_const|
17
- if self[@@has_bitmask_field]
18
- if bitmask_const.instance_of? Fixnum
19
- this_bit = bitmask_const
20
- else
21
- this_bit = self.class.bitmask_value(bitmask_const)
4
+ def has_bitmasks(types)
5
+ types.each do |attribute, values|
6
+ values.each_with_index do |value_name, i|
7
+
8
+ define_method("add_#{attribute}_#{value_name}".to_sym) do |save_it=false|
9
+ self["#{attribute}"] = (self["#{attribute}"] || 0) | (2 ** i)
10
+ self.save! if save_it
11
+ end
12
+
13
+ define_method("remove_#{attribute}_#{value_name}".to_sym) do |save_it=false|
14
+ self["#{attribute}"] = (self["#{attribute}"] || 0) & ~(2 ** i)
15
+ self.save! if save_it
16
+ end
17
+
18
+ define_method("has_#{attribute}_#{value_name}?".to_sym) do
19
+ self["#{attribute}"][i] != 0
22
20
  end
23
- has_it = (self[@@has_bitmask_field] & this_bit > 0)
24
- else
25
- has_it = false
26
21
  end
27
- has_it
28
- end
29
-
30
- define_method "add_#{@@has_bitmask_field}" do |bitmask_const, save_it=false|
31
- self[@@has_bitmask_field] = (self[@@has_bitmask_field] || 0) | self.class.bitmask_value(bitmask_const)
32
- self.save! if save_it
33
- end
34
-
35
- define_method "remove_#{@@has_bitmask_field}" do |bitmask_const, save_it=false|
36
- self[@@has_bitmask_field] = (self[@@has_bitmask_field] || 0) & ~(self.class.bitmask_value(bitmask_const))
37
- self.save! if save_it
22
+
23
+ define_method("show_#{attribute}_flags".to_sym) do
24
+ val = self["#{attribute}"]
25
+ potential_values.map.with_index { |value_name, i| value_name if val[i] == 1 }.compact
26
+ end
27
+
28
+ define_method("#{attribute}_includes_one_of?".to_sym) do |mask|
29
+ (self["#{attribute}"] | mask) != 0
30
+ end
31
+
38
32
  end
39
-
40
33
  end
41
34
  end
42
35
 
43
36
  def self.included(receiver)
44
- receiver.extend ClassMethods
45
- # receiver.send :include, InstanceMethods
37
+ receiver.extend ClassMethods
46
38
  end
47
39
  end
48
40
  end
41
+
42
+
@@ -1,3 +1,3 @@
1
1
  module ReallycareUtils
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,31 +1,25 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: reallycare_utils
3
- version: !ruby/object:Gem::Version
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.5
4
5
  prerelease:
5
- version: 0.0.4
6
6
  platform: ruby
7
- authors:
7
+ authors:
8
8
  - Mark Chapman
9
9
  - Andy Jeffries
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
-
14
- date: 2011-06-05 00:00:00 -04:00
15
- default_executable:
13
+ date: 2011-12-16 00:00:00.000000000Z
16
14
  dependencies: []
17
-
18
15
  description: A collection of shared utilities for ReallyCare sites
19
- email:
16
+ email:
20
17
  - mark.chapman@gmail.com
21
18
  - andy@andyjeffries.co.uk
22
19
  executables: []
23
-
24
20
  extensions: []
25
-
26
21
  extra_rdoc_files: []
27
-
28
- files:
22
+ files:
29
23
  - .gitignore
30
24
  - Gemfile
31
25
  - README.textile
@@ -37,33 +31,28 @@ files:
37
31
  - reallycare_utils.gemspec
38
32
  - spec/bitmask_spec.rb
39
33
  - spec/spec_helper.rb
40
- has_rdoc: true
41
- homepage: ""
34
+ homepage: ''
42
35
  licenses: []
43
-
44
36
  post_install_message:
45
37
  rdoc_options: []
46
-
47
- require_paths:
38
+ require_paths:
48
39
  - lib
49
- required_ruby_version: !ruby/object:Gem::Requirement
40
+ required_ruby_version: !ruby/object:Gem::Requirement
50
41
  none: false
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: "0"
55
- required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
47
  none: false
57
- requirements:
58
- - - ">="
59
- - !ruby/object:Gem::Version
60
- version: "0"
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
61
52
  requirements: []
62
-
63
53
  rubyforge_project: reallycare_utils
64
- rubygems_version: 1.6.0
54
+ rubygems_version: 1.8.8
65
55
  signing_key:
66
56
  specification_version: 3
67
57
  summary: Shared utilities for ReallyCare sites
68
58
  test_files: []
69
-