creatable 2.0.1 → 2.1.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: c3f68612a079b4a7fd1cde02086d26ee91f4006b
4
- data.tar.gz: ccdf89aaac1f988e84f63183af4c43ed05cbc711
3
+ metadata.gz: 2c83e3f19290c6508f5dd3440c16c4153ecdab7d
4
+ data.tar.gz: d676eae79e326cf161399f709ec4e65edcb356f0
5
5
  SHA512:
6
- metadata.gz: a651ef82b80b066b7c390f6fa1b68fb4563fab374f6b256116f0f2f93242cb2aa8ef4208960258199bf9f19e727eb76e18a77f62db474aafda45bd9d67fe86a7
7
- data.tar.gz: 10b25e036b87828ed0f5833953607c8716a1113e72eb6c9f041a62e98bb4daebe0a5002f8ea9dec2b4e41a54a660a4f2343688b95431cefca9ceb76fbf5e6902
6
+ metadata.gz: 72cefaad2c05643ff0a09a72151bb5d726b8b751483e1581c8dbe27a4efa1432130b151c856e1a461f48d84e431cc5ad86c3a6f59a9904f117947201b3e32e11
7
+ data.tar.gz: e897d53dc78f7ef879c8e074d9a9d79592bf841ad3dafb8b149c5549b86942407aa21080d63dc07f2aed9993f21683f5f7b8f6be948e047c453c5330ebb3431e
data/README.md CHANGED
@@ -27,7 +27,7 @@ class A
27
27
  # extend the class (not include)
28
28
  extend Creatable
29
29
 
30
- # add an attribute (will default to accesor)
30
+ # add an attribute (will default to accessor)
31
31
  attribute name: 'an_attribute'
32
32
 
33
33
  # add the reader method only
@@ -38,6 +38,9 @@ class A
38
38
 
39
39
  # Restrict the type you can assign to this attribute
40
40
  attribute name: 'an_attribute', type: 'accessor', kind_of: String
41
+
42
+ # You can assign multiple types
43
+ attribute name: 'an_attribute', type: 'accessor', kind_of: [String, Array, nil]
41
44
  end
42
45
  ```
43
46
 
@@ -49,4 +52,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
49
52
 
50
53
  ## Contributing
51
54
 
52
- Bug reports and pull requests are welcome on GitHub at https://github.com/erniebrodeur/creatable.
55
+ Bug reports and pull requests are welcome on GitHub at [creatable](https://github.com/erniebrodeur/creatable).
@@ -1,5 +1,5 @@
1
1
  require "creatable/version"
2
- require 'creatable/class_methods'
2
+ require 'creatable/instance_methods'
3
3
 
4
4
  # Main module you include in your class
5
5
  module Creatable
@@ -7,7 +7,7 @@ module Creatable
7
7
  # @param [Object] object
8
8
  # @return [Void]
9
9
  def self.included(object)
10
- object.extend ClassMethods
10
+ object.extend InstanceMethods
11
11
  end
12
12
 
13
13
  # Returns the hash of built attributes
@@ -15,4 +15,20 @@ module Creatable
15
15
  def attributes
16
16
  self.class.attributes
17
17
  end
18
+
19
+ # Returns the names of the attributes.
20
+ # @return [Array] list of names of the attributes
21
+ def attribute_names
22
+ self.class.attributes.map { |a| a[:name] }
23
+ end
24
+
25
+ # Returns the settings of all attributes.
26
+ # @return [Hash] k/v pairs of the current class attributes
27
+ def to_parameters
28
+ h = {}
29
+ attribute_names.each do |name|
30
+ h.store name, instance_variable_get("@#{name}")
31
+ end
32
+ h
33
+ end
18
34
  end
@@ -1,6 +1,6 @@
1
1
  module Creatable
2
2
  # Class methods that get mixed in.
3
- module ClassMethods
3
+ module InstanceMethods
4
4
  # Returns the list of attributes attatched to this object
5
5
  # @return [Array] the current attributes
6
6
  def attributes
@@ -19,29 +19,22 @@ module Creatable
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
 
22
- if ['accessor', 'reader'].include?(type)
23
- define_method name.to_s do
24
- instance_variable_get "@#{name}"
25
- end
26
- end
22
+ generate_reader(name) if ['accessor', 'reader'].include?(type)
27
23
 
28
24
  if ['accessor', 'writer'].include?(type)
29
25
  if kind_of.nil?
30
- define_method("#{name}=") { |value| instance_variable_set "@#{name}", value }
26
+ generate_writer(name)
31
27
  else
32
- define_method("#{name}=") do |value|
33
- raise ArgumentError, "parameter #{name} (#{value.class}) is not a kind of (#{kind_of})" unless value.is_a?(kind_of) || value.nil?
34
- instance_variable_set "@#{name}", value
35
- end
28
+ kind_of = [kind_of] unless kind_of.is_a? Array
29
+ generate_required_writer(name, kind_of)
36
30
  end
37
31
  end
38
32
 
39
33
  if attributes.map { |e| e[:name] }.include? name
40
34
  attributes.delete_if { |e| e[:name] == name }
41
- attributes.push(name: name, type: type, kind_of: kind_of)
42
- else
43
- attributes.push(name: name, type: type, kind_of: kind_of)
44
35
  end
36
+
37
+ attributes.push(name: name, type: type, kind_of: kind_of)
45
38
  nil
46
39
  end
47
40
 
@@ -56,5 +49,23 @@ module Creatable
56
49
  end
57
50
  object
58
51
  end
52
+
53
+ private
54
+
55
+ def generate_reader(name)
56
+ define_method(name.to_s) { instance_variable_get "@#{name}" }
57
+ end
58
+
59
+ def generate_writer(name)
60
+ define_method("#{name}=") { |value| instance_variable_set "@#{name}", value }
61
+ end
62
+
63
+ def generate_required_writer(name, kind_of)
64
+ define_method("#{name}=") do |value|
65
+ raise(ArgumentError, "parameter #{name} (#{value.class}) is not a kind of (#{kind_of.join})") unless kind_of.include?(value.class)
66
+
67
+ instance_variable_set "@#{name}", value
68
+ end
69
+ end
59
70
  end
60
71
  end
@@ -1,3 +1,3 @@
1
1
  module Creatable
2
- VERSION = '2.0.1'.freeze # VERSION
2
+ VERSION = '2.1.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.0.1
4
+ version: 2.1.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-09-04 00:00:00.000000000 Z
11
+ date: 2018-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bump
@@ -244,7 +244,7 @@ files:
244
244
  - bin/setup
245
245
  - creatable.gemspec
246
246
  - lib/creatable.rb
247
- - lib/creatable/class_methods.rb
247
+ - lib/creatable/instance_methods.rb
248
248
  - lib/creatable/version.rb
249
249
  homepage: https://github.com/erniebrodeur/creatable
250
250
  licenses: []