parameter_normalizer 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +33 -0
- data/lib/parameter_normalizer/controller_concerns/normalizable.rb +11 -0
- data/lib/parameter_normalizer/normalizer.rb +48 -0
- data/lib/parameter_normalizer/railtie.rb +11 -0
- data/lib/parameter_normalizer/version.rb +5 -0
- data/lib/parameter_normalizer.rb +12 -0
- data/sig/parameter_normalizer/controller_concerns/normalizable.rbs +9 -0
- data/sig/parameter_normalizer/normalizer.rbs +15 -0
- data/sig/parameter_normalizer.rbs +4 -0
- metadata +96 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d07ff233a093a1141aaf3030dc21cb66e4aa91148555ba5f26c1f98f140487fe
|
4
|
+
data.tar.gz: 45432ed88870f637720a5248e3b3eae540593c28456745ddb6813249665d73ea
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 594b7fc96c126223af7fc78b03bb05f8b8eb7785c615b2d600e822438b87df4031deabff37038b268b9af027f390768c4cfb51843d2959b21ba0de45cec3d390
|
7
|
+
data.tar.gz: 42e5d03971318c33d18699bb7d642c6121c6b96ad84579d456740353f67dd170f503d990b46ad7c2dfd13db49f74f09ed05f3c46bc48bbbd7f965ca9866ca6e7
|
data/README.md
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# ParameterNormalizer
|
2
|
+
|
3
|
+
TODO: Delete this and the text below, and describe your gem
|
4
|
+
|
5
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/parameter_normalizer`. To experiment with that code, run `bin/console` for an interactive prompt.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Install the gem and add to the application's Gemfile by executing:
|
10
|
+
|
11
|
+
$ bundle add parameter_normalizer
|
12
|
+
|
13
|
+
If bundler is not being used to manage dependencies, install the gem by executing:
|
14
|
+
|
15
|
+
$ gem install parameter_normalizer
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
TODO: Write usage instructions here
|
20
|
+
|
21
|
+
## Development
|
22
|
+
|
23
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
24
|
+
|
25
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/taka0125/parameter_normalizer.
|
30
|
+
|
31
|
+
## License
|
32
|
+
|
33
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,48 @@
|
|
1
|
+
module ParameterNormalizer
|
2
|
+
class Normalizer
|
3
|
+
def as_integer(value)
|
4
|
+
Integer(value)
|
5
|
+
rescue StandardError
|
6
|
+
nil
|
7
|
+
end
|
8
|
+
|
9
|
+
def as_integer_array(value)
|
10
|
+
Array(value).filter_map do |record|
|
11
|
+
as_integer(record)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def as_integer_hash(value)
|
16
|
+
{}.tap do |results|
|
17
|
+
next if value.blank?
|
18
|
+
|
19
|
+
value.each do |k, v|
|
20
|
+
results[k] = as_integer(v)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def as_boolean(value)
|
26
|
+
return true if value.in?(['1', 1, true, 'true'])
|
27
|
+
|
28
|
+
false
|
29
|
+
end
|
30
|
+
|
31
|
+
def as_datetime(value)
|
32
|
+
return if value.blank?
|
33
|
+
|
34
|
+
Time.zone.parse(value)
|
35
|
+
rescue StandardError
|
36
|
+
nil
|
37
|
+
end
|
38
|
+
|
39
|
+
def as_date(value)
|
40
|
+
return if value.blank?
|
41
|
+
|
42
|
+
datetime = Time.zone.parse(value)
|
43
|
+
datetime.to_date
|
44
|
+
rescue StandardError
|
45
|
+
nil
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'parameter_normalizer/controller_concerns/normalizable'
|
2
|
+
|
3
|
+
module ParameterNormalizer
|
4
|
+
class Railtie < ::Rails::Railtie
|
5
|
+
initializer 'parameter_normalizer.controller_concerns' do
|
6
|
+
ActiveSupport.on_load(:action_controller_base) do
|
7
|
+
include ParameterNormalizer::ControllerConcerns::Normalizable
|
8
|
+
end
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_support/core_ext'
|
5
|
+
|
6
|
+
require_relative "parameter_normalizer/version"
|
7
|
+
require_relative "parameter_normalizer/normalizer"
|
8
|
+
require_relative "parameter_normalizer/railtie" if defined?(::Rails::Railtie)
|
9
|
+
|
10
|
+
module ParameterNormalizer
|
11
|
+
class Error < StandardError; end
|
12
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
module ParameterNormalizer
|
2
|
+
class Normalizer
|
3
|
+
def as_boolean: -> bool
|
4
|
+
|
5
|
+
def as_date: -> Date?
|
6
|
+
|
7
|
+
def as_datetime: -> ActiveSupport::TimeWithZone?
|
8
|
+
|
9
|
+
def as_integer: -> Integer?
|
10
|
+
|
11
|
+
def as_integer_array: -> [Integer?]
|
12
|
+
|
13
|
+
def as_integer_hash: -> Hash[untyped, Integer?]
|
14
|
+
end
|
15
|
+
end
|
metadata
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: parameter_normalizer
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Takahiro Ooishi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-08-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: appraisal
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: A utility gem for normalizing parameters in Rails applications.
|
56
|
+
email:
|
57
|
+
- taka0125@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- README.md
|
63
|
+
- lib/parameter_normalizer.rb
|
64
|
+
- lib/parameter_normalizer/controller_concerns/normalizable.rb
|
65
|
+
- lib/parameter_normalizer/normalizer.rb
|
66
|
+
- lib/parameter_normalizer/railtie.rb
|
67
|
+
- lib/parameter_normalizer/version.rb
|
68
|
+
- sig/parameter_normalizer.rbs
|
69
|
+
- sig/parameter_normalizer/controller_concerns/normalizable.rbs
|
70
|
+
- sig/parameter_normalizer/normalizer.rbs
|
71
|
+
homepage: https://github.com/taka0125/parameter_normalizer
|
72
|
+
licenses:
|
73
|
+
- MIT
|
74
|
+
metadata:
|
75
|
+
homepage_uri: https://github.com/taka0125/parameter_normalizer
|
76
|
+
source_code_uri: https://github.com/taka0125/parameter_normalizer
|
77
|
+
post_install_message:
|
78
|
+
rdoc_options: []
|
79
|
+
require_paths:
|
80
|
+
- lib
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
82
|
+
requirements:
|
83
|
+
- - ">="
|
84
|
+
- !ruby/object:Gem::Version
|
85
|
+
version: 3.1.0
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
87
|
+
requirements:
|
88
|
+
- - ">="
|
89
|
+
- !ruby/object:Gem::Version
|
90
|
+
version: '0'
|
91
|
+
requirements: []
|
92
|
+
rubygems_version: 3.4.19
|
93
|
+
signing_key:
|
94
|
+
specification_version: 4
|
95
|
+
summary: A utility gem for normalizing parameters in Rails applications.
|
96
|
+
test_files: []
|