class2 0.2.2 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes +5 -0
- data/README.md +1 -1
- data/lib/class2.rb +8 -5
- data/lib/class2/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0646247304bc24095f0581f97b1bd1ca81638afd
|
4
|
+
data.tar.gz: af1db39469174d56a2da022c08f1bd00cb0d171b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b36794076c8fbdc3103417ef80168931bd03bfd671d64168c6add76aded76f445a75b2a73f4df8bc8c4d224926379bdb8cb74cfa3170b105d93bbc7f4f7544ea
|
7
|
+
data.tar.gz: 89269198476ad0561b081f0d93afabb5738a96981cb3618d6e52dca704927be9a6359afb64a210d14bd4e00f46ee123dfea7decae00041621d4f1735512c02b1
|
data/Changes
CHANGED
@@ -1,3 +1,8 @@
|
|
1
|
+
2017-10-11 v0.2.3
|
2
|
+
--------------------
|
3
|
+
* Bug fix: don't try to convert to Float, Fixnum, Date, DateTime when nil
|
4
|
+
* Bug fix: more constant checking before creating
|
5
|
+
|
1
6
|
2017-09-28 v0.2.2
|
2
7
|
--------------------
|
3
8
|
* Bug fix: check constant existance before creating
|
data/README.md
CHANGED
@@ -72,7 +72,7 @@ user.addresses << address
|
|
72
72
|
User.new(:name => "sshaw") == User.new(:name => "sshaw") # true
|
73
73
|
```
|
74
74
|
|
75
|
-
`class2` can create classes with typed attributes from example hashes.
|
75
|
+
`class2` can create classes with typed attributes from example hashes (with some caveats).
|
76
76
|
This makes it possible to build classes for things like API responses using the API response
|
77
77
|
itself as the specification:
|
78
78
|
|
data/lib/class2.rb
CHANGED
@@ -25,11 +25,11 @@ end
|
|
25
25
|
class Class2
|
26
26
|
CONVERSIONS = {
|
27
27
|
Array => lambda { |v| "Array(#{v})" },
|
28
|
-
Date => lambda { |v| "Date.parse(#{v})" },
|
29
|
-
DateTime => lambda { |v| "DateTime.parse(#{v})" },
|
30
|
-
Float => lambda { |v| "Float(#{v})" },
|
28
|
+
Date => lambda { |v| "#{v} && Date.parse(#{v})" },
|
29
|
+
DateTime => lambda { |v| "#{v} && DateTime.parse(#{v})" },
|
30
|
+
Float => lambda { |v| "#{v} && Float(#{v})" },
|
31
31
|
Hash => lambda { |v| sprintf "%s.respond_to?(:to_h) ? %s.to_h : %s", v, v, v },
|
32
|
-
Integer => lambda { |v| "Integer(#{v})" },
|
32
|
+
Integer => lambda { |v| "#{v} && Integer(#{v})" },
|
33
33
|
String => lambda { |v| "String(#{v})" },
|
34
34
|
TrueClass => lambda do |v|
|
35
35
|
sprintf '["1", 1, 1.0, true].freeze.include?(%s.is_a?(String) ? %s.strip : %s)', v, v, v
|
@@ -108,6 +108,9 @@ class Class2
|
|
108
108
|
|
109
109
|
make_method_name = lambda { |x| x.to_s.gsub(/[^\w]+/, "_") } # good enough
|
110
110
|
|
111
|
+
name = name.to_s.classify
|
112
|
+
return if namespace.const_defined?(name)
|
113
|
+
|
111
114
|
klass = Class.new do
|
112
115
|
def initialize(attributes = nil)
|
113
116
|
return unless attributes.is_a?(Hash)
|
@@ -224,7 +227,7 @@ class Class2
|
|
224
227
|
end
|
225
228
|
end
|
226
229
|
|
227
|
-
namespace.const_set(name
|
230
|
+
namespace.const_set(name, klass)
|
228
231
|
end
|
229
232
|
end
|
230
233
|
|
data/lib/class2/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: class2
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Skye Shaw
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-10-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|