smart_init 3.2.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81cc9ffbde7cec66828bcbb93ed817974a36c124b24ed41b375c15153c7cbb87
4
- data.tar.gz: 1c008ff8b237652378db6108e0c9e545b7b34663001a56f592955537f2bf826a
3
+ metadata.gz: 6348b5da71d03671ec74c8a43d2400e84076b4510fae3cb61f1db3da92752ba9
4
+ data.tar.gz: 95879658e1ff5b439206ad61c8472659d4331e058e1841d83bef56eaea3eb254
5
5
  SHA512:
6
- metadata.gz: 96a55247900e09af2ac195a5819467756cd9bccb302215399d7cc4e8d8d09d01cdb338c04a8e6e45a907b9ad7ca63c209d1696e5f1e7acddf9fa2fe86eefe120
7
- data.tar.gz: a2c82c78dc036fce298794963999d5327dab6b98d23f6eb778b79dd532dac2a5003065af0fd7fa44c4f188a52dd83ee15bbea6096d3f6c4ce4051ada60c08326
6
+ metadata.gz: 3239515e6cfee5cfc1a1be8b12d6b4799ede9e8ca020f4d7c8ee38d8ab6f2e61d80c9d1f86563c5302779f47d232dda0cd44db8a5164439522dfbd33abc450b2
7
+ data.tar.gz: 60abdec0ed7227d03bdc6d1c93d955202ea64ce31a6ade006a0b0c9a3f25c547bda222d0c8a349242789be5e8d1b70609f9a29ea11855cbb8b3981dce1d57d6c
data/README.md CHANGED
@@ -7,12 +7,18 @@ def initialize(network_provider, api_token)
7
7
  @network_provider = network_provider
8
8
  @api_token = api_token
9
9
  end
10
+
11
+ def self.call(network_provider, api_token)
12
+ new(network_provider, api_token).call
13
+ end
10
14
  ```
11
15
 
12
16
  Gem provides a simple DSL for getting rid of it. It offers an alternative to using `Struct.new` which does not check for number of parameters provided in initializer, exposes getters and instantiates unecessary class instances.
13
17
 
14
18
  **Smart Init** offers a unified api for stateless service objects, accepting values in initializer and exposing one public class method `call` which instantiates new objects and accepts arguments passed to initializer.
15
19
 
20
+ Check out [this blog post](https://pawelurbanek.com/2018/02/12/ruby-on-rails-service-objects-and-testing-in-isolation/) for my reasoning behind this approach to service object pattern.
21
+
16
22
  ## Installation
17
23
 
18
24
  In your Gemfile
@@ -74,6 +80,21 @@ end
74
80
  Calculator.call(data: data) => result
75
81
  ```
76
82
 
83
+ Optionally you can customize a callable method name:
84
+
85
+ ```ruby
86
+ class Routine < SmartInit::Base
87
+ initialize_with :params
88
+ is_callable method_name: :run!
89
+
90
+ def run!
91
+ ...
92
+ end
93
+ end
94
+
95
+ Routine.run!(params: params)
96
+ ```
97
+
77
98
  ### Default arguments
78
99
 
79
100
  You can use hash based, default argument values:
@@ -1,7 +1,13 @@
1
1
  module SmartInit
2
- def is_callable
3
- define_singleton_method :call do |*parameters|
4
- new(*parameters).call
2
+ def is_callable(opts={})
3
+ method_name = if name_from_opts = opts[:method_name]
4
+ name_from_opts.to_sym
5
+ else
6
+ :call
7
+ end
8
+
9
+ define_singleton_method method_name do |*parameters|
10
+ new(*parameters).public_send(method_name)
5
11
  end
6
12
  end
7
13
 
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "3.2.0"
2
+ VERSION = "3.3.0"
3
3
  end
@@ -20,6 +20,16 @@ class TestNoInit
20
20
  end
21
21
  end
22
22
 
23
+ class TestMethodName
24
+ extend SmartInit
25
+
26
+ is_callable method_name: :run!
27
+
28
+ def run!
29
+ true
30
+ end
31
+ end
32
+
23
33
  def test_object
24
34
  @_test_object ||= TestClass.new("attr_1_value", "attr_2_value")
25
35
  end
@@ -59,4 +69,8 @@ class StandardApiTest < Test::Unit::TestCase
59
69
  def test_is_callable_no_initializers
60
70
  assert_equal TestNoInit.call, 'result'
61
71
  end
72
+
73
+ def test_is_callable_method_name
74
+ assert_equal TestMethodName.run!, true
75
+ end
62
76
  end
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: 3.2.0
4
+ version: 3.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - pawurb
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-01-01 00:00:00.000000000 Z
11
+ date: 2019-01-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake