quick_class 1.0.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 +7 -0
- data/README.md +78 -0
- data/lib/quick_class.rb +26 -0
- data/lib/version.rb +3 -0
- metadata +46 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 3b3093c16f9af3bb3e33f49141d5c1228ff65dd2f9adf157d4d7d2227b0959c0
|
4
|
+
data.tar.gz: 2f421209b547fc473d15b40e309d30ebbcae4b3d7c73e27e0b6b1605407982d1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f48ab120ab0f22213b54bfd7a92b10bad4e64bc52ed66b590d0f9e55d653ae042115a8055347964702a90e68f835d2716251b5c0a8279c980ee6e627d49102ba
|
7
|
+
data.tar.gz: da3759670d7f7d63e6d123972c5853dc049c78c85aff7c314d6d1ef3ff9c610dd59d106ac710d92ccb4525c0294e73112da2dfcdabb2b7e4d64866a24ba3947c
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
## quick_class
|
2
|
+
|
3
|
+
Install it with `gem install quick_class` or `gem 'quick_class'` - you know the drill.
|
4
|
+
|
5
|
+
Usage:
|
6
|
+
|
7
|
+
First default a class and call `attributes=`:
|
8
|
+
|
9
|
+
```
|
10
|
+
require 'quick_class'
|
11
|
+
|
12
|
+
class Foo < QuickClass
|
13
|
+
self.attributes = { a: 0, b: 1 }
|
14
|
+
end
|
15
|
+
|
16
|
+
print Foo.attributes
|
17
|
+
# => {:a=>0, :b=>1}
|
18
|
+
```
|
19
|
+
|
20
|
+
`initialize` is generated and converted any passed arguments to instance variables.
|
21
|
+
The instance variables are accessible through generator `attr_accessor` functions.
|
22
|
+
|
23
|
+
```
|
24
|
+
Foo.new
|
25
|
+
# => #<Foo:0x007ff5e8a70d10>
|
26
|
+
|
27
|
+
foo = Foo.new a: 2
|
28
|
+
# => #<Foo:0x007ff5e8a70d10 @a=2>
|
29
|
+
|
30
|
+
foo.a
|
31
|
+
# => 2
|
32
|
+
|
33
|
+
foo.a = 3
|
34
|
+
# => 3
|
35
|
+
```
|
36
|
+
|
37
|
+
The `default` method is also defined, which is the same as passing `attributes` to `initialize`:
|
38
|
+
|
39
|
+
```
|
40
|
+
Foo.default
|
41
|
+
# => #<Foo:0x007fef7398f6b0 @a=0, @b=2>
|
42
|
+
```
|
43
|
+
|
44
|
+
It works with interitance also:
|
45
|
+
|
46
|
+
```
|
47
|
+
class Bar < Foo
|
48
|
+
self.attributes = { c: 2 }
|
49
|
+
end
|
50
|
+
|
51
|
+
Bar.default
|
52
|
+
# => <Bar:0x007f9868906b60 @a=0, @b=1, @c=2>
|
53
|
+
```
|
54
|
+
|
55
|
+
Internally, the `attributes=` method is dynamically generating `attr_accessor` and `default` functions.
|
56
|
+
|
57
|
+
It can be called multiple times, and these methods will correctly update:
|
58
|
+
|
59
|
+
```
|
60
|
+
class Foo < QuickClass
|
61
|
+
self.attributes = { a: 0 }
|
62
|
+
end
|
63
|
+
|
64
|
+
Foo.default.a
|
65
|
+
# => 0
|
66
|
+
|
67
|
+
Foo.attributes = { b: 1 }
|
68
|
+
|
69
|
+
Foo.default.a
|
70
|
+
# => NoMethodError
|
71
|
+
|
72
|
+
Foo.default.b
|
73
|
+
# => 1
|
74
|
+
```
|
75
|
+
|
76
|
+
The whole source code is about 15 un-minified lines without comments.
|
77
|
+
|
78
|
+
use at own risk
|
data/lib/quick_class.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# a quick and easy class factory
|
2
|
+
class QuickClass
|
3
|
+
# main public method - define attributes for a class
|
4
|
+
def self.attributes=(**keyvals)
|
5
|
+
# if attributes is already defined, delete the attr_accessor methods
|
6
|
+
@attributes&.each_key do |k|
|
7
|
+
[:"#{k}", :"#{k}="].each { |m| send :undef_method, m }
|
8
|
+
end
|
9
|
+
# define attributes at the keyvals passed to this method as an argument,
|
10
|
+
# merged with the attributes of the superclass, if there is one.
|
11
|
+
@attributes = keyvals.merge(superclass&.attributes || {})
|
12
|
+
# define public attr_accessor methods for all the attributes
|
13
|
+
attr_accessor *@attributes.keys
|
14
|
+
# define a .default method that invokes #initialize with .attributes
|
15
|
+
define_singleton_method(:default) { new @attributes }
|
16
|
+
end
|
17
|
+
# read default attributes
|
18
|
+
def self.attributes; @attributes; end
|
19
|
+
# configure attributes from #initialize.
|
20
|
+
# note that defaults are not set here. Use .default for that.
|
21
|
+
def initialize(opts={})
|
22
|
+
opts.each { |k,v| instance_variable_set("@#{k}", v) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
# See README.md for usage example
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quick_class
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- max pleaner
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-01-10 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: ''
|
14
|
+
email: maxpleaner@gmail.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- README.md
|
20
|
+
- lib/quick_class.rb
|
21
|
+
- lib/version.rb
|
22
|
+
homepage: http://rubygems.org/gems/quick_class
|
23
|
+
licenses:
|
24
|
+
- MIT
|
25
|
+
metadata: {}
|
26
|
+
post_install_message:
|
27
|
+
rdoc_options: []
|
28
|
+
require_paths:
|
29
|
+
- lib
|
30
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '2.3'
|
35
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: 2.7.3
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project:
|
42
|
+
rubygems_version: 2.7.3
|
43
|
+
signing_key:
|
44
|
+
specification_version: 4
|
45
|
+
summary: a minimal class factory
|
46
|
+
test_files: []
|