smart_init 2.0.3 → 3.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
- SHA1:
3
- metadata.gz: 6056d4cf08e55061d4b7cb406fa3d61107d7703e
4
- data.tar.gz: ff3853b9c773b1115503641aabd4bd93d46f3382
2
+ SHA256:
3
+ metadata.gz: 8dee28a188d21cf07e256ca0fd7ef9be6200e7397226b4b4e26b7f995b94acf5
4
+ data.tar.gz: fef3f5bad20e6cf3df03dd27a69df4aa429d304098b1acc60426a00c0a8eafd0
5
5
  SHA512:
6
- metadata.gz: fda5e0d95ed0b7282a4ba94ebbcc32ecf61c09e72949424372cd4a355ea92b32da05a27fb5d2a17c272bd9e63240c793e5f5031f97a23e872728fe043c974c73
7
- data.tar.gz: ce961616f650be88b47a8e4b0fcc074e91e17c469e8a3722ae265fae8929642e30096e9af21d0cecccd0d0f94a1c710fd6333b9ed0fba369aa5a7b070e660b36
6
+ metadata.gz: d47fbddd944636439c176a32d5d62871669d5c9ffb505f7b6180e28bc52ac7f91bb9e7699c14378e89b811671ef3626f888ea84916e17ccde3daff96538e1255
7
+ data.tar.gz: 32751fb0cda8bc3713f57da5368ae8dde9dbf90ab7b95a1671b61eec17ed69de249d58fa116e4127260291bdeb34c62e962a690a9ee2970929bf1a7733376332
data/.travis.yml CHANGED
@@ -1,8 +1,7 @@
1
1
  rvm:
2
- - 2.2.5
3
- - 2.3.0
4
- - 2.3.1
5
- - 2.4.0
2
+ - 2.4.4
3
+ - 2.5.3
4
+ - 2.6.0
6
5
  notifications:
7
6
  email: false
8
7
 
data/README.md CHANGED
@@ -21,7 +21,7 @@ In your Gemfile
21
21
  gem 'smart_init'
22
22
  ```
23
23
 
24
- ## Keyword arguments API
24
+ ## API
25
25
 
26
26
  You can use it either by extending a module:
27
27
 
@@ -29,7 +29,7 @@ You can use it either by extending a module:
29
29
  class ApiClient
30
30
  extend SmartInit
31
31
 
32
- initialize_with :network_provider, api_token: "default_token"
32
+ initialize_with :network_provider, :api_token
33
33
  end
34
34
 
35
35
  ```
@@ -38,7 +38,7 @@ or subclassing:
38
38
 
39
39
  ```ruby
40
40
  class ApiClient < SmartInit::Base
41
- initialize_with :network_provider, api_token: "default_token"
41
+ initialize_with :network_provider, :api_token
42
42
  end
43
43
 
44
44
  ```
@@ -52,7 +52,6 @@ object = ApiClient.new(network_provider: Faraday.new, api_token: 'secret_token')
52
52
 
53
53
  You can also use `is_callable` method:
54
54
 
55
-
56
55
  ```ruby
57
56
  class Calculator < SmartInit::Base
58
57
  initialize_with :data
@@ -69,10 +68,10 @@ Calculator.call(data: data) => result
69
68
 
70
69
  ### Default arguments
71
70
 
72
- You can use keyword based, default argument values:
71
+ You can use hash based, default argument values:
73
72
 
74
73
  ```ruby
75
- class Added < SmartInit::Base
74
+ class Adder < SmartInit::Base
76
75
  initialize_with :num_a, num_b: 2
77
76
  is_callable
78
77
 
@@ -88,11 +87,11 @@ Adder.call(num_a: 2, num_b: 3) => 5
88
87
 
89
88
  ## Legacy API
90
89
 
91
- Alternatively you can use standard API without keyword arguments:
90
+ Alternatively you can a legacy API without hash arguments and default values:
92
91
 
93
92
  ```ruby
94
93
  class Calculator < SmartInit::Base
95
- initialize_with :data
94
+ initialize_with_v1 :data
96
95
  is_callable
97
96
 
98
97
  def call
@@ -103,6 +102,3 @@ end
103
102
 
104
103
  Calculator.call(data) => result
105
104
  ```
106
-
107
- It does not support default argument values though.
108
-
@@ -6,61 +6,47 @@ module SmartInit
6
6
  end
7
7
 
8
8
  def initialize_with *attributes
9
+ required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
10
+ default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
11
+
9
12
  define_method :initialize do |*parameters|
10
- if attributes.count != parameters.count
11
- raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
13
+ if required_attrs.count > parameters.first.count
14
+ raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected at least #{required_attrs.count})"
12
15
  end
13
16
 
14
- attributes.zip(parameters).each do |pair|
15
- name = pair[0]
16
- value = pair[1]
17
- instance_variable_set("@#{name}", value)
17
+ (required_attrs + default_value_attrs.keys).each do |attribute|
18
+ value = parameters.first[attribute] || default_value_attrs[attribute]
19
+ instance_variable_set("@#{attribute}", value)
18
20
  end
19
21
  end
20
22
 
21
23
  instance_eval do
22
24
  private
23
25
 
24
- attr_reader *attributes
26
+ attr_reader *(required_attrs + default_value_attrs.keys)
25
27
  end
26
28
  end
27
29
 
28
- def initialize_with_keywords *attributes
29
- class_variable_set(:@@_attributes, attributes)
30
- @@_attributes = attributes
31
-
32
- required_attrs = attributes.select { |attr| attr.is_a?(Symbol) }
33
- default_value_attrs = attributes.select { |attr| attr.is_a?(Hash) }.first || {}
34
-
35
- class_variable_set(:@@_required_attrs, required_attrs)
36
- class_variable_set(:@@_default_value_attrs, default_value_attrs)
37
- @@_required_attrs = required_attrs
38
- @@_default_value_attrs = default_value_attrs
39
-
40
- class_eval <<-METHOD
41
- def initialize(#{(@@_required_attrs + @@_default_value_attrs.keys).compact.map { |a| @@_default_value_attrs[a] ? "#{a.to_s}: '#{@@_default_value_attrs.fetch(a)}'" : "#{a}:" }.join(', ')})
42
- @@_required_attrs.each do |attribute|
43
- instance_variable_set(
44
- "@"+ attribute.to_s,
45
- eval(attribute.to_s)
46
- )
47
- end
30
+ def initialize_with_v1 *attributes
31
+ define_method :initialize do |*parameters|
32
+ if attributes.count != parameters.count
33
+ raise ArgumentError, "wrong number of arguments (given #{parameters.count}, expected #{attributes.count})"
34
+ end
48
35
 
49
- @@_default_value_attrs.keys.each do |attribute|
50
- instance_variable_set(
51
- "@"+ attribute.to_s,
52
- eval(attribute.to_s)
53
- )
54
- end
36
+ attributes.zip(parameters).each do |pair|
37
+ name = pair[0]
38
+ value = pair[1]
39
+ instance_variable_set("@#{name}", value)
55
40
  end
56
- METHOD
41
+ end
57
42
 
58
43
  instance_eval do
59
44
  private
60
45
 
61
- attr_reader *(required_attrs + default_value_attrs.keys)
46
+ attr_reader *attributes
62
47
  end
63
48
  end
49
+
64
50
  end
65
51
 
66
52
  class SmartInit::Base
@@ -1,3 +1,3 @@
1
1
  module SmartInit
2
- VERSION = "2.0.3"
2
+ VERSION = "3.0.0"
3
3
  end
@@ -3,7 +3,7 @@ require_relative '../lib/smart_init/main'
3
3
 
4
4
  class TestKeywords
5
5
  extend SmartInit
6
- initialize_with_keywords :attribute_1, :attribute_2
6
+ initialize_with :attribute_1, :attribute_2
7
7
  is_callable
8
8
 
9
9
  def call
@@ -13,7 +13,7 @@ end
13
13
 
14
14
  class TestKeywordsDefaults
15
15
  extend SmartInit
16
- initialize_with_keywords :attribute_1, attribute_2: "default_value_2", attribute_3: "default_value_3"
16
+ initialize_with :attribute_1, attribute_2: "default_value_2", attribute_3: "default_value_3"
17
17
  is_callable
18
18
 
19
19
  def call
@@ -21,6 +21,16 @@ class TestKeywordsDefaults
21
21
  end
22
22
  end
23
23
 
24
+ class TestKeywordsIntegerDefaults
25
+ extend SmartInit
26
+ initialize_with :attribute_1, attribute_2: 2
27
+ is_callable
28
+
29
+ def call
30
+ [attribute_1, attribute_2]
31
+ end
32
+ end
33
+
24
34
  class KeywordsApiTest < Test::Unit::TestCase
25
35
  def test_keywords
26
36
  assert_equal TestKeywords.call(attribute_1: "a", attribute_2: "b"), ["a", "b"]
@@ -37,6 +47,10 @@ class KeywordsApiTest < Test::Unit::TestCase
37
47
  assert_equal TestKeywordsDefaults.call(attribute_1: "a", attribute_2: "b"), ["a", "b", "default_value_3"]
38
48
  end
39
49
 
50
+ def test_integer_defaults
51
+ assert_equal TestKeywordsIntegerDefaults.call(attribute_1: 1), [1, 2]
52
+ end
53
+
40
54
  private
41
55
 
42
56
  def test_object
@@ -3,7 +3,7 @@ require_relative '../lib/smart_init/main'
3
3
 
4
4
  class TestClass
5
5
  extend SmartInit
6
- initialize_with :attribute_1, :attribute_2
6
+ initialize_with_v1 :attribute_1, :attribute_2
7
7
  is_callable
8
8
 
9
9
  def call
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: 2.0.3
4
+ version: 3.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: 2018-09-24 00:00:00.000000000 Z
11
+ date: 2019-01-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -56,7 +56,7 @@ files:
56
56
  - lib/smart_init/version.rb
57
57
  - smart_init.gemspec
58
58
  - test/test_keywords_api.rb
59
- - test/test_standard_api.rb
59
+ - test/test_v1_api.rb
60
60
  homepage: http://github.com/pawurb/smart_init
61
61
  licenses:
62
62
  - MIT
@@ -76,8 +76,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
76
  - !ruby/object:Gem::Version
77
77
  version: '0'
78
78
  requirements: []
79
- rubyforge_project:
80
- rubygems_version: 2.6.14
79
+ rubygems_version: 3.0.0
81
80
  signing_key:
82
81
  specification_version: 4
83
82
  summary: Remove Ruby initializer boilerplate code