smart_init 5.0.2 → 5.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c940aeebc57ccfda08563113bada6dfb76719ab15de983801edeb532df79c022
4
- data.tar.gz: ab037fe33f3536b862c66e9b66b3c372f96648e4d69bb4d542bc9533c5a22fe2
3
+ metadata.gz: 051f72ec1eae040e05e230adce67fbb4ea8054833f5b517eb558b5c670258c15
4
+ data.tar.gz: 77b36f4ddd50e13701e0e6e8c32cc2242ac4042d1de8a1c6eaf34e8d06f36d17
5
5
  SHA512:
6
- metadata.gz: 5afb3dd23c4b4ee6bbcf435e1708f1bc82799d4d918d589bb96397b71423f2338888a20bdf236e352acd76c4d82c2edf84f002830358e9f6b1d9faad8985d0c4
7
- data.tar.gz: 276f69b1ec9b740f700e13c4153b68a377ad1f44bb35ce8efe33046dd9657c20c4ad50d6d90ce995df74597a6d590a6600f6fe021e8344bacb13cad747941600
6
+ metadata.gz: e1242877f56883a4be235b589c30f5116e021eaca1b01bf7af948582806ee95c9f42479e93dbfdaebe344a2d4bbe3d663946aaf8f826b5a786c720334dbf690e
7
+ data.tar.gz: 67c6ecd2b1cce330c1854f5b52d06cd64f79d17142ab2393841aa32e7525b58b6a99bc351ddc71ee6c8754b29457b01a7504d58e96e714a894aa20017eb66af4
@@ -0,0 +1,30 @@
1
+ name: Ruby CI
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ test:
11
+ runs-on: ubuntu-latest
12
+ strategy:
13
+ fail-fast: false
14
+ matrix:
15
+ ruby-version: ['3.4', '3.3', '3.2', '3.1', '3.0', '2.7']
16
+ steps:
17
+ - uses: actions/checkout@v3
18
+ - name: Set up Ruby ${{ matrix.ruby-version }}
19
+ uses: ruby/setup-ruby@v1
20
+ with:
21
+ ruby-version: ${{ matrix.ruby-version }}
22
+ - name: Setup dependencies
23
+ run: |
24
+ gem install bundler -v 2.4.22
25
+ sudo apt-get update --allow-releaseinfo-change
26
+ bundle config set --local path 'vendor/bundle'
27
+ bundle install
28
+ - name: Run tests
29
+ run: |
30
+ bundle exec rake test
data/Gemfile CHANGED
@@ -1,3 +1,3 @@
1
- source 'https://rubygems.org'
1
+ source "https://rubygems.org"
2
2
 
3
3
  gemspec
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Smart Init - Simple service objects in Ruby [![Build Status](https://travis-ci.org/pawurb/smart_init.svg)](https://travis-ci.org/pawurb/smart_init) [![Gem Version](https://badge.fury.io/rb/smart_init.svg)](https://badge.fury.io/rb/smart_init)
1
+ # Smart Init - Simple service objects in Ruby [![Gem Version](https://badge.fury.io/rb/smart_init.svg)](https://badge.fury.io/rb/smart_init) [![GH Actions](https://github.com/pawurb/smart_init/actions/workflows/ci.yml/badge.svg)](https://github.com/pawurb/smart_init/actions)
2
2
 
3
3
  Do you find yourself writing a lot of boilerplate code like this?
4
4
 
data/Rakefile CHANGED
@@ -1,8 +1,8 @@
1
- require 'bundler/gem_tasks'
2
- require 'rake/testtask'
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
3
 
4
4
  Rake::TestTask.new do |t|
5
- t.libs << 'test'
5
+ t.libs << "test"
6
6
  end
7
7
 
8
8
  desc "Run tests"
@@ -1,24 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartInit
4
- def is_callable(opts={})
4
+ def is_callable(opts = {})
5
5
  method_name = if name_from_opts = opts[:method_name]
6
- name_from_opts.to_sym
7
- else
8
- :call
9
- end
6
+ name_from_opts.to_sym
7
+ else
8
+ :call
9
+ end
10
10
 
11
11
  define_singleton_method method_name do |**parameters|
12
12
  new(**parameters).public_send(method_name)
13
13
  end
14
14
  end
15
15
 
16
- def initialize_with_hash(*required_attrs, **attributes_and_options)
17
- public_readers = attributes_and_options.delete(:public_readers) || []
18
- public_accessors = attributes_and_options.delete(:public_accessors) || []
19
- if public_readers == true || public_accessors == true
20
- public_readers = required_attrs + attributes_and_options.keys
21
- public_accessors = required_attrs + attributes_and_options.keys if public_accessors == true
16
+ def initialize_with_hash(*required_attrs, **default_values_and_opts)
17
+ public_readers = default_values_and_opts.delete(:public_readers) || []
18
+ public_accessors = default_values_and_opts.delete(:public_accessors) || []
19
+ if public_readers == true || public_accessors == true
20
+ public_readers = required_attrs + default_values_and_opts.keys
21
+ public_accessors = required_attrs + default_values_and_opts.keys if public_accessors == true
22
22
  else
23
23
  public_readers += public_accessors
24
24
  end
@@ -29,19 +29,26 @@ module SmartInit
29
29
  raise ArgumentError, "missing required attribute #{required_attr}"
30
30
  end
31
31
  end
32
- (required_attrs + attributes_and_options.keys).each do |attribute|
33
- value = if parameters.has_key?(attribute)
34
- parameters.fetch(attribute)
35
- else
36
- attributes_and_options[attribute]
32
+
33
+ parameters.keys.each do |param|
34
+ if !(required_attrs + [:public_readers, :public_accessors]).include?(param) && !default_values_and_opts.keys.include?(param)
35
+ raise ArgumentError, "invalid attribute '#{param}'"
37
36
  end
37
+ end
38
+
39
+ (required_attrs + default_values_and_opts.keys).each do |attribute|
40
+ value = if parameters.has_key?(attribute)
41
+ parameters.fetch(attribute)
42
+ else
43
+ default_values_and_opts[attribute]
44
+ end
38
45
 
39
46
  instance_variable_set("@#{attribute}", value)
40
47
  end
41
48
  end
42
49
 
43
50
  instance_eval do
44
- all_readers = (required_attrs + attributes_and_options.keys)
51
+ all_readers = (required_attrs + default_values_and_opts.keys)
45
52
  attr_reader(*all_readers)
46
53
  (all_readers - public_readers).each do |reader|
47
54
  private reader
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SmartInit
4
- VERSION = "5.0.2"
4
+ VERSION = "5.1.0"
5
5
  end
data/lib/smart_init.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'smart_init/main'
3
+ require "smart_init/main"
4
4
 
5
5
  module SmartInit
6
6
  end
data/smart_init.gemspec CHANGED
@@ -1,23 +1,25 @@
1
1
  # -*- encoding: utf-8 -*-
2
- lib = File.expand_path('../lib', __FILE__)
2
+ lib = File.expand_path("../lib", __FILE__)
3
3
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require 'smart_init/version'
4
+ require "smart_init/version"
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = "smart_init"
8
- s.version = SmartInit::VERSION
9
- s.authors = ["pawurb"]
10
- s.email = ["p.urbanek89@gmail.com"]
11
- s.summary = %q{ Remove Ruby initializer boilerplate code }
12
- s.description = %q{ A smart DSL for ruby initializers boilerplate }
13
- s.homepage = "http://github.com/pawurb/smart_init"
14
- s.files = `git ls-files`.split("\n")
15
- s.test_files = s.files.grep(%r{^(test)/})
7
+ s.name = "smart_init"
8
+ s.version = SmartInit::VERSION
9
+ s.authors = ["pawurb"]
10
+ s.email = ["p.urbanek89@gmail.com"]
11
+ s.summary = %q{ Remove Ruby initializer boilerplate code }
12
+ s.description = %q{ A smart DSL for ruby initializers boilerplate }
13
+ s.homepage = "http://github.com/pawurb/smart_init"
14
+ s.files = `git ls-files`.split("\n")
15
+ s.test_files = s.files.grep(%r{^(test)/})
16
16
  s.require_paths = ["lib"]
17
- s.license = "MIT"
17
+ s.license = "MIT"
18
18
  s.add_development_dependency "rake"
19
19
  s.add_development_dependency "byebug"
20
+ s.add_development_dependency "dbg-rb"
20
21
  s.add_development_dependency "test-unit"
22
+ s.add_development_dependency "rufo"
21
23
 
22
24
  if s.respond_to?(:metadata=)
23
25
  s.metadata = { "rubygems_mfa_required" => "true" }
@@ -1,7 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "test/unit"
4
- require_relative '../lib/smart_init/main'
4
+ require_relative "../lib/smart_init/main"
5
5
 
6
6
  class TestService
7
7
  extend SmartInit
@@ -49,7 +49,7 @@ class HashApiTest < Test::Unit::TestCase
49
49
 
50
50
  assert_raise ArgumentError do
51
51
  TestService.new(
52
- attribute_1: "a"
52
+ attribute_1: "a",
53
53
  )
54
54
  end
55
55
  end
@@ -91,4 +91,10 @@ class HashApiTest < Test::Unit::TestCase
91
91
  def test_falsey_values
92
92
  assert_equal TestService.call(attribute_1: false, attribute_2: nil), [false, nil]
93
93
  end
94
+
95
+ def test_invalid_keywords
96
+ assert_raise ArgumentError do
97
+ TestService.call(attribute_1: "a", attribute_2: "b", invalid_attribute: "c")
98
+ end
99
+ end
94
100
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "byebug"
4
3
  require "test/unit"
5
- require_relative '../lib/smart_init/main'
4
+ require_relative "../lib/smart_init/main"
6
5
 
7
6
  class TestAllPublicAccessors
8
7
  extend SmartInit
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "byebug"
4
3
  require "test/unit"
5
- require_relative '../lib/smart_init/main'
4
+ require_relative "../lib/smart_init/main"
6
5
 
7
6
  class TestSomePublicMixed
8
7
  extend SmartInit
@@ -26,12 +25,11 @@ class TestAllReadersSomeAccessorsPublic
26
25
  end
27
26
 
28
27
  class HashApiPublicTest < Test::Unit::TestCase
29
-
30
28
  def test_readers_some_public_mixed
31
29
  service = TestSomePublicMixed.new(
32
- attribute_1: "a", attribute_2: "b",
33
- attribute_3: "c", attribute_4: "d"
34
- )
30
+ attribute_1: "a", attribute_2: "b",
31
+ attribute_3: "c", attribute_4: "d",
32
+ )
35
33
  assert_nothing_raised do
36
34
  service.attribute_1
37
35
  service.attribute_2
@@ -44,9 +42,9 @@ class HashApiPublicTest < Test::Unit::TestCase
44
42
 
45
43
  def test_writers_some_public_mixed
46
44
  service = TestSomePublicMixed.new(
47
- attribute_1: "a", attribute_2: "b",
48
- attribute_3: "c", attribute_4: "d"
49
- )
45
+ attribute_1: "a", attribute_2: "b",
46
+ attribute_3: "c", attribute_4: "d",
47
+ )
50
48
  assert_nothing_raised do
51
49
  service.attribute_2 = "e"
52
50
  service.attribute_3 = "f"
@@ -60,8 +58,8 @@ class HashApiPublicTest < Test::Unit::TestCase
60
58
 
61
59
  def test_readers_all_readers_some_accessors_public
62
60
  service = TestAllReadersSomeAccessorsPublic.new(
63
- attribute_1: "a", attribute_2: "b"
64
- )
61
+ attribute_1: "a", attribute_2: "b",
62
+ )
65
63
  assert_nothing_raised do
66
64
  service.attribute_1
67
65
  service.attribute_2
@@ -70,8 +68,8 @@ class HashApiPublicTest < Test::Unit::TestCase
70
68
 
71
69
  def test_writers_all_readers_some_accessors_public
72
70
  service = TestAllReadersSomeAccessorsPublic.new(
73
- attribute_1: "a", attribute_2: "b"
74
- )
71
+ attribute_1: "a", attribute_2: "b",
72
+ )
75
73
  assert_raise NoMethodError do
76
74
  service.attribute_1 = "c"
77
75
  end
@@ -1,8 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "byebug"
4
3
  require "test/unit"
5
- require_relative '../lib/smart_init/main'
4
+ require_relative "../lib/smart_init/main"
6
5
 
7
6
  class TestAllPublic
8
7
  extend SmartInit
@@ -72,4 +71,3 @@ class HashApiPublicTest < Test::Unit::TestCase
72
71
  assert_equal service.attribute_2, 2
73
72
  end
74
73
  end
75
-
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_init
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.2
4
+ version: 5.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-02-21 00:00:00.000000000 Z
11
+ date: 2025-05-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: dbg-rb
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'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: test-unit
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -52,6 +66,20 @@ dependencies:
52
66
  - - ">="
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rufo
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
55
83
  description: " A smart DSL for ruby initializers boilerplate "
56
84
  email:
57
85
  - p.urbanek89@gmail.com
@@ -59,8 +87,8 @@ executables: []
59
87
  extensions: []
60
88
  extra_rdoc_files: []
61
89
  files:
90
+ - ".github/workflows/ci.yml"
62
91
  - ".gitignore"
63
- - ".travis.yml"
64
92
  - Gemfile
65
93
  - LICENSE.txt
66
94
  - README.md
@@ -78,7 +106,7 @@ licenses:
78
106
  - MIT
79
107
  metadata:
80
108
  rubygems_mfa_required: 'true'
81
- post_install_message:
109
+ post_install_message:
82
110
  rdoc_options: []
83
111
  require_paths:
84
112
  - lib
@@ -93,8 +121,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
121
  - !ruby/object:Gem::Version
94
122
  version: '0'
95
123
  requirements: []
96
- rubygems_version: 3.1.6
97
- signing_key:
124
+ rubygems_version: 3.5.16
125
+ signing_key:
98
126
  specification_version: 4
99
127
  summary: Remove Ruby initializer boilerplate code
100
128
  test_files:
data/.travis.yml DELETED
@@ -1,7 +0,0 @@
1
- rvm:
2
- - 2.5.3
3
- - 2.6.5
4
- - 2.7.0
5
- notifications:
6
- email: false
7
-