multiparameter_attributes_handler 0.0.2 → 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.
- checksums.yaml +15 -0
- data/README.rdoc +28 -10
- data/lib/multiparameter_attributes_handler.rb +2 -2
- data/lib/multiparameter_attributes_handler/manipulator.rb +6 -3
- data/lib/multiparameter_attributes_handler/version.rb +8 -1
- data/test/multiparameter_attributes_handler/manipulator_test.rb +74 -47
- data/test/multiparameter_attributes_handler/multiparameter_attributes_handler_error_test.rb +2 -5
- data/test/multiparameter_attributes_handler_test.rb +24 -0
- metadata +15 -15
checksums.yaml
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
---
|
2
|
+
!binary "U0hBMQ==":
|
3
|
+
metadata.gz: !binary |-
|
4
|
+
ZWFmYjJjMmVhMDMzMGY1NGFlMTY3Yzc1Mjk2OTIyYjdmZjM5MTE3ZA==
|
5
|
+
data.tar.gz: !binary |-
|
6
|
+
ZmEzZWIzMzcwMzM5MGQyZmUzMDkyNTBiODlmN2U1NmFjODUyZGU5OQ==
|
7
|
+
SHA512:
|
8
|
+
metadata.gz: !binary |-
|
9
|
+
OTkwYmE5ZTFmNDRiYzQxMWJhMTNmMTU5N2E5YTUyMjVjZjJiNTZjNzIzMjll
|
10
|
+
NTVhOTZjZTI2MTg1ODA2M2Y4NTY0MTI0MzQ5MjZmMTI0OGY4YmIxOTRkYzMx
|
11
|
+
MDI5N2YyNDY5MmFkOTdjODhjNWNhNTNiYzZjMjI3ODQ5NGQ5NzQ=
|
12
|
+
data.tar.gz: !binary |-
|
13
|
+
YzVjZTY3NmIyZGU5ODdlMDE3OTc0NWIyZjUwMzQ2OGE0ODMzODM2NWYzMDNi
|
14
|
+
M2NhZGFlNjI4NTkwMzI0MDU5YzIxNjU5ZTUzOGVhOTdiNzMzOTZiNTA2YWM2
|
15
|
+
MzA3NzRlNjY2ZWZjY2ViYjdjNjA5NWQzNDEwZGYwMTUyNWZlZGY=
|
data/README.rdoc
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
|
1
|
+
= Multiparameter Attributes Handler
|
2
2
|
|
3
|
-
|
3
|
+
== Background
|
4
4
|
|
5
5
|
Rails form helpers for time, datetime and date return multiparameter attributes
|
6
6
|
that ActiveRecord objects are able to convert into parent attributes.
|
@@ -18,9 +18,9 @@ when set to 24th June 2004, will generate
|
|
18
18
|
}
|
19
19
|
|
20
20
|
ActiveRecord understands this and converts these key/value pairs into a date
|
21
|
-
object that is passed to thing
|
21
|
+
object that is passed to thing#last_read= method.
|
22
22
|
|
23
|
-
|
23
|
+
== A solution without ActiveRecord
|
24
24
|
|
25
25
|
Multiparameter Attributes Handler allows other objects to do the same.
|
26
26
|
|
@@ -35,6 +35,24 @@ This creates a last_read key with a time object derived from the values
|
|
35
35
|
"last_read" => Time.local('2004', '6', '24')
|
36
36
|
}
|
37
37
|
|
38
|
+
=== Modifying the output
|
39
|
+
|
40
|
+
If you need to modify the derived values, pass a block to manipulate_all, and
|
41
|
+
that will be called on each output value.
|
42
|
+
|
43
|
+
thing_params = MultiparameterAttributesHandler.manipulate_all(params[:thing], &:to_s)
|
44
|
+
|
45
|
+
Will create
|
46
|
+
|
47
|
+
thing_params == {
|
48
|
+
"last_read(1i)" => "2004",
|
49
|
+
"last_read(2i)" => "6",
|
50
|
+
"last_read(3i)" => "24",
|
51
|
+
"last_read" => Time.local('2004', '6', '24').to_s
|
52
|
+
}
|
53
|
+
|
54
|
+
== An ActiveResource example
|
55
|
+
|
38
56
|
So for a ActiveResource Thing, this functionality can be added by over-riding
|
39
57
|
the attributes setter:
|
40
58
|
|
@@ -44,6 +62,8 @@ the attributes setter:
|
|
44
62
|
end
|
45
63
|
end
|
46
64
|
|
65
|
+
== Use within a Rails controller
|
66
|
+
|
47
67
|
However, as the params splitting and rebuilding is happening in the views and
|
48
68
|
controller space, it might well make more sense to do the manipulation in the
|
49
69
|
controller:
|
@@ -64,16 +84,14 @@ controller:
|
|
64
84
|
end
|
65
85
|
end
|
66
86
|
|
67
|
-
|
87
|
+
== Only setter affected
|
68
88
|
|
69
89
|
Note that this gem provides handling of the parameters received from the form
|
70
|
-
submission. For the form helpers to work, they need to passed a date or time
|
71
|
-
to the form.
|
90
|
+
submission. For the form helpers to work, they need to be passed a date or time.
|
72
91
|
|
73
92
|
One solution is to override the getter in the model. So for the Thing example
|
74
93
|
above:
|
75
94
|
|
76
|
-
require 'date'
|
77
95
|
class Thing < ActiveResource::Base
|
78
96
|
|
79
97
|
def last_read
|
@@ -83,9 +101,9 @@ above:
|
|
83
101
|
|
84
102
|
Rails form date helpers will then work for last_read:
|
85
103
|
|
86
|
-
|
104
|
+
date_select(:thing, :last_read)
|
87
105
|
|
88
|
-
|
106
|
+
== Installation
|
89
107
|
|
90
108
|
This functionality is made available via the multiparameter_attributes_handler
|
91
109
|
gem. So add this to your gemfile:
|
@@ -2,8 +2,8 @@ require_relative 'multiparameter_attributes_handler/manipulator'
|
|
2
2
|
require_relative 'multiparameter_attributes_handler/multiparameter_attributes_handler_error'
|
3
3
|
module MultiparameterAttributesHandler
|
4
4
|
|
5
|
-
def self.manipulate_all(hash)
|
6
|
-
Manipulator.new(hash).output
|
5
|
+
def self.manipulate_all(hash, &value_mod)
|
6
|
+
Manipulator.new(hash, &value_mod).output
|
7
7
|
end
|
8
8
|
|
9
9
|
end
|
@@ -1,10 +1,11 @@
|
|
1
1
|
module MultiparameterAttributesHandler
|
2
2
|
class Manipulator
|
3
3
|
|
4
|
-
attr_accessor :hash
|
4
|
+
attr_accessor :hash, :value_mod
|
5
5
|
|
6
|
-
def initialize(hash)
|
6
|
+
def initialize(hash, &value_mod)
|
7
7
|
@hash = hash
|
8
|
+
@value_mod = value_mod
|
8
9
|
end
|
9
10
|
|
10
11
|
def multiparameters
|
@@ -24,7 +25,9 @@ module MultiparameterAttributesHandler
|
|
24
25
|
def value_of(multiparameter)
|
25
26
|
values = values_for(multiparameter)
|
26
27
|
return values unless values.kind_of? Array
|
27
|
-
convert_to_time(multiparameter, values)
|
28
|
+
time = convert_to_time(multiparameter, values)
|
29
|
+
return time unless value_mod
|
30
|
+
value_mod.call(time)
|
28
31
|
end
|
29
32
|
|
30
33
|
def output
|
@@ -1,10 +1,17 @@
|
|
1
1
|
module MultiparameterAttributesHandler
|
2
|
-
VERSION = "0.0
|
2
|
+
VERSION = "0.1.0"
|
3
3
|
end
|
4
4
|
|
5
5
|
# History
|
6
6
|
# =======
|
7
7
|
#
|
8
|
+
# 0.1.0: Allowed a block to be user to modify the output
|
9
|
+
# ------------------------------------------------------
|
10
|
+
# A use case was found where the resulting data needed to be stored as a
|
11
|
+
# string, rather than a date. As there may be other ways the output needs
|
12
|
+
# modifying, this functionality was added by allowing a block to be
|
13
|
+
# passed to manipulate_all.
|
14
|
+
#
|
8
15
|
# 0.0.2: Modified usage so that developer decides where manipulation happens
|
9
16
|
# ---------------------------------------------------------------------------
|
10
17
|
#
|
@@ -1,6 +1,3 @@
|
|
1
|
-
# To change this template, choose Tools | Templates
|
2
|
-
# and open the template in the editor.
|
3
|
-
|
4
1
|
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
2
|
|
6
3
|
require 'test/unit'
|
@@ -8,78 +5,108 @@ require_relative '../../lib/multiparameter_attributes_handler'
|
|
8
5
|
|
9
6
|
module MultiparameterAttributesHandler
|
10
7
|
class ManipulatorTest < Test::Unit::TestCase
|
11
|
-
|
12
|
-
def setup
|
13
|
-
@hash = {
|
14
|
-
"last_read(1i)" => "2004",
|
15
|
-
"last_read(2i)" => "6",
|
16
|
-
"last_read(3i)" => "24",
|
17
|
-
"other(1i)" => "2012",
|
18
|
-
"other(2i)" => "2",
|
19
|
-
"other(3i)" => "2",
|
20
|
-
"other(4i)" => "11",
|
21
|
-
"other(5i)" => "35",
|
22
|
-
"foo" => 'bar',
|
23
|
-
"something" => 'ho',
|
24
|
-
"something_else" => 'hum'
|
25
|
-
}
|
26
|
-
@original = @hash.clone
|
27
|
-
@manipulator = Manipulator.new(@hash)
|
28
|
-
@output = @manipulator.output
|
29
|
-
end
|
30
|
-
|
8
|
+
|
31
9
|
def test_multiparameters
|
32
|
-
assert_equal
|
10
|
+
assert_equal ['last_read', 'other'], manipulator.multiparameters
|
33
11
|
end
|
34
12
|
|
35
13
|
def test_values_for
|
36
|
-
assert_equal
|
37
|
-
end
|
38
|
-
|
14
|
+
assert_equal hash['foo'], manipulator.values_for('foo')
|
15
|
+
end
|
16
|
+
|
39
17
|
def test_values_for_multiparameter
|
40
|
-
assert_equal
|
18
|
+
assert_equal ['2004', '6', '24'], manipulator.values_for('last_read')
|
41
19
|
end
|
42
|
-
|
20
|
+
|
43
21
|
def test_value_of
|
44
|
-
assert_equal
|
22
|
+
assert_equal hash['foo'], manipulator.value_of('foo')
|
45
23
|
end
|
46
|
-
|
24
|
+
|
47
25
|
def test_value_of_multiparameter
|
48
|
-
assert_equal(
|
49
|
-
assert_equal(
|
26
|
+
assert_equal time_for('last_read'), manipulator.value_of('last_read')
|
27
|
+
assert_equal time_for('other'), manipulator.value_of('other')
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_value_mod_changes_values
|
31
|
+
manipulator = Manipulator.new(hash, &:to_s)
|
32
|
+
assert_equal time_for('last_read').to_s, manipulator.value_of('last_read')
|
50
33
|
end
|
51
34
|
|
52
|
-
def
|
53
|
-
|
54
|
-
|
35
|
+
def test_value_mod_with_lambda
|
36
|
+
manipulator = Manipulator.new(hash) {|value| value + 1}
|
37
|
+
assert_equal (time_for('last_read') + 1), manipulator.value_of('last_read')
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_output_returns_original_content
|
41
|
+
build_manipulator
|
42
|
+
hash.each do |key, value|
|
43
|
+
assert_equal value, output[key]
|
55
44
|
end
|
56
45
|
end
|
57
|
-
|
46
|
+
|
58
47
|
def test_output_returns_multiparameter_content_and_original_content
|
59
|
-
combined_length =
|
60
|
-
assert_equal
|
48
|
+
combined_length = original.length + manipulator.multiparameters.length
|
49
|
+
assert_equal combined_length, output.length
|
61
50
|
end
|
62
|
-
|
51
|
+
|
63
52
|
def test_output_returns_multiparameter_content
|
64
|
-
|
65
|
-
assert_equal
|
53
|
+
manipulator.multiparameters.each do |multiparameter|
|
54
|
+
assert_equal manipulator.value_of(multiparameter), output[multiparameter]
|
66
55
|
end
|
67
56
|
end
|
68
|
-
|
57
|
+
|
69
58
|
def test_output_manipulates_hash
|
70
|
-
|
71
|
-
|
59
|
+
build_manipulator
|
60
|
+
assert_not_equal(original, hash)
|
61
|
+
assert_equal output, hash
|
72
62
|
end
|
73
|
-
|
63
|
+
|
74
64
|
def test_partial_multiparameters_are_ignored
|
75
65
|
[
|
76
66
|
{"last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => ""},
|
77
67
|
{"last_read(1i)" => "2004", "last_read(2i)" => "", "last_read(3i)" => "24"},
|
78
68
|
{"last_read(1i)" => "", "last_read(2i)" => "6", "last_read(3i)" => "24"}
|
79
69
|
].each do |params|
|
80
|
-
assert_equal
|
70
|
+
assert_equal [], Manipulator.new(params).multiparameters, "#{params.inspect} should return []"
|
81
71
|
end
|
82
72
|
end
|
73
|
+
|
74
|
+
def hash
|
75
|
+
@hash ||= {
|
76
|
+
"last_read(1i)" => "2004",
|
77
|
+
"last_read(2i)" => "6",
|
78
|
+
"last_read(3i)" => "24",
|
79
|
+
"other(1i)" => "2012",
|
80
|
+
"other(2i)" => "2",
|
81
|
+
"other(3i)" => "2",
|
82
|
+
"other(4i)" => "11",
|
83
|
+
"other(5i)" => "35",
|
84
|
+
"foo" => 'bar',
|
85
|
+
"something" => 'ho',
|
86
|
+
"something_else" => 'hum'
|
87
|
+
}
|
88
|
+
end
|
89
|
+
|
90
|
+
def original
|
91
|
+
@original ||= hash.clone
|
92
|
+
end
|
93
|
+
|
94
|
+
def manipulator
|
95
|
+
@manipulator ||= Manipulator.new(hash)
|
96
|
+
end
|
97
|
+
|
98
|
+
def build_manipulator
|
99
|
+
original # make sure original is generated before manipulator built
|
100
|
+
output # use output to create manipulator to ensure all objects built
|
101
|
+
end
|
102
|
+
|
103
|
+
def output
|
104
|
+
manipulator.output
|
105
|
+
end
|
83
106
|
|
107
|
+
def time_for(field)
|
108
|
+
Time.local *manipulator.values_for(field)
|
109
|
+
end
|
110
|
+
|
84
111
|
end
|
85
112
|
end
|
@@ -1,6 +1,3 @@
|
|
1
|
-
# To change this template, choose Tools | Templates
|
2
|
-
# and open the template in the editor.
|
3
|
-
|
4
1
|
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
5
2
|
|
6
3
|
require 'test/unit'
|
@@ -8,13 +5,13 @@ require_relative '../../lib/multiparameter_attributes_handler'
|
|
8
5
|
|
9
6
|
module MultiparameterAttributesHandler
|
10
7
|
class MultiparameterAttributesHandlerErrorTest < Test::Unit::TestCase
|
11
|
-
|
8
|
+
|
12
9
|
def test_error
|
13
10
|
assert_raise RuntimeError do
|
14
11
|
raise "Oh yes"
|
15
12
|
end
|
16
13
|
end
|
17
|
-
|
14
|
+
|
18
15
|
def test_attribute_assignment_error
|
19
16
|
error = assert_raise AttributeAssignmentError do
|
20
17
|
raise AttributeAssignmentError.new('b', 'c'), 'd'
|
@@ -0,0 +1,24 @@
|
|
1
|
+
$:.unshift File.join(File.dirname(__FILE__),'..','lib')
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'multiparameter_attributes_handler'
|
5
|
+
|
6
|
+
# NB most testing of MultiparameterAttributesHandler.manipulate_all is done
|
7
|
+
# via the thing_test.
|
8
|
+
class MultiparameterAttributesHandlerTest < Test::Unit::TestCase
|
9
|
+
|
10
|
+
def test_manipulate_all
|
11
|
+
output = MultiparameterAttributesHandler.manipulate_all(attributes)
|
12
|
+
assert_equal Time.local(2004, 6, 24), output['last_read']
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_manipulate_all_with_block
|
16
|
+
output = MultiparameterAttributesHandler.manipulate_all(attributes, &:to_s)
|
17
|
+
assert_equal Time.local(2004, 6, 24).to_s, output['last_read']
|
18
|
+
end
|
19
|
+
|
20
|
+
def attributes
|
21
|
+
{ "last_read(1i)" => "2004", "last_read(2i)" => "6", "last_read(3i)" => "24" }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
CHANGED
@@ -1,15 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multiparameter_attributes_handler
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
5
|
-
prerelease:
|
4
|
+
version: 0.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Rob Nichols
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2015-03-27 00:00:00.000000000 Z
|
13
12
|
dependencies: []
|
14
13
|
description: Rails forms helpers for date and time fields generate multiparameter
|
15
14
|
params. multiparameter_attributes_handler allows objects to assign these to thier
|
@@ -20,43 +19,44 @@ executables: []
|
|
20
19
|
extensions: []
|
21
20
|
extra_rdoc_files: []
|
22
21
|
files:
|
23
|
-
- lib/multiparameter_attributes_handler/version.rb
|
24
|
-
- lib/multiparameter_attributes_handler/manipulator.rb
|
25
|
-
- lib/multiparameter_attributes_handler/multiparameter_attributes_handler_error.rb
|
26
|
-
- lib/multiparameter_attributes_handler.rb
|
27
22
|
- MIT-LICENSE
|
28
|
-
- Rakefile
|
29
23
|
- README.rdoc
|
30
|
-
-
|
31
|
-
-
|
24
|
+
- Rakefile
|
25
|
+
- lib/multiparameter_attributes_handler.rb
|
26
|
+
- lib/multiparameter_attributes_handler/manipulator.rb
|
27
|
+
- lib/multiparameter_attributes_handler/multiparameter_attributes_handler_error.rb
|
28
|
+
- lib/multiparameter_attributes_handler/version.rb
|
32
29
|
- test/multiparameter_attributes_handler/manipulator_test.rb
|
30
|
+
- test/multiparameter_attributes_handler/multiparameter_attributes_handler_error_test.rb
|
31
|
+
- test/multiparameter_attributes_handler_test.rb
|
32
|
+
- test/thing.rb
|
33
33
|
- test/thing_test.rb
|
34
34
|
homepage: https://github.com/reggieb/multiparameter_attributes_handler
|
35
35
|
licenses: []
|
36
|
+
metadata: {}
|
36
37
|
post_install_message:
|
37
38
|
rdoc_options: []
|
38
39
|
require_paths:
|
39
40
|
- lib
|
40
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
42
|
requirements:
|
43
43
|
- - ! '>='
|
44
44
|
- !ruby/object:Gem::Version
|
45
45
|
version: '0'
|
46
46
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
47
|
-
none: false
|
48
47
|
requirements:
|
49
48
|
- - ! '>='
|
50
49
|
- !ruby/object:Gem::Version
|
51
50
|
version: '0'
|
52
51
|
requirements: []
|
53
52
|
rubyforge_project:
|
54
|
-
rubygems_version:
|
53
|
+
rubygems_version: 2.4.3
|
55
54
|
signing_key:
|
56
|
-
specification_version:
|
55
|
+
specification_version: 4
|
57
56
|
summary: Allows objects with attributes, to handle multiparameter params
|
58
57
|
test_files:
|
58
|
+
- test/thing_test.rb
|
59
|
+
- test/multiparameter_attributes_handler_test.rb
|
59
60
|
- test/thing.rb
|
60
61
|
- test/multiparameter_attributes_handler/multiparameter_attributes_handler_error_test.rb
|
61
62
|
- test/multiparameter_attributes_handler/manipulator_test.rb
|
62
|
-
- test/thing_test.rb
|