reallycare_utils 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in reallycare_utils.gemspec
4
+ gemspec
5
+
6
+ gem "rspec"
7
+ gem "fuubar"
data/README.textile ADDED
@@ -0,0 +1 @@
1
+ Hello
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+
6
+ RSpec::Core::RakeTask.new('spec')
7
+
8
+ # If you want to make this the default task
9
+ # task :default => :spec
@@ -0,0 +1,2 @@
1
+ ActiveRecord::Base.send(:include, ReallycareUtils::Bitmask)
2
+ Rails.logger.debug("AJ: Including")
@@ -0,0 +1,56 @@
1
+ module ReallycareUtils
2
+ module Bitmask
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)
22
+ end
23
+ has_it = (self[@@has_bitmask_field] & this_bit > 0)
24
+ else
25
+ has_it = false
26
+ end
27
+ has_it
28
+ end
29
+
30
+ define_method "add_#{@@has_bitmask_field}" do |bitmask_const, save_it=false|
31
+ this_bit = self.class.bitmask_value(bitmask_const)
32
+ if self[@@has_bitmask_field]
33
+ self[@@has_bitmask_field] = self[@@has_bitmask_field] + this_bit
34
+ else
35
+ self[@@has_bitmask_field] = this_bit
36
+ end
37
+ self.save! if save_it
38
+ end
39
+
40
+ define_method "remove_#{@@has_bitmask_field}" do |bitmask_const, save_it=false|
41
+ this_bit = self.class.bitmask_value(bitmask_const)
42
+ if self[@@has_bitmask_field]
43
+ self[@@has_bitmask_field] = self[@@has_bitmask_field] - this_bit
44
+ self.save! if save_it
45
+ end
46
+ end
47
+
48
+ end
49
+ end
50
+
51
+ def self.included(receiver)
52
+ receiver.extend ClassMethods
53
+ # receiver.send :include, InstanceMethods
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,3 @@
1
+ module ReallycareUtils
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,7 @@
1
+ require 'reallycare_utils/bitmask'
2
+ require 'activerecord'
3
+ require 'reallycare_utils/activerecord_extensions'
4
+
5
+ module ReallycareUtils
6
+ # Your code goes here...
7
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "reallycare_utils/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "reallycare_utils"
7
+ s.version = ReallycareUtils::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Mark Chapman", "Andy Jeffries"]
10
+ s.email = ["mark.chapman@gmail.com", "andy@andyjeffries.co.uk"]
11
+ s.homepage = ""
12
+ s.summary = %q{Shared utilities for ReallyCare sites}
13
+ s.description = %q{A collection of shared utilities for ReallyCare sites}
14
+
15
+ s.rubyforge_project = "reallycare_utils"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,72 @@
1
+ require File.join(File.dirname(__FILE__), 'spec_helper')
2
+
3
+ class TestBitmaskBase
4
+ def [](name)
5
+ @attributes ||= {}
6
+ @attributes[name]
7
+ end
8
+
9
+ def []=(name, value)
10
+ @attributes ||= {}
11
+ @attributes[name] = value
12
+ end
13
+ end
14
+
15
+ class TestBitmask < TestBitmaskBase
16
+ include ReallycareUtils::Bitmask
17
+ has_bitmask :status, [:pending, :new, :finished]
18
+ end
19
+
20
+ describe "Bitmask" do
21
+ it "should be able to set bitmask constants" do
22
+ o = TestBitmask.new
23
+ o.add_status :pending
24
+ o[:status].should == 1
25
+ o.add_status :new
26
+ o[:status].should == 3
27
+ end
28
+
29
+ it "should be able to check if bitmask constants are set" do
30
+ o = TestBitmask.new
31
+ o.add_status :pending
32
+ o.has_status?(:pending).should be_true
33
+ o.has_status?(:new).should be_false
34
+ end
35
+
36
+ it "should be able to unset bitmask constants" do
37
+ o = TestBitmask.new
38
+ o.add_status :pending
39
+ o.has_status?(:pending).should be_true
40
+ o.remove_status :pending
41
+ o.has_status?(:pending).should be_false
42
+ end
43
+
44
+ it "should save when setting if the second parameter is true" do
45
+ o = TestBitmask.new
46
+ o.should_receive("save!".to_sym).once
47
+ o.add_status(:pending, true)
48
+ end
49
+
50
+ it "should not save when setting if the second parameter is false or missing" do
51
+ o = TestBitmask.new
52
+ o.should_not_receive("save!".to_sym)
53
+ o.add_status(:pending, false)
54
+ o.add_status(:pending)
55
+ end
56
+
57
+ it "should save when unsetting if the second parameter is true" do
58
+ o = TestBitmask.new
59
+ o.should_receive("save!".to_sym).once
60
+ o.add_status(:pending, true)
61
+ end
62
+
63
+ it "should not save when unsetting if the second parameter is false or missing" do
64
+ o = TestBitmask.new
65
+ o.should_not_receive("save!".to_sym)
66
+ o.add_status(:pending)
67
+ o.remove_status(:pending, false)
68
+ o.add_status(:pending)
69
+ o.remove_status(:pending)
70
+ end
71
+
72
+ end
@@ -0,0 +1,6 @@
1
+ SPEC_DIR = File.dirname(__FILE__)
2
+ lib_path = File.expand_path("#{SPEC_DIR}/../lib")
3
+ $LOAD_PATH.unshift lib_path unless $LOAD_PATH.include?(lib_path)
4
+
5
+ require 'reallycare_utils'
6
+
metadata ADDED
@@ -0,0 +1,70 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: reallycare_utils
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Mark Chapman
9
+ - Andy Jeffries
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+
14
+ date: 2011-03-26 23:00:00 +00:00
15
+ default_executable:
16
+ dependencies: []
17
+
18
+ description: A collection of shared utilities for ReallyCare sites
19
+ email:
20
+ - mark.chapman@gmail.com
21
+ - andy@andyjeffries.co.uk
22
+ executables: []
23
+
24
+ extensions: []
25
+
26
+ extra_rdoc_files: []
27
+
28
+ files:
29
+ - .gitignore
30
+ - Gemfile
31
+ - README.textile
32
+ - Rakefile
33
+ - lib/reallycare_utils.rb
34
+ - lib/reallycare_utils/activerecord_extensions.rb
35
+ - lib/reallycare_utils/bitmask.rb
36
+ - lib/reallycare_utils/version.rb
37
+ - reallycare_utils.gemspec
38
+ - spec/bitmask_spec.rb
39
+ - spec/spec_helper.rb
40
+ has_rdoc: true
41
+ homepage: ""
42
+ licenses: []
43
+
44
+ post_install_message:
45
+ rdoc_options: []
46
+
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: "0"
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: "0"
61
+ requirements: []
62
+
63
+ rubyforge_project: reallycare_utils
64
+ rubygems_version: 1.6.1
65
+ signing_key:
66
+ specification_version: 3
67
+ summary: Shared utilities for ReallyCare sites
68
+ test_files:
69
+ - spec/bitmask_spec.rb
70
+ - spec/spec_helper.rb