quick_class 1.0.2 → 1.0.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -5
- data/lib/quick_class.rb +1 -1
- data/lib/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 75203a50476ed77a7f7f311f1593a49ac44d24719fe7f6b5ad3aafe1768497fa
|
4
|
+
data.tar.gz: b94797c4b5ec5f3d9a4435f532e58592bfc08b96cd28c83b5321a511e070cf76
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0006d0cb4caaa7985a24be7740b8f6cfe9dddb4de774f6ab9c5cbbb5e7ecf1bc33624fca6b2a3db1ace7daf90e78119a6974af04a5f3506b1e34dbaa53b61fe0
|
7
|
+
data.tar.gz: 0887077a3e6ee7cff85400e9e674463076c7c678da9331b8292249c3e1bead3512f73c328381c6b77636b6079bf0f46ed62fd6ea44e55cad9f7043798166a629
|
data/README.md
CHANGED
@@ -18,14 +18,14 @@ print Foo.attributes
|
|
18
18
|
```
|
19
19
|
|
20
20
|
`initialize` is generated and converted any passed arguments to instance variables.
|
21
|
-
The instance variables are accessible through
|
21
|
+
The instance variables are accessible through generated `attr_accessor` functions.
|
22
22
|
|
23
23
|
```
|
24
24
|
Foo.new
|
25
|
-
# => #<Foo:
|
25
|
+
# => #<Foo:xxxxxxxxxxxxxxx>
|
26
26
|
|
27
27
|
foo = Foo.new a: 2
|
28
|
-
# => #<Foo:
|
28
|
+
# => #<Foo:xxxxxxxxxxxxxxx @a=2>
|
29
29
|
|
30
30
|
foo.a
|
31
31
|
# => 2
|
@@ -38,9 +38,17 @@ The `default` method is also defined, which is the same as passing `attributes`
|
|
38
38
|
|
39
39
|
```
|
40
40
|
Foo.default
|
41
|
-
# => #<Foo:
|
41
|
+
# => #<Foo:xxxxxxxxxxxxxxx @a=0, @b=1>
|
42
42
|
```
|
43
43
|
|
44
|
+
It accepts arguments as well:
|
45
|
+
|
46
|
+
```
|
47
|
+
Foo.default b: 2
|
48
|
+
# => #<Foo:xxxxxxxxxxxxxxx @a=0, @b=2>
|
49
|
+
```
|
50
|
+
|
51
|
+
|
44
52
|
It works with interitance also:
|
45
53
|
|
46
54
|
```
|
@@ -49,7 +57,7 @@ class Bar < Foo
|
|
49
57
|
end
|
50
58
|
|
51
59
|
Bar.default
|
52
|
-
# => <Bar:
|
60
|
+
# => <Bar:xxxxxxxxxxxxxxx @a=0, @b=1, @c=2>
|
53
61
|
```
|
54
62
|
|
55
63
|
Internally, the `attributes=` method is dynamically generating `attr_accessor` and `default` functions.
|
data/lib/quick_class.rb
CHANGED
@@ -10,7 +10,7 @@ class QuickClass
|
|
10
10
|
# define public attr_accessor methods for all the attributes
|
11
11
|
attr_accessor *@attributes.keys
|
12
12
|
# define a .default method that invokes #initialize with .attributes
|
13
|
-
define_singleton_method(:default) { new @attributes }
|
13
|
+
define_singleton_method(:default) { |**opts| new @attributes.merge(opts) }
|
14
14
|
end
|
15
15
|
# read default attributes
|
16
16
|
def self.attributes; @attributes; end
|
data/lib/version.rb
CHANGED