class2 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Changes +4 -0
- data/README.md +72 -53
- data/lib/class2.rb +14 -2
- data/lib/class2/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7671575eba7274b6b325b522c133daeaf250f9ee
|
4
|
+
data.tar.gz: 9da2448df22a465c53201c23441a1142bc86d3bd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3aa91695a4eab4393fb211ec0d5582383e69e9e183728a532f4e5fe4c6d2f25d809f2e86e857299935ce5a216b790063dadb0281198a19c94952719b082cc901
|
7
|
+
data.tar.gz: be3aa3f33af4db98178ca53c056c143f1cfc5a3eef70d8d62ca49578c11beadf9d18212290f02e8571737d44f3f7647acba9afabf37c4a442326a014a2dffd82
|
data/Changes
CHANGED
data/README.md
CHANGED
@@ -1,13 +1,13 @@
|
|
1
|
-
#
|
1
|
+
# class2
|
2
2
|
|
3
|
-
Easily create hierarchies
|
3
|
+
Easily create class hierarchies that support nested attributes, type conversion, equality, and more.
|
4
4
|
|
5
5
|
[![Build Status](https://travis-ci.org/sshaw/class2.svg?branch=master)](https://travis-ci.org/sshaw/class2)
|
6
6
|
|
7
7
|
## Usage
|
8
8
|
|
9
9
|
```rb
|
10
|
-
|
10
|
+
class2 :user => [
|
11
11
|
:name, :age,
|
12
12
|
:addresses => [
|
13
13
|
:city, :state, :zip,
|
@@ -22,12 +22,13 @@ This creates 3 classes: `User`, `Address`, and `Country` with the following attr
|
|
22
22
|
* `Address`: city, state, zip, country
|
23
23
|
* `Country`: name, code
|
24
24
|
|
25
|
-
Each of these classes are created with [several additional methods](#methods).
|
26
25
|
|
27
|
-
|
26
|
+
Each of these classes are created with
|
27
|
+
[several additional methods](#methods). You can also specify types
|
28
|
+
(or [namespaces](#namespaces)):
|
28
29
|
|
29
30
|
```rb
|
30
|
-
|
31
|
+
class2 :user => {
|
31
32
|
:name => String,
|
32
33
|
:age => Fixnum,
|
33
34
|
:addresses => [
|
@@ -39,8 +40,8 @@ Class2 :user => {
|
|
39
40
|
]
|
40
41
|
}
|
41
42
|
```
|
42
|
-
|
43
|
-
|
43
|
+
|
44
|
+
Attributes without types are treated as is.
|
44
45
|
|
45
46
|
After calling either one of the above you can do the following:
|
46
47
|
|
@@ -71,8 +72,8 @@ user.addresses << address
|
|
71
72
|
User.new(:name => "sshaw") == User.new(:name => "sshaw") # true
|
72
73
|
```
|
73
74
|
|
74
|
-
`
|
75
|
-
This makes it possible to build classes for things like API responses
|
75
|
+
`class2` can create classes with typed attributes from example hashes.
|
76
|
+
This makes it possible to build classes for things like API responses using the API response
|
76
77
|
itself as the specification:
|
77
78
|
|
78
79
|
```rb
|
@@ -94,14 +95,45 @@ response = [
|
|
94
95
|
}
|
95
96
|
]
|
96
97
|
|
97
|
-
|
98
|
+
class2 :commit => response.first
|
98
99
|
|
99
100
|
commit = Commit.new(response.first)
|
100
101
|
commit.author.name # "sshaw"
|
101
102
|
commit.comment_count # 0
|
102
103
|
```
|
103
104
|
|
104
|
-
###
|
105
|
+
### class2 API
|
106
|
+
|
107
|
+
The are 3 ways to use class2. Pick the one that suites your style and/or requirements:
|
108
|
+
|
109
|
+
* `class2()`
|
110
|
+
* `Class2()`
|
111
|
+
* `Class2.new`
|
112
|
+
|
113
|
+
They all create classes the same way. They all return `nil`.
|
114
|
+
|
115
|
+
To control the creation of the top-level methods, see the
|
116
|
+
[`CLASS2_NO_EXPORT` environment variable](https://github.com/sshaw/class2/blob/a7ebe022b48db33d532cc483b0e036e4ec7d2e66/lib/class2.rb#L9-L23).
|
117
|
+
|
118
|
+
#### Naming
|
119
|
+
|
120
|
+
`class2` uses
|
121
|
+
[`String#classify`](http://api.rubyonrails.org/classes/String.html#method-i-classify)
|
122
|
+
to turn keys into class names: `:foo` will be `Foo`, `:foo_bars` will
|
123
|
+
be `FooBar`.
|
124
|
+
|
125
|
+
Plural keys with an array value are always assumed to be accessors for
|
126
|
+
a collection and will default to returning an `Array`. `#classify` is
|
127
|
+
used to derive the class names from the plural attribute names. An
|
128
|
+
`:addresses` key with an `Array` value will result in a class named
|
129
|
+
`Address` being created.
|
130
|
+
|
131
|
+
Plurality is determined by [`String#pluralize`](http://api.rubyonrails.org/classes/String.html#method-i-pluralize).
|
132
|
+
|
133
|
+
#### Conversions
|
134
|
+
|
135
|
+
An attempt is made to convert the attribute's type when a value is passed to the constructor
|
136
|
+
or set via its accessor.
|
105
137
|
|
106
138
|
You can use any of these classes or their instances in your class definitions:
|
107
139
|
|
@@ -116,38 +148,25 @@ You can use any of these classes or their instances in your class definitions:
|
|
116
148
|
Custom conversions are possible, just add the conversion to
|
117
149
|
[`Class2::CONVERSIONS`](https://github.com/sshaw/class2/blob/517239afc76a4d80677e169958a1dc7836726659/lib/class2.rb#L14-L29)
|
118
150
|
|
119
|
-
|
151
|
+
#### Namespaces
|
120
152
|
|
121
|
-
`
|
153
|
+
`class2` can use an exiting namespace or create a new one:
|
122
154
|
|
123
155
|
```rb
|
124
|
-
|
156
|
+
class2 My::Namespace,
|
157
|
+
:user => %i[name age]
|
125
158
|
|
126
159
|
My::Namespace::User.new(:name => "sshaw")
|
127
160
|
|
128
|
-
|
161
|
+
class2 "New::Namespace",
|
162
|
+
:user => %i[name age]
|
129
163
|
|
130
164
|
New::Namespace::User.new(:name => "sshaw")
|
131
165
|
```
|
132
166
|
|
133
|
-
|
134
|
-
|
135
|
-
`Class2` uses
|
136
|
-
[`String#classify`](http://api.rubyonrails.org/classes/String.html#method-i-classify)
|
137
|
-
to turn keys into class names: `:foo` will be `Foo`, `:foo_bars` will
|
138
|
-
be `FooBar`.
|
167
|
+
#### Methods
|
139
168
|
|
140
|
-
|
141
|
-
a collection and will default to returning an `Array`. `#classify` is
|
142
|
-
used to derive the class names from the plural attribute names. An
|
143
|
-
`:addresses` key with an `Array` value will result in a class named
|
144
|
-
`Address` being created.
|
145
|
-
|
146
|
-
Plurality is determined by [`String#pluralize`](http://api.rubyonrails.org/classes/String.html#method-i-pluralize).
|
147
|
-
|
148
|
-
### Methods
|
149
|
-
|
150
|
-
Classes created by `Class2` will have:
|
169
|
+
Classes created by `class2` will have:
|
151
170
|
|
152
171
|
* A constructor that accepts a nested attribute hash
|
153
172
|
* Attribute readers and writers
|
@@ -155,28 +174,12 @@ Classes created by `Class2` will have:
|
|
155
174
|
* `#eql?` and `#==`
|
156
175
|
* `#hash`
|
157
176
|
|
158
|
-
|
159
|
-
|
160
|
-
The default constructor ignores unknown attributes.
|
161
|
-
If you prefer to raise an exception include `Class2::StrictConstructor`:
|
162
|
-
|
163
|
-
```rb
|
164
|
-
Class2 :user => %w[id name age] do
|
165
|
-
include Class2::StrictConstructor
|
166
|
-
end
|
167
|
-
```
|
168
|
-
|
169
|
-
Now an `ArgumentError` will be raised if anything but `id`, `name`, or
|
170
|
-
`age` are passed in.
|
171
|
-
|
172
|
-
Also see [Customizations](#customizations).
|
173
|
-
|
174
|
-
### Customizations
|
177
|
+
#### Customizations
|
175
178
|
|
176
179
|
To add methods or include modules just open up the class and write or include them:
|
177
180
|
|
178
181
|
```rb
|
179
|
-
|
182
|
+
class2 :user => :name
|
180
183
|
|
181
184
|
class User
|
182
185
|
include SomeModule
|
@@ -189,11 +192,11 @@ end
|
|
189
192
|
User.new(:name => "sshaw").first_initial
|
190
193
|
```
|
191
194
|
|
192
|
-
`
|
195
|
+
`class2` does accept a block whose contents will be added to
|
193
196
|
*every* class defined within the call:
|
194
197
|
|
195
198
|
```rb
|
196
|
-
|
199
|
+
class2 :user => :name, :address => :city do
|
197
200
|
include ActiveModel::Conversion
|
198
201
|
extend ActiveModel::Naming
|
199
202
|
end
|
@@ -202,6 +205,22 @@ User.new.model_name.route_key
|
|
202
205
|
Address.new.model_name.route_key
|
203
206
|
```
|
204
207
|
|
208
|
+
#### Constructor
|
209
|
+
|
210
|
+
The default constructor ignores unknown attributes.
|
211
|
+
If you prefer to raise an exception include `Class2::StrictConstructor`:
|
212
|
+
|
213
|
+
```rb
|
214
|
+
class2 :user => %w[id name age] do
|
215
|
+
include Class2::StrictConstructor
|
216
|
+
end
|
217
|
+
```
|
218
|
+
|
219
|
+
Now an `ArgumentError` will be raised if anything but `id`, `name`, or
|
220
|
+
`age` are passed in.
|
221
|
+
|
222
|
+
Also see [Customizations](#customizations).
|
223
|
+
|
205
224
|
## See Also
|
206
225
|
|
207
226
|
The Perl modules that served as inspiration:
|
data/lib/class2.rb
CHANGED
@@ -6,8 +6,20 @@ require "active_support/core_ext/string"
|
|
6
6
|
|
7
7
|
require "class2/version"
|
8
8
|
|
9
|
-
|
10
|
-
|
9
|
+
no_export = ENV["CLASS2_NO_EXPORT"]
|
10
|
+
|
11
|
+
unless no_export == "1"
|
12
|
+
unless no_export == "Class2"
|
13
|
+
def Class2(*args, &block)
|
14
|
+
Class2.new(*args, &block)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
unless no_export == "class2"
|
19
|
+
def class2(*args, &block)
|
20
|
+
Class2.new(*args, &block)
|
21
|
+
end
|
22
|
+
end
|
11
23
|
end
|
12
24
|
|
13
25
|
class Class2
|
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.2.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: 2017-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -103,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
103
103
|
version: '0'
|
104
104
|
requirements: []
|
105
105
|
rubyforge_project:
|
106
|
-
rubygems_version: 2.
|
106
|
+
rubygems_version: 2.4.5.1
|
107
107
|
signing_key:
|
108
108
|
specification_version: 4
|
109
109
|
summary: Easily create hierarchies of classes that support nested attributes, type
|