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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1acb2d18da3e6cfb18689ecedaa0d86802107f6e
4
- data.tar.gz: fd226407d158ca0fd0646b7cd5a7a853e9ecd7cc
3
+ metadata.gz: 0646247304bc24095f0581f97b1bd1ca81638afd
4
+ data.tar.gz: af1db39469174d56a2da022c08f1bd00cb0d171b
5
5
  SHA512:
6
- metadata.gz: e5ab09b2e8ff5af58b82a1cd5e65bd8035d3bf6912819a322afaca3a63c28e713869b6b88ed60dc6f39f136ede41ce5acae6f38cc6e0b472afff4f525de82f19
7
- data.tar.gz: 7f24bcfc92a94d4ad35f7239b1299b7412ec384db2bf86b199e91bd20ee81fc7fdb008733571d4b3996b4ac233daf753d88d2a7fef41e9cbc71518c878c8c71a
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
 
@@ -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.to_s.classify, klass)
230
+ namespace.const_set(name, klass)
228
231
  end
229
232
  end
230
233
 
@@ -1,3 +1,3 @@
1
1
  class Class2
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
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.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-09-28 00:00:00.000000000 Z
11
+ date: 2017-10-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport