creatable 2.1.1 → 2.2.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: c329dfc34d0517b100f5f53dcd1d56997ff21ded
4
- data.tar.gz: 332be29fed1620fbb6cfea7fd6b6fce9904b6ffd
3
+ metadata.gz: e6b42abcff609998d39ef809a52187796c820a88
4
+ data.tar.gz: fa805820fcee26be7b4febe18422cf47f33c0140
5
5
  SHA512:
6
- metadata.gz: 6e112941a632494e7c0ea34bff9e0b56751726d75fa4ecbb1a6f74efc31908a0efb168067784713cd53558de8602a836c845709f456d70f8b53a5ff84daa49ca
7
- data.tar.gz: e65394e04cfe7df279f5c4d583615fd5fbf81c86291b84171566ee3abe241408b1120d912eeb746c8e009e2e83cfeff5728181787d86dc0ee12cbdc8fc730bc9
6
+ metadata.gz: f64fc80434e4a78a55b934b44d3d321c16315a3ddf5a8a592884d191a70592a271097f5b9e86e769d9fb4e98f58bc0bf7996c2fd9485f987189025fe50fd58bf
7
+ data.tar.gz: 8b77d760f5e4cad59d6f78ebecaccfc7883ce6bb6069447370d07d044544df7b0400d8981ec54a41a01017df788a6a1b65e61cf7042819ceb1e8f7f10cdf08e9
@@ -15,7 +15,7 @@ module Creatable
15
15
  # @raise [ArgumentError] if name is not supplied
16
16
  # @raise [ArgumentError] if the type is not accessor, reader, or writer
17
17
  # @return [Void]
18
- def attribute(name: nil, type: 'accessor', kind_of: nil)
18
+ def attribute(name: nil, type: 'accessor', kind_of: nil, &block)
19
19
  raise ArgumentError, 'name is a required parameter' unless name
20
20
  raise ArgumentError, "type must be of type: 'accessor', 'reader', or 'writer'" unless ['accessor', 'reader', 'writer'].include? type
21
21
 
@@ -30,11 +30,9 @@ module Creatable
30
30
  end
31
31
  end
32
32
 
33
- if attributes.map { |e| e[:name] }.include? name
34
- attributes.delete_if { |e| e[:name] == name }
35
- end
33
+ attributes.delete_if { |e| e[:name] == name } if attributes.map { |e| e[:name] }.include? name
36
34
 
37
- attributes.push(name: name, type: type, kind_of: kind_of)
35
+ attributes.push(name: name, type: type, kind_of: kind_of, block: block)
38
36
  nil
39
37
  end
40
38
 
@@ -43,13 +41,20 @@ module Creatable
43
41
  # @return [Object] Newly created object
44
42
  def create(args = {})
45
43
  object = new
46
- names = attributes.map { |e| e[:name].to_sym }
47
- args.each do |k, v|
48
- object.instance_variable_set "@#{k}".to_sym, v if names.include? k.to_sym
49
- end
44
+ # names = attributes.map { |e| e[:name].to_sym }
45
+
46
+ attributes.each do |a|
47
+ value = args[a[:name].to_sym]
48
+ next unless value
50
49
 
51
- yield(object) if block_given?
50
+ if a[:block]
51
+ a[:block].call object
52
+ else
53
+ object.instance_variable_set "@#{a[:name]}".to_sym, value
54
+ end
55
+ end
52
56
 
57
+ yield object if block_given?
53
58
  object
54
59
  end
55
60
 
@@ -1,3 +1,3 @@
1
1
  module Creatable
2
- VERSION = '2.1.1'.freeze # VERSION
2
+ VERSION = '2.2.0'.freeze # VERSION
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: creatable
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.1
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ernie Brodeur
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-10-15 00:00:00.000000000 Z
11
+ date: 2018-12-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump