creatable 2.3.0 → 2.3.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -0
- data/lib/creatable/class_methods.rb +12 -4
- data/lib/creatable/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dea71b1c10a1d84dba409e63e548bf0ae7506281
|
4
|
+
data.tar.gz: 0c8c4e8fd956cc31098e489d17597fb057fd1011
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2f5c993adc099683ca06ad07d1f9e312e766973beb0ab2629c2429464e5b1df8571cdeb49bf8df078b50258852a61a6e3c78654bed3c4c0927014925fe614164
|
7
|
+
data.tar.gz: d3859c46f62b4bd23b00d9a74cc57b0b57bd2b5e495edb816474b1e28b049329b083c0d15bebc80f32b6b596875a012e501fb52b83665e55d3f2ea91e971e5cb
|
data/README.md
CHANGED
@@ -66,6 +66,10 @@ end
|
|
66
66
|
# creating a new object from parameters
|
67
67
|
A.create i.to_parameters
|
68
68
|
```
|
69
|
+
## Usage notes
|
70
|
+
|
71
|
+
- key can be in several formats: 'key', :key, and ':key' will all be accepted.
|
72
|
+
- if you supply a `block` of code, it will be executed right before `.create` returns.
|
69
73
|
|
70
74
|
## Development
|
71
75
|
|
@@ -42,19 +42,22 @@ module Creatable
|
|
42
42
|
def create(args = {})
|
43
43
|
object = new
|
44
44
|
|
45
|
+
fixed_args = {}
|
46
|
+
args.each { |k, v| fixed_args.store(fix_key(k), v) }
|
47
|
+
|
45
48
|
attributes.each do |a|
|
46
|
-
next unless
|
49
|
+
next unless fixed_args.key? a[:name].to_sym
|
47
50
|
|
48
|
-
value =
|
51
|
+
value = fixed_args[a[:name].to_sym]
|
49
52
|
|
50
53
|
if a[:block]
|
51
54
|
a[:block].call object, value
|
52
55
|
else
|
53
|
-
object.instance_variable_set "@#{a[:name]}"
|
56
|
+
object.instance_variable_set "@#{a[:name].to_sym}", value
|
54
57
|
end
|
55
58
|
end
|
56
59
|
|
57
|
-
yield object,
|
60
|
+
yield object, fixed_args if block_given?
|
58
61
|
object
|
59
62
|
end
|
60
63
|
|
@@ -75,5 +78,10 @@ module Creatable
|
|
75
78
|
instance_variable_set "@#{name}", value
|
76
79
|
end
|
77
80
|
end
|
81
|
+
|
82
|
+
def fix_key(key)
|
83
|
+
key = key[1..-1] if key.is_a?(String) && key.start_with?(":")
|
84
|
+
key.to_sym
|
85
|
+
end
|
78
86
|
end
|
79
87
|
end
|
data/lib/creatable/version.rb
CHANGED