class2 0.4.1 → 0.5.0
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/.travis.yml +1 -0
- data/Changes +4 -0
- data/README.md +26 -0
- data/lib/class2.rb +24 -0
- data/lib/class2/autoload.rb +2 -0
- data/lib/class2/autoload/namespaced.rb +9 -0
- data/lib/class2/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 495d09d37f08a9426e07e7dc11aeb89e98e91898
|
4
|
+
data.tar.gz: d045a532d5807ff0683165696e39db4b139fc92d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7a8a8369c8ab850c2d9233a8951facf0163d1e404a4fff3919099d3d7f6c84160cae2112a2ae15dc7f91ff18f3bc0463f71732f4d230d5074d56e69a98032fca
|
7
|
+
data.tar.gz: ea2223eb9048584675754f2c240a79055f654f897117a5219913874ab0872994e87dbf76e073c8317af3a4ffd0a91685e074b5fe4a74d6b470732e42792bc648
|
data/.travis.yml
CHANGED
data/Changes
CHANGED
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,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)
|
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.
|
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-
|
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:
|