uber 0.0.10 → 0.0.11
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGES.md +4 -0
- data/README.md +9 -10
- data/lib/uber/inheritable_attr.rb +18 -3
- data/lib/uber/uber_version.rb +1 -1
- data/test/inheritable_attr_test.rb +26 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 542663f6bd6360841443f9ac9cb3a3e5d8ea6672
|
4
|
+
data.tar.gz: 2bf5d837bc6d9f64b32a48aa28f8ad3dd5a3cd37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8b135d80f988a6e9230c5a3317df4dc16ea982fba517c27c4b1638538b204f1d388829fcf194ed93e679f8536efc402c66c8c21c961e5b057113676bf9bfbb10
|
7
|
+
data.tar.gz: 44c29b8fa99ec1c7a97bffd4792d198ebf9baef598326fdf14fdbf254e0c3f6f84f53a50398736d1b508bcaa1fb3682f4bd176c9c99bf5311d8b33ee5a15026e
|
data/CHANGES.md
CHANGED
data/README.md
CHANGED
@@ -14,7 +14,8 @@ Ready?
|
|
14
14
|
|
15
15
|
# Inheritable Class Attributes
|
16
16
|
|
17
|
-
|
17
|
+
If you want inherited class attributes, this is for you.
|
18
|
+
This is a mandatory mechanism for creating DSLs.
|
18
19
|
|
19
20
|
```ruby
|
20
21
|
require 'uber/inheritable_attr'
|
@@ -27,15 +28,13 @@ class Song
|
|
27
28
|
end
|
28
29
|
```
|
29
30
|
|
30
|
-
Note that you have to initialize your attribute
|
31
|
-
|
32
|
-
You can now use that attribute on the class level.
|
31
|
+
Note that you have to initialize your class attribute with whatever you want - usually a hash or an array.
|
33
32
|
|
34
33
|
```ruby
|
35
34
|
Song.properties #=> [:title, :track]
|
36
35
|
```
|
37
36
|
|
38
|
-
|
37
|
+
A subclass of `Song` will have a `clone`d `properties` class attribute.
|
39
38
|
|
40
39
|
```ruby
|
41
40
|
class Hit < Song
|
@@ -44,7 +43,7 @@ end
|
|
44
43
|
Hit.properties #=> [:title, :track]
|
45
44
|
```
|
46
45
|
|
47
|
-
The cool thing about the inheritance is: you can work on the inherited attribute without any restrictions
|
46
|
+
The cool thing about the inheritance is: you can work on the inherited attribute without any restrictions. It is a _copy_ of the original.
|
48
47
|
|
49
48
|
```ruby
|
50
49
|
Hit.properties << :number
|
@@ -53,10 +52,10 @@ Hit.properties #=> [:title, :track, :number]
|
|
53
52
|
Song.properties #=> [:title, :track]
|
54
53
|
```
|
55
54
|
|
56
|
-
It's similar to ActiveSupport's `class_attribute` but with a simpler implementation
|
57
|
-
|
58
|
-
This module is very popular amongst numerous gems like Cells, Representable, Roar and Reform.
|
55
|
+
It's similar to ActiveSupport's `class_attribute` but with a simpler implementation.
|
56
|
+
It is less dangerous. There are no restrictions for modifying the attribute. [compared to `class_attribute`](http://apidock.com/rails/v4.0.2/Class/class_attribute).
|
59
57
|
|
58
|
+
This module is used across several other gems like [Cells](https://rubygems.org/gems/cells), [Representable](https://rubygems.org/gems/representable), [Roar](https://rubygems.org/gems/roar) and [Reform](https://rubygems.org/gems/reform).
|
60
59
|
|
61
60
|
# Dynamic Options
|
62
61
|
|
@@ -274,4 +273,4 @@ version.~ "1.1", "1.2" #=> true
|
|
274
273
|
|
275
274
|
Copyright (c) 2014 by Nick Sutterer <apotonick@gmail.com>
|
276
275
|
|
277
|
-
|
276
|
+
Uber is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
@@ -7,14 +7,29 @@ module Uber
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def #{name}
|
10
|
-
@#{name}
|
10
|
+
return @#{name} if instance_variable_defined?(:@#{name})
|
11
|
+
@#{name} = InheritableAttribute.inherit_for(self, :#{name})
|
11
12
|
end
|
12
13
|
}
|
13
14
|
end
|
14
15
|
|
15
16
|
def self.inherit_for(klass, name)
|
16
|
-
return unless klass.superclass.respond_to?(name)
|
17
|
-
|
17
|
+
return unless klass.superclass.respond_to?(name)
|
18
|
+
|
19
|
+
value = klass.superclass.send(name) # could be nil.
|
20
|
+
Clone.(value) # this could be dynamic, allowing other inheritance strategies.
|
21
|
+
end
|
22
|
+
|
23
|
+
class Clone
|
24
|
+
# The second argument allows injecting more types.
|
25
|
+
def self.call(value, uncloneable=uncloneable)
|
26
|
+
uncloneable.each { |klass| return value if value.kind_of?(klass) }
|
27
|
+
value.clone
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.uncloneable
|
31
|
+
[Symbol, TrueClass, FalseClass, NilClass]
|
32
|
+
end
|
18
33
|
end
|
19
34
|
end
|
20
35
|
|
data/lib/uber/uber_version.rb
CHANGED
@@ -7,9 +7,14 @@ class InheritableAttrTest < MiniTest::Spec
|
|
7
7
|
Class.new(Object) do
|
8
8
|
extend Uber::InheritableAttribute
|
9
9
|
inheritable_attr :drinks
|
10
|
+
inheritable_attr :glass
|
10
11
|
end
|
11
12
|
}
|
12
13
|
|
14
|
+
def assert_nothing_raised(*)
|
15
|
+
yield
|
16
|
+
end
|
17
|
+
|
13
18
|
it "provides a reader with empty inherited attributes, already" do
|
14
19
|
assert_equal nil, subject.drinks
|
15
20
|
end
|
@@ -50,5 +55,26 @@ class InheritableAttrTest < MiniTest::Spec
|
|
50
55
|
subklass.drinks = [:merlot] # we only want merlot explicitely.
|
51
56
|
assert_equal [:merlot], subklass.drinks # no :cabernet, here
|
52
57
|
end
|
58
|
+
|
59
|
+
it "does not attempt to clone symbols" do
|
60
|
+
subject.glass = :highball
|
61
|
+
subklass = Class.new(subject)
|
62
|
+
|
63
|
+
subklass.glass.must_equal :highball
|
64
|
+
end
|
65
|
+
|
66
|
+
it "does not attempt to clone true" do
|
67
|
+
subject.glass = true
|
68
|
+
subklass = Class.new(subject)
|
69
|
+
|
70
|
+
subklass.glass.must_equal true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "does not attempt to clone false" do
|
74
|
+
subject.glass = false
|
75
|
+
subklass = Class.new(subject)
|
76
|
+
|
77
|
+
subklass.glass.must_equal false
|
78
|
+
end
|
53
79
|
end
|
54
80
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: uber
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Nick Sutterer
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10
|
11
|
+
date: 2014-11-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rake
|