status_accessor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/status_accessor.rb +48 -0
- metadata +65 -0
@@ -0,0 +1,48 @@
|
|
1
|
+
module StatusAccessor
|
2
|
+
|
3
|
+
def status_accessor(*args)
|
4
|
+
|
5
|
+
options = args.last.is_a?(Hash) ? args.pop : {}
|
6
|
+
|
7
|
+
# if the second argument is an array, assume the first is an alternative field name
|
8
|
+
if args[1].is_a?(Array)
|
9
|
+
field_name, statuses = args
|
10
|
+
|
11
|
+
# otherwise, assume all the agruments are statuses and default to field name 'status'
|
12
|
+
else
|
13
|
+
field_name = :status
|
14
|
+
statuses = args
|
15
|
+
end
|
16
|
+
|
17
|
+
statuses.each do |s|
|
18
|
+
state = options[:transform] ? s.to_s.send(options[:transform]) : s.to_s.upcase
|
19
|
+
define_method("#{s.to_s.downcase}!") do
|
20
|
+
self.send("#{field_name}=", state)
|
21
|
+
end
|
22
|
+
define_method("#{s.to_s.downcase}?") do
|
23
|
+
self.send(field_name) == state
|
24
|
+
end
|
25
|
+
define_method("update_to_#{s.to_s.downcase}!") do
|
26
|
+
self.update_attribute(field_name, state)
|
27
|
+
end
|
28
|
+
|
29
|
+
# in case we want to use this outside the context of ActiveRecord
|
30
|
+
if self.respond_to?(:named_scope) # rails 2
|
31
|
+
named_scope s.to_s.downcase.to_sym, :conditions => {:"#{field_name}" => state}
|
32
|
+
elsif self.respond_to?(:scope) # rails 3
|
33
|
+
scope s.to_s.downcase.to_sym, :conditions => {:"#{field_name}" => state}
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
class_variable_set "@@#{field_name}_strings", statuses.collect {|s| s.to_s.upcase}
|
39
|
+
|
40
|
+
(class << self; self; end).send(:define_method, "#{field_name}_strings") do
|
41
|
+
class_variable_get "@@#{field_name}_strings"
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: status_accessor
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 27
|
5
|
+
prerelease:
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 0
|
10
|
+
version: 0.1.0
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Rob Anderson
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2012-03-26 00:00:00 Z
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: utility for handling status-like fields
|
22
|
+
email: rob.anderson@paymentcardsolutions.co.uk
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files: []
|
28
|
+
|
29
|
+
files:
|
30
|
+
- lib/status_accessor.rb
|
31
|
+
homepage: http://rubygems.org/gems/status_accessor
|
32
|
+
licenses: []
|
33
|
+
|
34
|
+
post_install_message:
|
35
|
+
rdoc_options: []
|
36
|
+
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ">="
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
hash: 3
|
45
|
+
segments:
|
46
|
+
- 0
|
47
|
+
version: "0"
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ">="
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
hash: 3
|
54
|
+
segments:
|
55
|
+
- 0
|
56
|
+
version: "0"
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.8.15
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: utility for handling status-like fields
|
64
|
+
test_files: []
|
65
|
+
|