shuber-hattr_accessor 1.0.3 → 1.0.4
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/CHANGELOG +4 -0
- data/README.markdown +35 -45
- data/lib/hattr_accessor.rb +13 -9
- data/test/hattr_accessor_test.rb +17 -7
- metadata +2 -3
- data/lib/alias_method_chain.rb +0 -21
data/CHANGELOG
CHANGED
|
@@ -1,3 +1,7 @@
|
|
|
1
|
+
2009-01-12 - Sean Huber (shuber@huberry.com)
|
|
2
|
+
* Check for existing reader and writer methods separately before creating default ones for options[:attribute]
|
|
3
|
+
* Remove alias_method_chain - unnecessary
|
|
4
|
+
|
|
1
5
|
2009-01-10 - Sean Huber (shuber@huberry.com)
|
|
2
6
|
* Object includes Huberry::AliasMethodChain instead of Module
|
|
3
7
|
* Remove init.rb
|
data/README.markdown
CHANGED
|
@@ -1,54 +1,55 @@
|
|
|
1
1
|
hattr\_accessor
|
|
2
2
|
===============
|
|
3
3
|
|
|
4
|
-
Allows you to define attr\_accessors that reference members of a hash
|
|
4
|
+
Allows you to define attr\_accessors that reference members of a hash
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
Installation
|
|
8
8
|
------------
|
|
9
9
|
|
|
10
10
|
gem install shuber-hattr_accessor --source http://gems.github.com
|
|
11
|
-
OR
|
|
12
|
-
script/plugin install git://github.com/shuber/proxy.git
|
|
13
11
|
|
|
14
12
|
|
|
15
13
|
Usage
|
|
16
14
|
-----
|
|
17
15
|
|
|
18
|
-
The hattr\_accessor method requires an option named `:attribute` which should be
|
|
16
|
+
The hattr\_accessor method requires an option named `:attribute` which should be name of an attribute which will store the hash. For example:
|
|
19
17
|
|
|
20
|
-
class
|
|
21
|
-
|
|
22
|
-
hattr_accessor :my_attr, :attribute => :my_hash
|
|
18
|
+
class DataSource
|
|
19
|
+
hattr_accessor :adapter, :username, :password, :attribute => :credentials
|
|
23
20
|
end
|
|
24
|
-
|
|
25
|
-
@some_class = SomeClass.new
|
|
26
|
-
@some_class.my_attr = 'test'
|
|
27
|
-
@some_class.my_hash # => { :my_attr => 'test' }
|
|
28
21
|
|
|
29
|
-
|
|
22
|
+
The reader and writer methods for `:attribute` (`:credentials` in the example above) would be automatically created unless they exist already.
|
|
23
|
+
You can then use those attributes like normal ones:
|
|
30
24
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
25
|
+
@data_source = DataSource.new
|
|
26
|
+
@data_source.adapter = 'mysql'
|
|
27
|
+
@data_source.username = 'root'
|
|
28
|
+
@data_source.credentials # { :adapter => 'mysql', :username => 'root' }
|
|
35
29
|
|
|
36
|
-
@
|
|
37
|
-
@
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
@some_class.birth_year # => 2008
|
|
30
|
+
@data_source.credentials = {}
|
|
31
|
+
@data_source.adapter # nil
|
|
32
|
+
|
|
33
|
+
The reader method for `:attribute` is overwritten with logic to ensure that it returns a hash by default.
|
|
41
34
|
|
|
42
|
-
|
|
35
|
+
@data_source = DataSource.new
|
|
36
|
+
@data_source.credentials # {}
|
|
43
37
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
38
|
+
You may optionally pass a `:type` option which will type cast the values when calling their getter methods. This is useful if you're using this
|
|
39
|
+
gem with rails which will pass values as strings if submitted from a form. For example:
|
|
40
|
+
|
|
41
|
+
class CustomField::Date < CustomField
|
|
42
|
+
hattr_accessor :offset, :type => :integer, :attribute => :configuration
|
|
43
|
+
hattr_accessor :unit, :reference, :type => :string, :attribute => :configuration
|
|
44
|
+
|
|
45
|
+
def default_value
|
|
46
|
+
self.offset.send(self.unit.to_sym).send(self.reference.to_sym)
|
|
50
47
|
end
|
|
51
48
|
end
|
|
49
|
+
|
|
50
|
+
@custom_field = CustomField::Date.new(:offset => '5', :unit => 'days', :reference => 'from_now')
|
|
51
|
+
@custom_field.offset # 5 (notice it's an integer, not a string)
|
|
52
|
+
@custom_field.default_value # evaluates 5.days.from_now
|
|
52
53
|
|
|
53
54
|
The current options (email me for suggestions for others) for `:type` are:
|
|
54
55
|
|
|
@@ -57,27 +58,16 @@ The current options (email me for suggestions for others) for `:type` are:
|
|
|
57
58
|
:float
|
|
58
59
|
:boolean
|
|
59
60
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
-------
|
|
61
|
+
NOTE: Make sure your call `define_attribute_methods` before calling `hattr_accessor` when you're using ActiveRecord and your `:attribute` is a
|
|
62
|
+
database field.
|
|
63
63
|
|
|
64
64
|
class CustomField < ActiveRecord::Base
|
|
65
|
-
|
|
65
|
+
define_attribute_methods
|
|
66
|
+
|
|
66
67
|
serialize :configuration, Hash
|
|
68
|
+
|
|
69
|
+
hattr_accessor :testing, :attribute => :configuration
|
|
67
70
|
end
|
|
68
|
-
|
|
69
|
-
class CustomFields::Date < CustomField
|
|
70
|
-
hattr_accessor :offset, :type => :integer, :attribute => :configuration
|
|
71
|
-
hattr_accessor :unit, :reference, :attribute => :configuration
|
|
72
|
-
|
|
73
|
-
def default_value
|
|
74
|
-
self.offset.send(self.unit).send(self.reference)
|
|
75
|
-
end
|
|
76
|
-
end
|
|
77
|
-
|
|
78
|
-
@field = CustomFields::Date.new({ :offset => '5', :unit => 'days', :reference => 'from_now' })
|
|
79
|
-
@field.configuration # => { :offset => '5', :unit => 'days', :reference => 'from_now' }
|
|
80
|
-
@field.default_value # => evaluates 5.days.from_now
|
|
81
71
|
|
|
82
72
|
|
|
83
73
|
Contact
|
data/lib/hattr_accessor.rb
CHANGED
|
@@ -1,8 +1,3 @@
|
|
|
1
|
-
unless Object.method_defined? :alias_method_chain
|
|
2
|
-
require File.dirname(__FILE__) + '/alias_method_chain'
|
|
3
|
-
Object.send :include, Huberry::AliasMethodChain
|
|
4
|
-
end
|
|
5
|
-
|
|
6
1
|
module Huberry
|
|
7
2
|
module HattrAccessor
|
|
8
3
|
class MissingAttributeError < StandardError; self; end
|
|
@@ -10,7 +5,7 @@ module Huberry
|
|
|
10
5
|
def hattr_accessor(*attrs)
|
|
11
6
|
options = attrs.last.is_a?(Hash) ? attrs.pop : {}
|
|
12
7
|
|
|
13
|
-
raise MissingAttributeError, 'Must specify the :attribute option
|
|
8
|
+
raise MissingAttributeError, 'Must specify the :attribute option with the name of an attribute which will store the hash' if options[:attribute].nil?
|
|
14
9
|
|
|
15
10
|
attrs.each do |name|
|
|
16
11
|
# Defines a type casting getter method for each attribute
|
|
@@ -25,7 +20,7 @@ module Huberry
|
|
|
25
20
|
when :float
|
|
26
21
|
value.to_f
|
|
27
22
|
when :boolean
|
|
28
|
-
![false, nil, 0, '0'].include?
|
|
23
|
+
![false, nil, 0, '0'].include?(value)
|
|
29
24
|
else
|
|
30
25
|
value
|
|
31
26
|
end
|
|
@@ -38,15 +33,24 @@ module Huberry
|
|
|
38
33
|
end
|
|
39
34
|
end
|
|
40
35
|
|
|
36
|
+
# Create the reader for #{options[:attribute]} unless it exists already
|
|
37
|
+
#
|
|
38
|
+
attr_reader options[:attribute] unless instance_methods.include?(options[:attribute].to_s)
|
|
39
|
+
|
|
40
|
+
# Create the writer for #{options[:attribute]} unless it exists already
|
|
41
|
+
#
|
|
42
|
+
attr_writer options[:attribute] unless instance_methods.include?("#{options[:attribute]}=")
|
|
43
|
+
|
|
41
44
|
# Overwrites the method passed as the :attribute option to ensure that it is a hash by default
|
|
42
45
|
#
|
|
43
|
-
unless instance_methods.include?
|
|
46
|
+
unless instance_methods.include?("#{options[:attribute]}_with_hattr_accessor")
|
|
44
47
|
class_eval <<-EOF
|
|
45
48
|
def #{options[:attribute]}_with_hattr_accessor
|
|
46
49
|
self.#{options[:attribute]} = {} if #{options[:attribute]}_without_hattr_accessor.nil?
|
|
47
50
|
#{options[:attribute]}_without_hattr_accessor
|
|
48
51
|
end
|
|
49
|
-
|
|
52
|
+
alias_method :#{options[:attribute]}_without_hattr_accessor, :#{options[:attribute]}
|
|
53
|
+
alias_method :#{options[:attribute]}, :#{options[:attribute]}_with_hattr_accessor
|
|
50
54
|
EOF
|
|
51
55
|
end
|
|
52
56
|
end
|
data/test/hattr_accessor_test.rb
CHANGED
|
@@ -2,12 +2,19 @@ require 'test/unit'
|
|
|
2
2
|
require File.dirname(__FILE__) + '/../lib/hattr_accessor'
|
|
3
3
|
|
|
4
4
|
class CustomField
|
|
5
|
-
attr_accessor :configuration, :configuration2
|
|
6
5
|
hattr_accessor :name, :type, :type => :string, :attribute => :configuration
|
|
7
6
|
hattr_accessor :unit, :reference, :attribute => :configuration
|
|
8
7
|
hattr_accessor :offset, :type => :integer, :attribute => :configuration
|
|
9
8
|
hattr_accessor :amount, :type => :float, :attribute => :configuration
|
|
10
9
|
hattr_accessor :required, :type => :boolean, :attribute => :configuration2
|
|
10
|
+
|
|
11
|
+
def configuration2
|
|
12
|
+
@configuration2 ||= { :some_default_reader_value => true }
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def configuration2=(value)
|
|
16
|
+
@configuration2 = value.merge(:some_default_writer_value => true)
|
|
17
|
+
end
|
|
11
18
|
end
|
|
12
19
|
|
|
13
20
|
class HattrAccessorTest < Test::Unit::TestCase
|
|
@@ -99,7 +106,7 @@ class HattrAccessorTest < Test::Unit::TestCase
|
|
|
99
106
|
|
|
100
107
|
def test_should_set_required_in_configuration2
|
|
101
108
|
@custom_field.required = true
|
|
102
|
-
assert_equal
|
|
109
|
+
assert_equal true, @custom_field.configuration2[:required]
|
|
103
110
|
end
|
|
104
111
|
|
|
105
112
|
def test_should_get_required
|
|
@@ -138,10 +145,13 @@ class HattrAccessorTest < Test::Unit::TestCase
|
|
|
138
145
|
end
|
|
139
146
|
end
|
|
140
147
|
|
|
141
|
-
def
|
|
142
|
-
|
|
143
|
-
CustomField.hattr_accessor :test, :attribute => :non_existent
|
|
144
|
-
end
|
|
148
|
+
def test_should_not_overwrite_existing_reader
|
|
149
|
+
assert_equal true, @custom_field.configuration2[:some_default_reader_value]
|
|
145
150
|
end
|
|
146
|
-
|
|
151
|
+
|
|
152
|
+
def test_should_not_overwrite_existing_writer
|
|
153
|
+
@custom_field.configuration2 = {}
|
|
154
|
+
assert_equal true, @custom_field.configuration2[:some_default_writer_value]
|
|
155
|
+
end
|
|
156
|
+
|
|
147
157
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: shuber-hattr_accessor
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sean Huber
|
|
@@ -9,7 +9,7 @@ autorequire:
|
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
11
|
|
|
12
|
-
date: 2009-01-
|
|
12
|
+
date: 2009-01-12 00:00:00 -08:00
|
|
13
13
|
default_executable:
|
|
14
14
|
dependencies: []
|
|
15
15
|
|
|
@@ -23,7 +23,6 @@ extra_rdoc_files: []
|
|
|
23
23
|
|
|
24
24
|
files:
|
|
25
25
|
- CHANGELOG
|
|
26
|
-
- lib/alias_method_chain.rb
|
|
27
26
|
- lib/hattr_accessor.rb
|
|
28
27
|
- MIT-LICENSE
|
|
29
28
|
- Rakefile
|
data/lib/alias_method_chain.rb
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
module Huberry
|
|
2
|
-
module AliasMethodChain # :nodoc:
|
|
3
|
-
# Defines the alias_method_chain method from rails unless it exists already
|
|
4
|
-
#
|
|
5
|
-
def alias_method_chain(target, feature)
|
|
6
|
-
aliased_target, punctuation = target.to_s.sub(/([?!=])$/, ''), $1
|
|
7
|
-
yield(aliased_target, punctuation) if block_given?
|
|
8
|
-
with_method, without_method = "#{aliased_target}_with_#{feature}#{punctuation}", "#{aliased_target}_without_#{feature}#{punctuation}"
|
|
9
|
-
alias_method without_method, target
|
|
10
|
-
alias_method target, with_method
|
|
11
|
-
case
|
|
12
|
-
when public_method_defined?(without_method)
|
|
13
|
-
public target
|
|
14
|
-
when protected_method_defined?(without_method)
|
|
15
|
-
protected target
|
|
16
|
-
when private_method_defined?(without_method)
|
|
17
|
-
private target
|
|
18
|
-
end
|
|
19
|
-
end
|
|
20
|
-
end
|
|
21
|
-
end
|