class2 0.6.0 → 0.6.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/.github/workflows/ci.yml +25 -0
- data/Changes +4 -0
- data/README.md +27 -0
- data/class2.gemspec +2 -2
- data/lib/class2/inflector.rb +51 -0
- data/lib/class2/version.rb +1 -1
- data/lib/class2.rb +12 -2
- metadata +8 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9e33244884d5c16ee2b4d8b43195e2247bfc88ad9d829cfcdaf32efc25195a72
|
|
4
|
+
data.tar.gz: c1e550bf7044f09d66930f09a859f0bf6713e83a3f648d5fb511d0deef8ba9f1
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9959d03761af0652265bb53e65d75614ddd47bdf0315ffe1f91d7a31396124d31b2bc3369272ebd64667b51792ef543f9ec99c800b963ed9680fc409a78d35f1
|
|
7
|
+
data.tar.gz: 9ca603b6e2dfc257d26d8e6e31aebcc6ef08af5c46c0bfc53fdc0552a15a926c1f3a86e1fa439c55cd487ef4d0ce86c6207a101d5f44688391fe0d2d340325ae
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["**"]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
|
|
12
|
+
strategy:
|
|
13
|
+
fail-fast: false
|
|
14
|
+
matrix:
|
|
15
|
+
ruby: ["2.7", "3.0", "3.1", "3.2", "3.3", "3.4", "4.0"]
|
|
16
|
+
|
|
17
|
+
steps:
|
|
18
|
+
- uses: actions/checkout@v7
|
|
19
|
+
- name: Set up Ruby
|
|
20
|
+
uses: ruby/setup-ruby@v1
|
|
21
|
+
with:
|
|
22
|
+
ruby-version: ${{ matrix.ruby }}
|
|
23
|
+
bundler-cache: true
|
|
24
|
+
- name: Run the default task
|
|
25
|
+
run: bundle exec rake
|
data/Changes
CHANGED
data/README.md
CHANGED
|
@@ -124,6 +124,32 @@ For more info on accessor formats and JSON see:
|
|
|
124
124
|
* [`Class2::UpperCamelCase`](https://www.rubydoc.info/gems/class2/Class2/UpperCamelCase)
|
|
125
125
|
* [`Class2::LowerCamelCase`](https://www.rubydoc.info/gems/class2/Class2/LowerCamelCase)
|
|
126
126
|
|
|
127
|
+
[Using Ruby-specific JSON extensions](https://github.com/ruby/json?tab=readme-ov-file#usage) you can define
|
|
128
|
+
Ruby types in the JSON class2 will use for type conversion:
|
|
129
|
+
|
|
130
|
+
```json
|
|
131
|
+
{
|
|
132
|
+
"your_class": {
|
|
133
|
+
"id": 0,
|
|
134
|
+
"name": "string",
|
|
135
|
+
"updated_at": {"json_class":"Time","s":0,"n":0},
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Then require the appropriate JSON conversion class:
|
|
141
|
+
|
|
142
|
+
```rb
|
|
143
|
+
require "json/add/time"
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
This will result in class2 creating a `MyClass` class with the following:
|
|
147
|
+
|
|
148
|
+
- `#id` returning an `Integer` instance
|
|
149
|
+
- `#name` returning a `String` instance
|
|
150
|
+
- `#updated_at` returning a `Time` instance
|
|
151
|
+
|
|
152
|
+
This will work for any [supported conversions](#conversions).
|
|
127
153
|
|
|
128
154
|
You can also autoload a definition from a DATA section:
|
|
129
155
|
|
|
@@ -191,6 +217,7 @@ You can use any of these classes or their instances in your class definitions:
|
|
|
191
217
|
* `Float`
|
|
192
218
|
* `Hash`
|
|
193
219
|
* `Integer`
|
|
220
|
+
* `Time`
|
|
194
221
|
* `TrueClass`/`FalseClass` - either one will cause a boolean conversion
|
|
195
222
|
|
|
196
223
|
Custom conversions are possible, just add the conversion to
|
data/class2.gemspec
CHANGED
|
@@ -18,10 +18,10 @@ Gem::Specification.new do |spec|
|
|
|
18
18
|
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
19
19
|
spec.require_paths = ["lib"]
|
|
20
20
|
|
|
21
|
-
spec.add_dependency "activesupport", ">= 3.2", "<
|
|
21
|
+
spec.add_dependency "activesupport", ">= 3.2", "< 9"
|
|
22
22
|
|
|
23
23
|
spec.add_development_dependency "bundler"
|
|
24
|
-
spec.add_development_dependency "rake", "~>
|
|
24
|
+
spec.add_development_dependency "rake", "~> 13"
|
|
25
25
|
spec.add_development_dependency "appraisal"
|
|
26
26
|
spec.add_development_dependency "minitest"
|
|
27
27
|
end
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support/inflector"
|
|
4
|
+
|
|
5
|
+
class Class2
|
|
6
|
+
#
|
|
7
|
+
# String inflection used to convert attribute names into class names, method
|
|
8
|
+
# names, and JSON keys.
|
|
9
|
+
#
|
|
10
|
+
# A lightweight wrapper around ActiveSupport::Inflector. Class2 calls this
|
|
11
|
+
# instead of ActiveSupport's String and Module extensions so that the
|
|
12
|
+
# inflection library can be replaced without changing the rest of Class2.
|
|
13
|
+
#
|
|
14
|
+
module Inflector
|
|
15
|
+
extend self
|
|
16
|
+
|
|
17
|
+
def pluralize(word)
|
|
18
|
+
ActiveSupport::Inflector.pluralize(word.to_s)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def singularize(word)
|
|
22
|
+
ActiveSupport::Inflector.singularize(word.to_s)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
#
|
|
26
|
+
# Convert a table, attribute, or method name into a class name.
|
|
27
|
+
# "user_statuses" => "UserStatus", "admin/user" => "Admin::User".
|
|
28
|
+
#
|
|
29
|
+
def classify(word)
|
|
30
|
+
ActiveSupport::Inflector.classify(word.to_s)
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
#
|
|
34
|
+
# "some_value" => "SomeValue", "some_value", true => "someValue".
|
|
35
|
+
# "/" becomes "::".
|
|
36
|
+
#
|
|
37
|
+
# ActiveSupport's second argument is the opposite of `lower`: it is true
|
|
38
|
+
# when the first letter should be upper cased.
|
|
39
|
+
#
|
|
40
|
+
def camelize(word, lower = false)
|
|
41
|
+
ActiveSupport::Inflector.camelize(word.to_s, !lower)
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
#
|
|
45
|
+
# "SomeValue" => "some_value", "Admin::User" => "admin/user".
|
|
46
|
+
#
|
|
47
|
+
def underscore(word)
|
|
48
|
+
ActiveSupport::Inflector.underscore(word.to_s)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
data/lib/class2/version.rb
CHANGED
data/lib/class2.rb
CHANGED
|
@@ -3,10 +3,12 @@
|
|
|
3
3
|
require "date"
|
|
4
4
|
require "time" # for parse()
|
|
5
5
|
require "json"
|
|
6
|
+
require "rbconfig" # for autoload()
|
|
6
7
|
require "active_support/core_ext/module"
|
|
7
8
|
require "active_support/inflector"
|
|
8
9
|
|
|
9
10
|
require "class2/version"
|
|
11
|
+
require "class2/inflector"
|
|
10
12
|
|
|
11
13
|
no_export = ENV["CLASS2_NO_EXPORT"]
|
|
12
14
|
|
|
@@ -61,9 +63,17 @@ class Class2
|
|
|
61
63
|
|
|
62
64
|
def autoload(namespace = Object, stack = nil) # :nodoc:
|
|
63
65
|
failure = lambda { |message| abort "class2: cannot autoload class definitions: #{message}" }
|
|
66
|
+
#
|
|
67
|
+
# Ignore our autoload file and require()'s internals. Both their file
|
|
68
|
+
# names and the backtrace format change between Rubies: kernel_require.rb
|
|
69
|
+
# and bundled_gems.rb (3.3+) are where the frames come from, and since
|
|
70
|
+
# 3.4 frames are quoted with ' instead of `. Some Rubies report the
|
|
71
|
+
# rubygems frames as <internal:...>, which is not a readable path.
|
|
72
|
+
#
|
|
73
|
+
libdir = RbConfig::CONFIG["rubylibdir"]
|
|
64
74
|
failure["cannot find the right caller"] unless (stack || caller).find do |line|
|
|
65
|
-
|
|
66
|
-
|
|
75
|
+
line =~ /(.+):\d+:in\s+[`']\S/ && $1.index("/class2/autoload.rb").nil? &&
|
|
76
|
+
!$1.start_with?(libdir, "<internal:")
|
|
67
77
|
end
|
|
68
78
|
|
|
69
79
|
# Give this precedence over global DATA constant
|
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.6.
|
|
4
|
+
version: 0.6.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Skye Shaw
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-09-16 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activesupport
|
|
@@ -19,7 +19,7 @@ dependencies:
|
|
|
19
19
|
version: '3.2'
|
|
20
20
|
- - "<"
|
|
21
21
|
- !ruby/object:Gem::Version
|
|
22
|
-
version: '
|
|
22
|
+
version: '9'
|
|
23
23
|
type: :runtime
|
|
24
24
|
prerelease: false
|
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -29,7 +29,7 @@ dependencies:
|
|
|
29
29
|
version: '3.2'
|
|
30
30
|
- - "<"
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version: '
|
|
32
|
+
version: '9'
|
|
33
33
|
- !ruby/object:Gem::Dependency
|
|
34
34
|
name: bundler
|
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -50,14 +50,14 @@ dependencies:
|
|
|
50
50
|
requirements:
|
|
51
51
|
- - "~>"
|
|
52
52
|
- !ruby/object:Gem::Version
|
|
53
|
-
version: '
|
|
53
|
+
version: '13'
|
|
54
54
|
type: :development
|
|
55
55
|
prerelease: false
|
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
|
57
57
|
requirements:
|
|
58
58
|
- - "~>"
|
|
59
59
|
- !ruby/object:Gem::Version
|
|
60
|
-
version: '
|
|
60
|
+
version: '13'
|
|
61
61
|
- !ruby/object:Gem::Dependency
|
|
62
62
|
name: appraisal
|
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -93,6 +93,7 @@ executables: []
|
|
|
93
93
|
extensions: []
|
|
94
94
|
extra_rdoc_files: []
|
|
95
95
|
files:
|
|
96
|
+
- ".github/workflows/ci.yml"
|
|
96
97
|
- ".gitignore"
|
|
97
98
|
- Appraisals
|
|
98
99
|
- Changes
|
|
@@ -107,6 +108,7 @@ files:
|
|
|
107
108
|
- lib/class2.rb
|
|
108
109
|
- lib/class2/autoload.rb
|
|
109
110
|
- lib/class2/autoload/namespaced.rb
|
|
111
|
+
- lib/class2/inflector.rb
|
|
110
112
|
- lib/class2/version.rb
|
|
111
113
|
homepage: https://github.com/sshaw/class2
|
|
112
114
|
licenses:
|