smart_init 0.1.0 → 1.0.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
  SHA1:
3
- metadata.gz: b7dc87097a9f29e7ec98b46da93f31a0f97b3f78
4
- data.tar.gz: 3607b757069ec5da39b8dfadee1f3867bbcead68
3
+ metadata.gz: 7394612537ae9d948b6a8e18a6ef14ae60e0cdee
4
+ data.tar.gz: fc5bc846777c3495a1ce1ead2fc0e9ee0758dd0a
5
5
  SHA512:
6
- metadata.gz: 37c4bf26508473b50275d6923c8c735cbce63d9680151cc1b47ec33d858c9cf053a4898be359633113fd2f0a77c3223c232a572ddb985aa5fb4f8a74d3766e2e
7
- data.tar.gz: 233481aee605bee113c8391d7bdf925d96ddd33d0cc4abaceb49f1c2c099fb43ec13bedba3f108c40b0428f2357ff2a558a1839b99214bd8cf29fae9e2732d4a
6
+ metadata.gz: 5319b8f52c4e52aee3916bf280db7484616a284488f14f4e0a3cd8014b02c858e3f646da81b89fe3ae009f14a6ab2ec2bd273b4fddad3a275b32b52b9e80a4c8
7
+ data.tar.gz: 8694b7aebfd30e3750e4be7365f71c4dc34dd954a949a0cd6dd694acc1e843b9696e3ecb5979bd63cecb4d46bb093b8d2206a9a79f63f6dc72272041c9e5c4b5
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # SmartInit [![Build Status](https://travis-ci.org/pawurb/smart_init.svg)](https://travis-ci.org/pawurb/smart_init)
1
+ # Smart Init [![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)](http://badge.fury.io/rb/smart_init)
2
2
 
3
- Do you find yourself writing a lot of boilerplate code like this?
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
- WIP...
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
 
@@ -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})"
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "0.1.0"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -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: 0.1.0
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-05-04 00:00:00.000000000 Z
11
+ date: 2017-10-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake