easy_params 0.6.3 → 0.7.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 +4 -4
- data/.github/workflows/ruby.yml +27 -38
- data/Gemfile +1 -1
- data/Gemfile.lock +2 -2
- data/README.md +2 -2
- data/lib/easy_params/base.rb +9 -11
- data/lib/easy_params/types.rb +2 -32
- data/lib/easy_params/version.rb +1 -1
- data/lib/easy_params.rb +44 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f63905ee9e223d63e2f3855f7e67ace0f3d2abd14ed97fb1f706f73082f73c90
|
4
|
+
data.tar.gz: 1ce09ad5d8a018e97c9c46eb21b32f5dd537d448830578c8b760ee2c99d903ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 935a110dbedbace4f05a9df987aec2932fec9ccec5cbc9dbb3b4a3460b42971cd7cdafa60c391b24ad334f74e39086c7d81df191e9622844ebda0f4ed9ca952f
|
7
|
+
data.tar.gz: 9ebe7ebeae1961a820e396361e020b5b8cfe712ad81480dcf14b64784d7de6cf16e66e8c8b9eaf44272c0aaed20805ab255bfe1effc073c4f2aebb355f21339a
|
data/.github/workflows/ruby.yml
CHANGED
@@ -1,50 +1,39 @@
|
|
1
|
-
# This workflow uses actions that are not certified by GitHub.
|
2
|
-
# They are provided by a third-party and are governed by
|
3
|
-
# separate terms of service, privacy policy, and support
|
4
|
-
# documentation.
|
5
|
-
# This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
|
6
|
-
# For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
|
7
|
-
|
8
1
|
name: Ruby
|
9
2
|
|
10
3
|
on:
|
11
4
|
push:
|
12
|
-
branches:
|
13
|
-
|
14
|
-
branches: [ "master" ]
|
5
|
+
branches:
|
6
|
+
- master
|
15
7
|
|
16
|
-
|
17
|
-
|
8
|
+
pull_request:
|
9
|
+
workflow_call:
|
10
|
+
secrets:
|
11
|
+
QLTY_COVERAGE_TOKEN:
|
12
|
+
required: true
|
18
13
|
|
19
14
|
jobs:
|
20
|
-
|
21
|
-
|
15
|
+
build:
|
22
16
|
runs-on: ubuntu-latest
|
17
|
+
name: Ruby ${{ matrix.ruby }}
|
23
18
|
strategy:
|
24
19
|
matrix:
|
25
|
-
ruby
|
20
|
+
ruby:
|
21
|
+
- '3.4'
|
26
22
|
|
27
23
|
steps:
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
CC_TEST_REPORTER_ID: 3484f4290e34aa523ef280da3743fd5dc86a443fd7d8a1e5ba2339d1f63308d8
|
45
|
-
run: bundle exec rake
|
46
|
-
- name: Publish code coverage
|
47
|
-
run: |
|
48
|
-
export GIT_BRANCH="master"
|
49
|
-
./cc-test-reporter after-build -d -t simplecov -r 3484f4290e34aa523ef280da3743fd5dc86a443fd7d8a1e5ba2339d1f63308d8
|
50
|
-
|
24
|
+
- uses: actions/checkout@v4
|
25
|
+
with:
|
26
|
+
persist-credentials: false
|
27
|
+
- name: Set up Ruby
|
28
|
+
uses: ruby/setup-ruby@v1
|
29
|
+
with:
|
30
|
+
ruby-version: ${{ matrix.ruby }}
|
31
|
+
bundler-cache: true
|
32
|
+
- name: Run the default task
|
33
|
+
run: bundle exec rspec --require spec_helper
|
34
|
+
- name: Upload coverage to QLty
|
35
|
+
uses: qltysh/qlty-action/coverage@v2
|
36
|
+
with:
|
37
|
+
token: ${{ secrets.QLTY_COVERAGE_TOKEN }}
|
38
|
+
files: coverage/coverage.json
|
39
|
+
format: simplecov
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
easy_params (0.
|
4
|
+
easy_params (0.7.0)
|
5
5
|
activemodel (>= 3.2)
|
6
6
|
|
7
7
|
GEM
|
@@ -111,7 +111,7 @@ DEPENDENCIES
|
|
111
111
|
rspec (~> 3.0)
|
112
112
|
rspec_vars_helper (~> 0.1)
|
113
113
|
rubocop
|
114
|
-
simplecov
|
114
|
+
simplecov
|
115
115
|
|
116
116
|
BUNDLED WITH
|
117
117
|
2.7.1
|
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# EasyParams
|
2
2
|
|
3
|
+
[](https://qlty.sh/gh/andriy-baran/projects/easy_params)
|
4
|
+
[](https://qlty.sh/gh/andriy-baran/projects/easy_params)
|
3
5
|
[](https://badge.fury.io/rb/easy_params)
|
4
|
-
[](https://codeclimate.com/github/andriy-baran/easy_params/maintainability)
|
5
|
-
[](https://codeclimate.com/github/andriy-baran/easy_params/test_coverage)
|
6
6
|
|
7
7
|
Provides an easy way to define structure, validation rules, type coercion, and default values for any hash-like structure. It's built on top of `ActiveModel`.
|
8
8
|
|
data/lib/easy_params/base.rb
CHANGED
@@ -59,7 +59,7 @@ module EasyParams
|
|
59
59
|
|
60
60
|
def array(param_name, of:, default: nil, normalize: nil, **validations)
|
61
61
|
validates param_name, **validations if validations.any?
|
62
|
-
type = EasyParams::Types::Array.of(EasyParams
|
62
|
+
type = EasyParams::Types::Array.of(EasyParams.types[of])
|
63
63
|
type = customize_type(type, default, &normalize)
|
64
64
|
attribute(param_name, type)
|
65
65
|
end
|
@@ -68,6 +68,14 @@ module EasyParams
|
|
68
68
|
@schema = parent.schema.dup
|
69
69
|
end
|
70
70
|
|
71
|
+
def define_type_method(type_name)
|
72
|
+
define_singleton_method(type_name) do |param_name, default: nil, normalize: nil, **validations|
|
73
|
+
validates param_name, **validations if validations.any?
|
74
|
+
type = customize_type(EasyParams.types[type_name], default, &normalize)
|
75
|
+
attribute(param_name, type)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
71
79
|
private
|
72
80
|
|
73
81
|
def customize_type(type, default, &normalize)
|
@@ -77,16 +85,6 @@ module EasyParams
|
|
77
85
|
end
|
78
86
|
end
|
79
87
|
|
80
|
-
%w[Integer Decimal Float Bool String Date DateTime Time].each do |type_name|
|
81
|
-
send(:define_singleton_method,
|
82
|
-
type_name.underscore) do |param_name, default: nil, normalize: nil, **validations|
|
83
|
-
validates param_name, **validations if validations.any?
|
84
|
-
type = EasyParams::Types.const_get(type_name)
|
85
|
-
type = customize_type(type, default, &normalize)
|
86
|
-
attribute(param_name, type)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
88
|
def attributes
|
91
89
|
self.class.schema.to_h { |k, type| [k, type.array? ? send(k).to_a : send(k)] }
|
92
90
|
end
|
data/lib/easy_params/types.rb
CHANGED
@@ -2,37 +2,7 @@
|
|
2
2
|
|
3
3
|
module EasyParams
|
4
4
|
module Types
|
5
|
-
|
6
|
-
|
7
|
-
BOOLEAN_MAP =
|
8
|
-
{ '1' => true, 't' => true, 'true' => true, 'True' => true, 'TRUE' => true, 'T' => true }.merge(
|
9
|
-
{ '0' => false, 'f' => false, 'false' => false, 'False' => false, 'FALSE' => false, 'F' => false }
|
10
|
-
).freeze
|
11
|
-
|
12
|
-
Struct = EasyParams::Base.new
|
13
|
-
Array = Collection.new(:array)
|
14
|
-
Each = StructsCollection.new(:array_of_structs)
|
15
|
-
Integer = Generic.new(:integer, &:to_i)
|
16
|
-
Float = Generic.new(:float, &:to_f)
|
17
|
-
String = Generic.new(:string, &:to_s)
|
18
|
-
Decimal = Generic.new(:decimal) { |v| v.to_f.to_d }
|
19
|
-
Bool = Generic.new(:bool) do |v|
|
20
|
-
BOOLEAN_MAP.fetch(v.to_s) { raise CoercionError }
|
21
|
-
end
|
22
|
-
Date = Generic.new(:date) do |v|
|
23
|
-
::Date.parse(v)
|
24
|
-
rescue ArgumentError, RangeError
|
25
|
-
raise CoercionError, 'cannot be coerced'
|
26
|
-
end
|
27
|
-
DateTime = Generic.new(:datetime) do |v|
|
28
|
-
::DateTime.parse(v)
|
29
|
-
rescue ArgumentError
|
30
|
-
raise CoercionError, 'cannot be coerced'
|
31
|
-
end
|
32
|
-
Time = Generic.new(:time) do |v|
|
33
|
-
::Time.parse(v)
|
34
|
-
rescue ArgumentError
|
35
|
-
raise CoercionError, 'cannot be coerced'
|
36
|
-
end
|
5
|
+
Array = Collection.new(:array)
|
6
|
+
Each = StructsCollection.new(:array_of_structs)
|
37
7
|
end
|
38
8
|
end
|
data/lib/easy_params/version.rb
CHANGED
data/lib/easy_params.rb
CHANGED
@@ -10,7 +10,50 @@ require 'easy_params/base'
|
|
10
10
|
require 'easy_params/types'
|
11
11
|
require 'easy_params/version'
|
12
12
|
|
13
|
+
# EasyParams provides a simple way to define parameter classes with type coercion and validation
|
13
14
|
module EasyParams
|
14
15
|
class Error < StandardError; end
|
15
|
-
|
16
|
+
class CoercionError < StandardError; end
|
17
|
+
|
18
|
+
def self.types
|
19
|
+
@types ||= {}
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.register_type(name, type = nil, &coerce_proc)
|
23
|
+
if type.nil? && coerce_proc
|
24
|
+
type = Types::Generic.new(name, &coerce_proc)
|
25
|
+
elsif type.nil? && !coerce_proc
|
26
|
+
raise ArgumentError, 'Either a type instance or a coercion block must be provided'
|
27
|
+
end
|
28
|
+
types[name] = type
|
29
|
+
Base.define_type_method(name)
|
30
|
+
end
|
31
|
+
|
32
|
+
BOOLEAN_MAP =
|
33
|
+
{ '1' => true, 't' => true, 'true' => true, 'True' => true, 'TRUE' => true, 'T' => true }.merge(
|
34
|
+
{ '0' => false, 'f' => false, 'false' => false, 'False' => false, 'FALSE' => false, 'F' => false }
|
35
|
+
).freeze
|
36
|
+
|
37
|
+
register_type :integer, &:to_i
|
38
|
+
register_type :float, &:to_f
|
39
|
+
register_type :string, &:to_s
|
40
|
+
register_type(:decimal) { |v| v.to_f.to_d }
|
41
|
+
register_type(:bool) do |v|
|
42
|
+
BOOLEAN_MAP.fetch(v.to_s) { raise CoercionError }
|
43
|
+
end
|
44
|
+
register_type(:date) do |v|
|
45
|
+
::Date.parse(v)
|
46
|
+
rescue ArgumentError, RangeError
|
47
|
+
raise CoercionError, 'cannot be coerced'
|
48
|
+
end
|
49
|
+
register_type(:datetime) do |v|
|
50
|
+
::DateTime.parse(v)
|
51
|
+
rescue ArgumentError
|
52
|
+
raise CoercionError, 'cannot be coerced'
|
53
|
+
end
|
54
|
+
register_type(:time) do |v|
|
55
|
+
::Time.parse(v)
|
56
|
+
rescue ArgumentError
|
57
|
+
raise CoercionError, 'cannot be coerced'
|
58
|
+
end
|
16
59
|
end
|