easy_params 0.6.2 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 23259cc1e65725f6e9665a3ad772daaef176cd6f4e19ef19da024e967a081d46
4
- data.tar.gz: 655a86c2642f3687157f616cdb535317af993ed95ac853e5acd614a769c31ba0
3
+ metadata.gz: f63905ee9e223d63e2f3855f7e67ace0f3d2abd14ed97fb1f706f73082f73c90
4
+ data.tar.gz: 1ce09ad5d8a018e97c9c46eb21b32f5dd537d448830578c8b760ee2c99d903ca
5
5
  SHA512:
6
- metadata.gz: 82c370a09789747b80297d76f8315e3eb44531c613fe7f36b8b166316a7bdafbfa9554c1ca8eff24bb335b495373330e14eae63c5930cc0b4ea806bf17f74945
7
- data.tar.gz: 4f8494ba8a3f7c9fc610593afa4a3bc7115d239779d7deecc1d4b2f9763a68ea83baa2c81d36577c98a08ef0544c7b29b73ce7e717d55ec5ab7fc2ef2aa6f7d0
6
+ metadata.gz: 935a110dbedbace4f05a9df987aec2932fec9ccec5cbc9dbb3b4a3460b42971cd7cdafa60c391b24ad334f74e39086c7d81df191e9622844ebda0f4ed9ca952f
7
+ data.tar.gz: 9ebe7ebeae1961a820e396361e020b5b8cfe712ad81480dcf14b64784d7de6cf16e66e8c8b9eaf44272c0aaed20805ab255bfe1effc073c4f2aebb355f21339a
@@ -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: [ "master" ]
13
- pull_request:
14
- branches: [ "master" ]
5
+ branches:
6
+ - master
15
7
 
16
- permissions:
17
- contents: read
8
+ pull_request:
9
+ workflow_call:
10
+ secrets:
11
+ QLTY_COVERAGE_TOKEN:
12
+ required: true
18
13
 
19
14
  jobs:
20
- test:
21
-
15
+ build:
22
16
  runs-on: ubuntu-latest
17
+ name: Ruby ${{ matrix.ruby }}
23
18
  strategy:
24
19
  matrix:
25
- ruby-version: ['2.7', '3.0']
20
+ ruby:
21
+ - '3.4'
26
22
 
27
23
  steps:
28
- - uses: actions/checkout@v3
29
- - name: Set up Ruby
30
- # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
- # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
- # uses: ruby/setup-ruby@v1
33
- uses: ruby/setup-ruby@55283cc23133118229fd3f97f9336ee23a179fcf # v1.146.0
34
- with:
35
- ruby-version: ${{ matrix.ruby-version }}
36
- bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
- - name: Setup Code Climate test-reporter
38
- run: |
39
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
40
- chmod +x ./cc-test-reporter
41
- ./cc-test-reporter before-build
42
- - name: Run the default task
43
- env:
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
@@ -11,4 +11,4 @@ gem 'rake', '~> 12.0'
11
11
  gem 'rspec', '~> 3.0'
12
12
  gem 'rspec_vars_helper', '~> 0.1'
13
13
  gem 'rubocop'
14
- gem 'simplecov', '~> 0.17'
14
+ gem 'simplecov', require: false
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_params (0.6.2)
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 (~> 0.17)
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
+ [![Maintainability](https://qlty.sh/gh/andriy-baran/projects/easy_params/maintainability.svg)](https://qlty.sh/gh/andriy-baran/projects/easy_params)
4
+ [![Code Coverage](https://qlty.sh/gh/andriy-baran/projects/easy_params/coverage.svg)](https://qlty.sh/gh/andriy-baran/projects/easy_params)
3
5
  [![Gem Version](https://badge.fury.io/rb/easy_params.svg)](https://badge.fury.io/rb/easy_params)
4
- [![Maintainability](https://api.codeclimate.com/v1/badges/17872804ce576b8b0df2/maintainability)](https://codeclimate.com/github/andriy-baran/easy_params/maintainability)
5
- [![Test Coverage](https://api.codeclimate.com/v1/badges/17872804ce576b8b0df2/test_coverage)](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
 
@@ -16,6 +16,11 @@ module EasyParams
16
16
  end
17
17
 
18
18
  class << self
19
+ def inherited(subclass)
20
+ super
21
+ subclass.clone_schema(self)
22
+ end
23
+
19
24
  def name
20
25
  'EasyParams::Base'
21
26
  end
@@ -54,11 +59,23 @@ module EasyParams
54
59
 
55
60
  def array(param_name, of:, default: nil, normalize: nil, **validations)
56
61
  validates param_name, **validations if validations.any?
57
- type = EasyParams::Types::Array.of(EasyParams::Types.const_get(of.to_s.camelcase))
62
+ type = EasyParams::Types::Array.of(EasyParams.types[of])
58
63
  type = customize_type(type, default, &normalize)
59
64
  attribute(param_name, type)
60
65
  end
61
66
 
67
+ def clone_schema(parent)
68
+ @schema = parent.schema.dup
69
+ end
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
+
62
79
  private
63
80
 
64
81
  def customize_type(type, default, &normalize)
@@ -68,16 +85,6 @@ module EasyParams
68
85
  end
69
86
  end
70
87
 
71
- %w[Integer Decimal Float Bool String Date DateTime Time].each do |type_name|
72
- send(:define_singleton_method,
73
- type_name.underscore) do |param_name, default: nil, normalize: nil, **validations|
74
- validates param_name, **validations if validations.any?
75
- type = EasyParams::Types.const_get(type_name)
76
- type = customize_type(type, default, &normalize)
77
- attribute(param_name, type)
78
- end
79
- end
80
-
81
88
  def attributes
82
89
  self.class.schema.to_h { |k, type| [k, type.array? ? send(k).to_a : send(k)] }
83
90
  end
@@ -2,37 +2,7 @@
2
2
 
3
3
  module EasyParams
4
4
  module Types
5
- class CoercionError < StandardError; end
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EasyParams
4
- VERSION = '0.6.2'
4
+ VERSION = '0.7.0'
5
5
  end
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
- # Your code goes here...
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.2
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrii Baran