virtus-multiparams 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 +7 -0
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +53 -0
- data/Rakefile +8 -0
- data/lib/virtus/multiparams.rb +50 -0
- data/lib/virtus/multiparams/version.rb +5 -0
- data/virtus-multiparams.gemspec +29 -0
- metadata +134 -0
- metadata.gz.sig +0 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 59ccd7bef3cc6d7f9dc7e3c57d7af3af395362d6
|
4
|
+
data.tar.gz: 1f21e2590fb2aba9695218a8055bdb2c1268222f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 434abaab14dd3315fc7c43ecb9fa0286b3f1eb7f0b01a4537ab3df5a2e59b8edf0c6d1675f27c0b9ac583f817d8f7530e31cf9b484e9ff354486a80bfa74bbdd
|
7
|
+
data.tar.gz: a25e99d943f32830656eaff3ff084344f23aadb39094abfad703ac7992342c08988f785130f9f8560fb946233e0bfc3cef8ab76e18db791fae5c4fdf92dca6ec
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
Binary file
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Samuel Cochran
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
# Virtus::Multiparams
|
2
|
+
|
3
|
+
Support for Rails-style multiparameters, which makes `datetime_select` [and
|
4
|
+
friends](http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html)
|
5
|
+
work with [Virtus](https://github.com/solnic/virtus) objects.
|
6
|
+
|
7
|
+
Rails Date, Time and DateTime selectors all use multiparameters. This means
|
8
|
+
they break one attribute into separate parameters for each input, like year,
|
9
|
+
month and day, and reconstitute these multiple parameters into a single
|
10
|
+
parameter to be passed back into a model as an attribute.
|
11
|
+
|
12
|
+
## Usage
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
class PostForm
|
16
|
+
include Virtus
|
17
|
+
include Virtus::Multiparams
|
18
|
+
|
19
|
+
attribute :title, String
|
20
|
+
attribute :publish_at, DateTime
|
21
|
+
end
|
22
|
+
|
23
|
+
# app/views/posts/new.html.erb
|
24
|
+
<%= form_for @post_form do |form| %>
|
25
|
+
<%= form.label :title %>
|
26
|
+
<%= form.text_field :title %>
|
27
|
+
|
28
|
+
<%= form.label :publish_at %>
|
29
|
+
<%= form.datetime_select :publish_at %>
|
30
|
+
<!-- Which does something like:
|
31
|
+
<select name="post_form[publish_at(i1)]"><option>2015</option>...</select>
|
32
|
+
<select name="post_form[publish_at(i2)]"><option value="1">January</option>...<option value="12">December</option></select>
|
33
|
+
<select name="post_form[publish_at(i3)]"><option>1</option>...</select>
|
34
|
+
<select name="post_form[publish_at(i4)]"><option>1</option>...<option>24</option></select>
|
35
|
+
<select name="post_form[publish_at(i5)]"><option>1</option>...<option>60</option></select>
|
36
|
+
<select name="post_form[publish_at(i6)]"><option>1</option>...<option>60</option></select>
|
37
|
+
-->
|
38
|
+
|
39
|
+
<%= form.submit %>
|
40
|
+
<% end %>
|
41
|
+
|
42
|
+
# app/controllers/posts_controller.rb
|
43
|
+
class PostsController
|
44
|
+
def new
|
45
|
+
@post_form = PostForm.new
|
46
|
+
end
|
47
|
+
|
48
|
+
def create
|
49
|
+
@post_form = PostForm.new(params[:post_form])
|
50
|
+
# ...
|
51
|
+
end
|
52
|
+
end
|
53
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "virtus"
|
2
|
+
require "virtus/multiparams/version"
|
3
|
+
|
4
|
+
module Virtus
|
5
|
+
module Multiparams
|
6
|
+
def self.included(base)
|
7
|
+
base.attribute_set.singleton_class.prepend AttributeSetExtension
|
8
|
+
end
|
9
|
+
|
10
|
+
module AttributeSetExtension
|
11
|
+
def coerce(attributes)
|
12
|
+
super(attributes).tap do |attributes|
|
13
|
+
multiparams = Hash.new { |hash, key| hash[key] = {} }
|
14
|
+
|
15
|
+
attributes.each do |(key, value)|
|
16
|
+
if /\A(?<name>.+)\((?<offset>[0-9]+)(?<cast>[if])?\)\Z/ =~ key
|
17
|
+
attributes.delete(key)
|
18
|
+
|
19
|
+
unless attributes.include? name
|
20
|
+
offset = offset.to_i
|
21
|
+
value = value.send("to_#{cast}") if cast
|
22
|
+
multiparams[name][offset] = value
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
multiparams.each do |name, values|
|
28
|
+
array = Array.new(values.keys.max)
|
29
|
+
|
30
|
+
values.each do |offset, value|
|
31
|
+
array[offset - 1] = value
|
32
|
+
end
|
33
|
+
|
34
|
+
# Virus::Attribute has a primitive type which might be Date, Time, DateTime, etc.
|
35
|
+
attribute = self[name]
|
36
|
+
|
37
|
+
attributes[name] =
|
38
|
+
if attribute.primitive <= Date || attribute.primitive <= Time
|
39
|
+
# Basic convesion is enough, Virtus invokes `to_date[time]`
|
40
|
+
# Also, lololol timezones
|
41
|
+
Time.new(*array[0...6]) unless array[0...3].any? { |param| param.nil? || param == 0 }
|
42
|
+
else
|
43
|
+
array
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'virtus/multiparams/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "virtus-multiparams"
|
8
|
+
spec.version = Virtus::Multiparams::VERSION
|
9
|
+
spec.author = "Samuel Cochran"
|
10
|
+
spec.email = "sj26@sj26.com"
|
11
|
+
|
12
|
+
spec.summary = "Rails multiparams support for virtus models"
|
13
|
+
spec.description = <<-END.gsub(/^ {4}/, "")
|
14
|
+
Rails date and time fields generate multiple parameters which need t which
|
15
|
+
need to be combined to create a coercible value. Teach virtus the trick
|
16
|
+
with virtus-multiparams.
|
17
|
+
END
|
18
|
+
spec.homepage = "https://github.com/sj26/virtus-multiparams"
|
19
|
+
spec.license = "MIT"
|
20
|
+
|
21
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "virtus", "~> 1.0"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,134 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: virtus-multiparams
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Samuel Cochran
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain:
|
11
|
+
- |
|
12
|
+
-----BEGIN CERTIFICATE-----
|
13
|
+
MIIDKDCCAhCgAwIBAgIBAzANBgkqhkiG9w0BAQUFADA6MQ0wCwYDVQQDDARzajI2
|
14
|
+
MRQwEgYKCZImiZPyLGQBGRYEc2oyNjETMBEGCgmSJomT8ixkARkWA2NvbTAeFw0x
|
15
|
+
NTAzMTcyMjUwMjZaFw0xNjAzMTYyMjUwMjZaMDoxDTALBgNVBAMMBHNqMjYxFDAS
|
16
|
+
BgoJkiaJk/IsZAEZFgRzajI2MRMwEQYKCZImiZPyLGQBGRYDY29tMIIBIjANBgkq
|
17
|
+
hkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsr60Eo/ttCk8GMTMFiPr3GoYMIMFvLak
|
18
|
+
xSmTk9YGCB6UiEePB4THSSA5w6IPyeaCF/nWkDp3/BAam0eZMWG1IzYQB23TqIM0
|
19
|
+
1xzcNRvFsn0aQoQ00k+sj+G83j3T5OOV5OZIlu8xAChMkQmiPd1NXc6uFv+Iacz7
|
20
|
+
kj+CMsI9YUFdNoU09QY0b+u+Rb6wDYdpyvN60YC30h0h1MeYbvYZJx/iZK4XY5zu
|
21
|
+
4O/FL2ChjL2CPCpLZW55ShYyrzphWJwLOJe+FJ/ZBl6YXwrzQM9HKnt4titSNvyU
|
22
|
+
KzE3L63A3PZvExzLrN9u09kuWLLJfXB2sGOlw3n9t72rJiuBr3/OQQIDAQABozkw
|
23
|
+
NzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU99dfRjEKFyczTeIz
|
24
|
+
m3ZsDWrNC80wDQYJKoZIhvcNAQEFBQADggEBAFxKLjiLkMLkUmdpsAzJad/t7Jo/
|
25
|
+
CGby/3n0WSXPBeZJfsnSdJ2qtG7iy/xqYDc1RjpKgX0RlMgeQRSE3ZDL/HZzBKDF
|
26
|
+
azaTgG9Zk1Quu59/79Z0Sltq07Z/IeccFl5j9M+1YS8VY2mOPi9g03OoOSRmhsMS
|
27
|
+
wpEF+zvJ0ESS5OPjtp6Sk4q1QYc0aVIthEznuVNMW6CPpTNhMAOFMaTC5AXCzJ3Q
|
28
|
+
52G9HuhbVSTgE/I10H9qZBOE3qdP8ka/Fk0PUrux/DuUanNZgSKJokrQvRA4H9Au
|
29
|
+
WpPA7HJYV6msWQiukoBEhfQ2l6Fl2HUwntvX3MCcFNHeJJ5ETERp9alo88E=
|
30
|
+
-----END CERTIFICATE-----
|
31
|
+
date: 2015-05-07 00:00:00.000000000 Z
|
32
|
+
dependencies:
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: virtus
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.0'
|
40
|
+
type: :runtime
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: bundler
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.9'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.9'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rake
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '10.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
- !ruby/object:Gem::Dependency
|
76
|
+
name: rspec
|
77
|
+
requirement: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '3.0'
|
82
|
+
type: :development
|
83
|
+
prerelease: false
|
84
|
+
version_requirements: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '3.0'
|
89
|
+
description: |
|
90
|
+
Rails date and time fields generate multiple parameters which need t which
|
91
|
+
need to be combined to create a coercible value. Teach virtus the trick
|
92
|
+
with virtus-multiparams.
|
93
|
+
email: sj26@sj26.com
|
94
|
+
executables: []
|
95
|
+
extensions: []
|
96
|
+
extra_rdoc_files: []
|
97
|
+
files:
|
98
|
+
- ".gitignore"
|
99
|
+
- ".rspec"
|
100
|
+
- ".travis.yml"
|
101
|
+
- Gemfile
|
102
|
+
- LICENSE.txt
|
103
|
+
- README.md
|
104
|
+
- Rakefile
|
105
|
+
- bin/console
|
106
|
+
- bin/setup
|
107
|
+
- lib/virtus/multiparams.rb
|
108
|
+
- lib/virtus/multiparams/version.rb
|
109
|
+
- virtus-multiparams.gemspec
|
110
|
+
homepage: https://github.com/sj26/virtus-multiparams
|
111
|
+
licenses:
|
112
|
+
- MIT
|
113
|
+
metadata: {}
|
114
|
+
post_install_message:
|
115
|
+
rdoc_options: []
|
116
|
+
require_paths:
|
117
|
+
- lib
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.4.5
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: Rails multiparams support for virtus models
|
134
|
+
test_files: []
|
metadata.gz.sig
ADDED
Binary file
|