hash_accessor 1.0.4 → 1.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/hash_accessor.gemspec +3 -4
- data/lib/hash_accessor.rb +1 -1
- data/lib/hash_accessor/class_methods.rb +43 -44
- data/test/hash_accessor_test.rb +13 -0
- metadata +8 -7
data/README.rdoc
CHANGED
@@ -58,7 +58,7 @@ Here is an example:
|
|
58
58
|
|
59
59
|
== Contributing & Reporting Issues
|
60
60
|
|
61
|
-
While I can't promise the most speedy response, I do plan to continue to maintain this gem. Please feel free to report any issues, provide feedback, or make suggestions on the {Issue Tracker}[http://github.com/
|
61
|
+
While I can't promise the most speedy response, I do plan to continue to maintain this gem. Please feel free to report any issues, provide feedback, or make suggestions on the {Issue Tracker}[http://github.com/GetJobber/hash_accessor/issues]
|
62
62
|
|
63
63
|
|
64
64
|
---
|
data/hash_accessor.gemspec
CHANGED
@@ -3,13 +3,12 @@ require File.expand_path("../lib/hash_accessor", __FILE__)
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = "hash_accessor"
|
5
5
|
s.version = HashAccessor::VERSION
|
6
|
-
s.author = ["Forrest"]
|
7
|
-
s.email = ["
|
6
|
+
s.author = ["Jobber", "Forrest Zeisler"]
|
7
|
+
s.email = ["forrest@getjobber.com"]
|
8
8
|
s.date = Time.now.utc.strftime("%Y-%m-%d")
|
9
9
|
|
10
|
-
|
11
10
|
s.description = 'This gem provides accessor methods to hash keys.'
|
12
|
-
s.summary = "This gem provides extended functionality to serialized hashed in rails. It allows you to define accessor methods for variable that rest "+
|
11
|
+
s.summary = "This gem provides extended functionality to serialized hashed in rails similary to rails' active_attr. It allows you to define accessor methods for variable that rest "+
|
13
12
|
"inside a serialized hash. This is very useful if you have a large list of often changing DB variables on a model which don't get queried against."
|
14
13
|
|
15
14
|
s.required_rubygems_version = ">= 1.3.6"
|
data/lib/hash_accessor.rb
CHANGED
@@ -16,27 +16,26 @@ module HashAccessor
|
|
16
16
|
# :reject_blanks - removes all blank elements after the collect method
|
17
17
|
def hash_accessor(hash_name, method_name, options = {})
|
18
18
|
begin
|
19
|
-
|
20
|
-
default = hash_accessor_default(options)
|
19
|
+
method_name = method_name.to_sym
|
21
20
|
|
22
21
|
# Define getter
|
23
22
|
define_method(method_name) do
|
24
23
|
send("#{hash_name}=", {}) if send(hash_name).blank?
|
25
|
-
if
|
26
|
-
|
24
|
+
if send(hash_name)[method_name].nil?
|
25
|
+
send(hash_name)[method_name] = hash_accessor_default(options)
|
27
26
|
end
|
28
|
-
|
27
|
+
send(hash_name)[method_name]
|
29
28
|
end
|
30
29
|
|
31
30
|
# Define setter
|
32
|
-
define_method("#{method_name}=") do
|
33
|
-
|
34
|
-
|
31
|
+
define_method("#{method_name}=") do |new_val|
|
32
|
+
send("#{hash_name}=", {}) if send(hash_name).blank?
|
33
|
+
method_modifier = hash_accessor_method_modifier(method_name, options)
|
35
34
|
new_val = method_modifier.call(new_val)
|
36
|
-
if
|
35
|
+
if send(hash_name)[method_name] != new_val
|
37
36
|
instance_variable_set("@#{method_name}_changed", true)
|
38
37
|
end
|
39
|
-
|
38
|
+
send(hash_name)[method_name] = new_val
|
40
39
|
end
|
41
40
|
|
42
41
|
# Define changed?
|
@@ -55,47 +54,47 @@ module HashAccessor
|
|
55
54
|
puts "\n\nError adding hash_accessor:\n#{e.message}\n#{e.backtrace}\n\n"
|
56
55
|
end
|
57
56
|
end
|
57
|
+
end
|
58
58
|
|
59
|
-
|
59
|
+
private
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
61
|
+
def hash_accessor_method_modifier(method_name, options)
|
62
|
+
case options[:type]
|
63
|
+
when :integer
|
64
|
+
lambda {|new_val| new_val.try(:to_i) }
|
65
|
+
when :float
|
66
|
+
lambda {|new_val| new_val.try(:to_f) }
|
67
|
+
when :decimal
|
68
|
+
lambda {|new_val| new_val.try(:to_d) }
|
69
|
+
when :boolean, :bool
|
70
|
+
lambda {|new_val| (new_val.is_a?(TrueClass) or (new_val.is_a?(String) and (new_val=~/true|1/i).present?) or (new_val.is_a?(Fixnum) and new_val==1)) }
|
71
|
+
when :array
|
72
|
+
lambda {|new_val|
|
73
|
+
new_val = Array.wrap(new_val)
|
74
74
|
|
75
|
-
|
76
|
-
|
77
|
-
|
75
|
+
if options[:collects]
|
76
|
+
new_val = new_val.collect(&options[:collects])
|
77
|
+
end
|
78
78
|
|
79
|
-
|
80
|
-
|
81
|
-
|
79
|
+
if options[:reject_blanks]
|
80
|
+
new_val.reject!(&:blank?)
|
81
|
+
end
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
end
|
83
|
+
new_val
|
84
|
+
}
|
85
|
+
else lambda {|new_val| new_val}
|
87
86
|
end
|
87
|
+
end
|
88
88
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
end
|
89
|
+
def hash_accessor_default(options)
|
90
|
+
if options[:default].is_a?(String)
|
91
|
+
default = options[:default]
|
92
|
+
elsif options[:default].nil? and options[:type]==:array
|
93
|
+
default = []
|
94
|
+
elsif options[:default].nil?
|
95
|
+
default = nil
|
96
|
+
else
|
97
|
+
default = options[:default]
|
99
98
|
end
|
100
99
|
end
|
101
100
|
|
data/test/hash_accessor_test.rb
CHANGED
@@ -33,6 +33,19 @@ class HashAccessorTest < Test::Unit::TestCase
|
|
33
33
|
assert_equal "some test", @tester.unspecified_variable
|
34
34
|
end
|
35
35
|
|
36
|
+
def test_no_sharing_of_variables
|
37
|
+
t1 = TestClassWithHash.new
|
38
|
+
t2 = TestClassWithHash.new
|
39
|
+
|
40
|
+
t1.unspecified_variable = "some test"
|
41
|
+
t1.test_array_2 << :blah
|
42
|
+
|
43
|
+
assert_equal "some test", t1.unspecified_variable
|
44
|
+
assert_equal [:blah], t1.test_array_2
|
45
|
+
assert_equal nil, t2.unspecified_variable
|
46
|
+
assert_equal [], t2.test_array_2
|
47
|
+
end
|
48
|
+
|
36
49
|
def test_accessors_being_casted_correctly
|
37
50
|
@tester.test_integer = "3"
|
38
51
|
@tester.test_decimal = "3"
|
metadata
CHANGED
@@ -1,11 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hash_accessor
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
|
-
-
|
8
|
+
- Jobber
|
9
|
+
- Forrest Zeisler
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
@@ -29,7 +30,7 @@ dependencies:
|
|
29
30
|
version: 3.2.0
|
30
31
|
description: This gem provides accessor methods to hash keys.
|
31
32
|
email:
|
32
|
-
-
|
33
|
+
- forrest@getjobber.com
|
33
34
|
executables: []
|
34
35
|
extensions: []
|
35
36
|
extra_rdoc_files:
|
@@ -68,10 +69,10 @@ rubyforge_project:
|
|
68
69
|
rubygems_version: 1.8.22
|
69
70
|
signing_key:
|
70
71
|
specification_version: 3
|
71
|
-
summary: This gem provides extended functionality to serialized hashed in rails
|
72
|
-
allows you to define accessor methods for variable that
|
73
|
-
hash. This is very useful if you have a large list of often
|
74
|
-
on a model which don't get queried against.
|
72
|
+
summary: This gem provides extended functionality to serialized hashed in rails similary
|
73
|
+
to rails' active_attr. It allows you to define accessor methods for variable that
|
74
|
+
rest inside a serialized hash. This is very useful if you have a large list of often
|
75
|
+
changing DB variables on a model which don't get queried against.
|
75
76
|
test_files:
|
76
77
|
- test/hash_accessor_test.rb
|
77
78
|
has_rdoc: true
|