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 +4 -4
- data/README.md +5 -2
- data/lib/creatable.rb +18 -2
- data/lib/creatable/{class_methods.rb → instance_methods.rb} +25 -14
- data/lib/creatable/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2c83e3f19290c6508f5dd3440c16c4153ecdab7d
|
4
|
+
data.tar.gz: d676eae79e326cf161399f709ec4e65edcb356f0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
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).
|
data/lib/creatable.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
require "creatable/version"
|
2
|
-
require 'creatable/
|
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
|
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
|
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
|
-
|
26
|
+
generate_writer(name)
|
31
27
|
else
|
32
|
-
|
33
|
-
|
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
|
data/lib/creatable/version.rb
CHANGED
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
|
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-
|
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/
|
247
|
+
- lib/creatable/instance_methods.rb
|
248
248
|
- lib/creatable/version.rb
|
249
249
|
homepage: https://github.com/erniebrodeur/creatable
|
250
250
|
licenses: []
|