status_accessor 0.1.0 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/README.md ADDED
@@ -0,0 +1,70 @@
1
+ status_accessor
2
+ ===============
3
+
4
+ A simple bit of code we keep reusing.
5
+
6
+ It provides a quick way of setting up accessors for a status-like field,
7
+ which by default is called :status, but can be anything
8
+
9
+ Examples
10
+ --------
11
+
12
+ ```ruby
13
+ status_accessor :open, :closed
14
+ ```
15
+
16
+ is equivalent to
17
+
18
+ ```ruby
19
+ @@status_strings = ['OPEN', 'CLOSED']
20
+
21
+ def open?
22
+ status == 'OPEN'
23
+ end
24
+
25
+ def open!
26
+ status = 'OPEN'
27
+ end
28
+
29
+ def closed?
30
+ status == 'CLOSED'
31
+ end
32
+
33
+ def closed!
34
+ status = 'CLOSED'
35
+ end
36
+
37
+ # also adds appropriate named_scope accessors if the class responds to
38
+ # named_scope (ie it is an ActiveRecord subclass)
39
+
40
+ named_scope :open, :conditions => {:status => 'OPEN'}
41
+ named_scope :closed, :conditions => {:status => 'CLOSED'}
42
+ ```
43
+
44
+ An alternative field name can also be specified to override the default fieldname of 'status' like this:
45
+
46
+ ```ruby
47
+ status_accessor :foo, [:open, :closed]
48
+ ```
49
+
50
+ which works as expected so
51
+
52
+ ```ruby
53
+ obj.open!
54
+ obj.foo
55
+ # => 'OPEN'
56
+
57
+ Obj.foo_strings
58
+ # => ['OPEN', 'CLOSED']
59
+ ```
60
+
61
+ By default statuses are stored upcase. This behaviour can be modified by passing a :transform option eg
62
+
63
+ ```ruby
64
+ status_accessor :open, :closed, :transform => :downcase
65
+
66
+ # then
67
+ obj.open!
68
+ obj.status
69
+ # => 'open'
70
+ ```
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
@@ -38,7 +38,7 @@ module StatusAccessor
38
38
  class_variable_set "@@#{field_name}_strings", statuses.collect {|s| s.to_s.upcase}
39
39
 
40
40
  (class << self; self; end).send(:define_method, "#{field_name}_strings") do
41
- class_variable_get "@@#{field_name}_strings"
41
+ class_variable_get("@@#{field_name}_strings").clone
42
42
  end
43
43
 
44
44
  end
data/lib/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module StatusAccessor
2
+ VERSION = '1.0'
3
+ end
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'status_accessor'
6
+ s.version = StatusAccessor::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.date = '2012-03-26'
9
+ s.summary = "utility for handling status-like fields"
10
+ s.description = "utility for handling status-like fields"
11
+ s.authors = "Rob Anderson"
12
+ s.email = 'rob.anderson@paymentcardsolutions.co.uk'
13
+ s.files = ["lib/status_accessor.rb"]
14
+ s.homepage = 'https://github.com/rob-anderson/status_accessor.git'
15
+
16
+ s.required_rubygems_version = ">= 1.3.6"
17
+
18
+ s.add_development_dependency "bundler", ">= 1.0.0"
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
22
+ s.require_path = 'lib'
23
+ end
@@ -0,0 +1,87 @@
1
+ require 'lib/status_accessor'
2
+ require 'test/unit'
3
+
4
+ class StatusAccessorTest < Test::Unit::TestCase
5
+
6
+ class Test1
7
+ extend StatusAccessor
8
+ status_accessor :cheese, :ham
9
+ end
10
+ class Test2
11
+ extend StatusAccessor
12
+ status_accessor :chalk
13
+ end
14
+
15
+ def test_statuses_are_unique_to_classes
16
+ assert_equal ['CHEESE', 'HAM'], Test1.status_strings
17
+ assert_equal ['CHALK'], Test2.status_strings
18
+ assert_equal ['CHEESE', 'HAM'], Test1.status_strings # reading the Test2 definition hasn't overwritten class variable
19
+ end
20
+
21
+ def test_status_strings_provdes_cloned_instance
22
+ assert_equal ['Any', 'CHEESE', 'HAM'], Test1.status_strings.unshift('Any')
23
+ assert_equal ['CHEESE', 'HAM'], Test1.status_strings
24
+ end
25
+
26
+ class Valve
27
+ extend StatusAccessor
28
+ attr_accessor :status
29
+ status_accessor :open, :closed
30
+ end
31
+
32
+ def test_setters_and_getters_work_as_expected
33
+ v = Valve.new
34
+ v.open!
35
+ assert_equal 'OPEN', v.status
36
+ v.closed!
37
+ assert_equal 'CLOSED', v.status
38
+ assert v.closed?
39
+ assert !v.open?
40
+ end
41
+
42
+ class Chameleon
43
+ attr_accessor :colour
44
+ extend StatusAccessor
45
+ status_accessor :colour, [:red, :blue, :green]
46
+ end
47
+
48
+ def test_default_status_field_name_can_be_overwritten
49
+ assert_equal %w{RED BLUE GREEN}, Chameleon.colour_strings
50
+ c = Chameleon.new
51
+ c.blue!
52
+ assert_equal 'BLUE', c.colour
53
+ assert c.blue?
54
+ end
55
+
56
+ class Camel
57
+ attr_accessor :hump
58
+ extend StatusAccessor
59
+ status_accessor :hump, [:single_hump, :double_hump], :transform => :capitalize
60
+ end
61
+
62
+ def status_formatting_can_be_set_via_a_transform_option
63
+ c = Camel.new
64
+ c.single_hump!
65
+ assert_equal "Single_hump", c.hump
66
+ end
67
+
68
+ class MockedActiveRecord
69
+ class << self
70
+ @@nops = []
71
+ def named_scope name, options
72
+ @@nops << [name, options]
73
+ end
74
+ def named_scopes
75
+ @@nops
76
+ end
77
+ end
78
+ extend StatusAccessor
79
+ status_accessor :ham, :spam
80
+ end
81
+
82
+ def test_named_scopes
83
+ assert_equal [:ham, {:conditions => {:status => 'HAM'}}], MockedActiveRecord.named_scopes.first
84
+ end
85
+
86
+ end
87
+
metadata CHANGED
@@ -1,13 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: status_accessor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 27
4
+ hash: 15
5
5
  prerelease:
6
6
  segments:
7
- - 0
8
7
  - 1
9
8
  - 0
10
- version: 0.1.0
9
+ version: "1.0"
11
10
  platform: ruby
12
11
  authors:
13
12
  - Rob Anderson
@@ -16,8 +15,23 @@ bindir: bin
16
15
  cert_chain: []
17
16
 
18
17
  date: 2012-03-26 00:00:00 Z
19
- dependencies: []
20
-
18
+ dependencies:
19
+ - !ruby/object:Gem::Dependency
20
+ name: bundler
21
+ prerelease: false
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ hash: 23
28
+ segments:
29
+ - 1
30
+ - 0
31
+ - 0
32
+ version: 1.0.0
33
+ type: :development
34
+ version_requirements: *id001
21
35
  description: utility for handling status-like fields
22
36
  email: rob.anderson@paymentcardsolutions.co.uk
23
37
  executables: []
@@ -27,8 +41,14 @@ extensions: []
27
41
  extra_rdoc_files: []
28
42
 
29
43
  files:
44
+ - .gitignore
45
+ - README.md
46
+ - Rakefile
30
47
  - lib/status_accessor.rb
31
- homepage: http://rubygems.org/gems/status_accessor
48
+ - lib/version.rb
49
+ - status_accessor.gemspec
50
+ - test/status_accessor_test.rb
51
+ homepage: https://github.com/rob-anderson/status_accessor.git
32
52
  licenses: []
33
53
 
34
54
  post_install_message:
@@ -50,14 +70,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
50
70
  requirements:
51
71
  - - ">="
52
72
  - !ruby/object:Gem::Version
53
- hash: 3
73
+ hash: 23
54
74
  segments:
55
- - 0
56
- version: "0"
75
+ - 1
76
+ - 3
77
+ - 6
78
+ version: 1.3.6
57
79
  requirements: []
58
80
 
59
81
  rubyforge_project:
60
- rubygems_version: 1.8.15
82
+ rubygems_version: 1.8.24
61
83
  signing_key:
62
84
  specification_version: 3
63
85
  summary: utility for handling status-like fields