class2 0.4.1 → 0.5.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: 017263e92869b1680872ca430f4dab936a69f1b5
4
- data.tar.gz: fa66be4eb23322ac1459c1e728229fb29c18eb05
3
+ metadata.gz: 495d09d37f08a9426e07e7dc11aeb89e98e91898
4
+ data.tar.gz: d045a532d5807ff0683165696e39db4b139fc92d
5
5
  SHA512:
6
- metadata.gz: 644ff7e386b61470745d7c0b63e5d6376ca793e122e9f262cf541e4d7d6455c9a88195b044b8f476633c3f3ff5fdbca250627c2a21132de146cb90776fc67f64
7
- data.tar.gz: 12f2fd230377e12285f4c79cd6607cd09d1eac31880ba8888c84ed2822175c6637716d36e078b3b71d344d0e1a087881501a44e842c7714e4b05bb861ce77a47
6
+ metadata.gz: 7a8a8369c8ab850c2d9233a8951facf0163d1e404a4fff3919099d3d7f6c84160cae2112a2ae15dc7f91ff18f3bc0463f71732f4d230d5074d56e69a98032fca
7
+ data.tar.gz: ea2223eb9048584675754f2c240a79055f654f897117a5219913874ab0872994e87dbf76e073c8317af3a4ffd0a91685e074b5fe4a74d6b470732e42792bc648
data/.travis.yml CHANGED
@@ -1,6 +1,7 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
+ - jruby-9
4
5
  - 2.3
5
6
  - 2.4
6
7
  - 2.5
data/Changes CHANGED
@@ -1,3 +1,7 @@
1
+ 2018-06-07 v0.5.0
2
+ --------------------
3
+ * Add support for autoloading class definitions from JSON in a DATA section
4
+
1
5
  2018-05-29 v0.4.1
2
6
  --------------------
3
7
  * Fix a namespace'd class from not being defined if top-level class exists
data/README.md CHANGED
@@ -126,6 +126,32 @@ For more info on accessor formats and JSON see:
126
126
  * [`Class2::UpperCamelCase`](https://www.rubydoc.info/gems/class2/Class2/UpperCamelCase)
127
127
  * [`Class2::LowerCamelCase`](https://www.rubydoc.info/gems/class2/Class2/LowerCamelCase)
128
128
 
129
+
130
+ You can also autoload a definition from a DATA section:
131
+
132
+ ```rb
133
+ require "class2/autoload" # builds classes from below JSON
134
+ require "pp"
135
+
136
+ commit = Commit.new(:author => { :name => "luser1" })
137
+ pp commit.to_h
138
+
139
+ __END__
140
+ {
141
+ "response": {
142
+ "sha": "f52f1ed9144e1f73346176ab79a61af78df1b6bd",
143
+ "commit": {
144
+ "author": {
145
+ "name": "sshaw",
146
+ "email": "skye.shaw@gmail.com",
147
+ "date": "2016-06-30T03:51:00Z"
148
+ }
149
+ },
150
+ "comment_count": 0
151
+ }
152
+ }
153
+ ```
154
+
129
155
  ### class2 API
130
156
 
131
157
  The are 3 ways to use class2. Pick the one that suites your style and/or requirements:
data/lib/class2.rb CHANGED
@@ -58,6 +58,30 @@ class Class2
58
58
  nil
59
59
  end
60
60
 
61
+ def autoload(namespace = Object) # :nodoc:
62
+ failure = lambda { |message| abort "class2: cannot autoload class definitions: #{message}" }
63
+ failure["cannot find the right caller"] unless caller.find do |line|
64
+ line.index("/kernel_require.rb:").nil? && line =~ /(.+):\d+:in\s+`\w/
65
+ end
66
+
67
+ data = String.new
68
+ File.open($1) do |io|
69
+ while line = io.gets
70
+ if line == "__END__\n"
71
+ data << line while line = io.gets
72
+ end
73
+ end
74
+ end
75
+
76
+ data = ::DATA.read if data.empty? && defined?(::DATA)
77
+ failure["no data section found"] if data.empty?
78
+
79
+ spec = JSON.parse(data)
80
+ Class2.new(namespace, spec)
81
+ rescue IOError, SystemCallError, JSON::ParserError => e
82
+ failure[e.message]
83
+ end
84
+
61
85
  private
62
86
 
63
87
  def create_namespace(str)
@@ -0,0 +1,2 @@
1
+ require "class2"
2
+ Class2.autoload
@@ -0,0 +1,9 @@
1
+ require "class2"
2
+
3
+ unless caller.find { |bt| bt =~ /(.+):\d+:in\s+`require'\z/ }
4
+ abort "class2: cannot auto detect namespace: cannot find what required me"
5
+ end
6
+
7
+ source = $1
8
+ namespace = source =~ %r{/lib/(.+?)(?:\.rb)?\z} ? $1 : File.basename(source, File.extname(source))
9
+ Class2.autoload(namespace.camelize)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Class2
4
- VERSION = "0.4.1"
4
+ VERSION = "0.5.0"
5
5
  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.4.1
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Skye Shaw
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-05-30 00:00:00.000000000 Z
11
+ date: 2018-06-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -82,6 +82,8 @@ files:
82
82
  - Rakefile
83
83
  - class2.gemspec
84
84
  - lib/class2.rb
85
+ - lib/class2/autoload.rb
86
+ - lib/class2/autoload/namespaced.rb
85
87
  - lib/class2/version.rb
86
88
  homepage: https://github.com/sshaw/class2
87
89
  licenses: