smart_init 0.1.0 → 1.0.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/README.md +57 -3
- data/lib/smart_init/main.rb +9 -0
- data/lib/smart_init/version.rb +1 -1
- data/test/test_smart_init.rb +9 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7394612537ae9d948b6a8e18a6ef14ae60e0cdee
|
4
|
+
data.tar.gz: fc5bc846777c3495a1ce1ead2fc0e9ee0758dd0a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5319b8f52c4e52aee3916bf280db7484616a284488f14f4e0a3cd8014b02c858e3f646da81b89fe3ae009f14a6ab2ec2bd273b4fddad3a275b32b52b9e80a4c8
|
7
|
+
data.tar.gz: 8694b7aebfd30e3750e4be7365f71c4dc34dd954a949a0cd6dd694acc1e843b9696e3ecb5979bd63cecb4d46bb093b8d2206a9a79f63f6dc72272041c9e5c4b5
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
#
|
1
|
+
# Smart Init [](https://travis-ci.org/pawurb/smart_init) [](http://badge.fury.io/rb/smart_init)
|
2
2
|
|
3
|
-
Do you find yourself writing a lot of boilerplate code like
|
3
|
+
Do you find yourself writing a lot of boilerplate code like that?
|
4
4
|
|
5
5
|
``` ruby
|
6
6
|
def initialize(network_provider, api_token)
|
@@ -9,5 +9,59 @@ def initialize(network_provider, api_token)
|
|
9
9
|
end
|
10
10
|
```
|
11
11
|
|
12
|
-
|
12
|
+
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
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
In your Gemfile
|
17
|
+
|
18
|
+
```ruby
|
19
|
+
gem 'smart_init'
|
20
|
+
```
|
21
|
+
|
22
|
+
Then you can use it either by extending a module:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
class ApiClient
|
26
|
+
extend SmartInit
|
27
|
+
|
28
|
+
initialize_with :network_provider, :api_token
|
29
|
+
end
|
30
|
+
|
31
|
+
```
|
32
|
+
|
33
|
+
or subclassing:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
class ApiClient < SmartInit::Base
|
37
|
+
initialize_with :network_provider, :api_token
|
38
|
+
end
|
39
|
+
|
40
|
+
```
|
41
|
+
|
42
|
+
Now you can just:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
object = ApiClient.new(Faraday.new, 'secret_token')
|
46
|
+
# <ApiClient:0x007fa16684ec20 @network_provider=Faraday<...>, @api_token="secret_token">
|
47
|
+
```
|
48
|
+
|
49
|
+
You can also use `is_callable` method:
|
50
|
+
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
class Calculator < SmartInit::Base
|
54
|
+
initialize_with :data
|
55
|
+
is_callable
|
56
|
+
|
57
|
+
def call
|
58
|
+
...
|
59
|
+
result
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
Calculator.call(data) => data
|
64
|
+
```
|
65
|
+
|
66
|
+
It provides a unified api for stateless service objects, accepting values in initializer and exposing one public method `call` which accepts no arguments.
|
13
67
|
|
data/lib/smart_init/main.rb
CHANGED
@@ -1,5 +1,14 @@
|
|
1
1
|
module SmartInit
|
2
|
+
@@_init_attributes = []
|
3
|
+
|
4
|
+
def is_callable
|
5
|
+
define_singleton_method :call do |*parameters|
|
6
|
+
new(*parameters).call
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
2
10
|
def initialize_with *attributes
|
11
|
+
@_init_attributes = attributes
|
3
12
|
define_method :initialize do |*parameters|
|
4
13
|
if attributes.count != parameters.count
|
5
14
|
raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
|
data/lib/smart_init/version.rb
CHANGED
data/test/test_smart_init.rb
CHANGED
@@ -4,6 +4,11 @@ require 'smart_init'
|
|
4
4
|
class TestClass
|
5
5
|
extend SmartInit
|
6
6
|
initialize_with :attribute_1, :attribute_2
|
7
|
+
is_callable
|
8
|
+
|
9
|
+
def call
|
10
|
+
[attribute_1, attribute_2]
|
11
|
+
end
|
7
12
|
end
|
8
13
|
|
9
14
|
class SmartInitTest < Test::Unit::TestCase
|
@@ -34,6 +39,10 @@ class SmartInitTest < Test::Unit::TestCase
|
|
34
39
|
assert_equal test_object.send(:attribute_1), "attr_1_value"
|
35
40
|
end
|
36
41
|
|
42
|
+
def test_is_callable
|
43
|
+
assert_equal TestClass.call("a", "b"), ["a", "b"]
|
44
|
+
end
|
45
|
+
|
37
46
|
private
|
38
47
|
|
39
48
|
def test_object
|
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:
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- pawurb
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|