uber 0.0.13 → 0.0.14

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9ba4151c41861ad82f7e78103bd2c45b6cb0fc6e
4
- data.tar.gz: ef674c49120eb7f86930e11ab8a12ac5bb3192e1
3
+ metadata.gz: f71a66b9f3f5b51c0ceb68c2aa396d9f1571d28b
4
+ data.tar.gz: 243f81afa103da2cc3b1c2d8f9d1445b9acf8013
5
5
  SHA512:
6
- metadata.gz: 78ff679013edac4b297e88352768e69d0daa56fc02d8491bd3fc6fb463c7336f3f8ef95fc9c667ba1c8a22af1f2b8c4b8b55821e4306d2558b858550eca89058
7
- data.tar.gz: 2a3c2906155aa3c85ada42ba320040a19ad7165be9b695ad98e672c200a8e82b13b30fb4d3a8f91f4fef3ffd0817c0057c847ed5336b5324040411489f41eda9
6
+ metadata.gz: c67f0fe6bf6fe588af3848e9a243f4fe5ca2857468c7ccdf9e0100e3a94b717a923e395ec77c2cc31edb301593663f8de6483f06f7cfb6cf53fb603cea1b43a9
7
+ data.tar.gz: 4c3ef47a30140dc3191d582e678a28362477ec0786ab30194f9434e26ced92bc12ce937385a7848918e715b22218ea7c12329e17bb5f64e65d171f85dd48d730
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ # 0.0.14
2
+
3
+ * Add `inheritable_attr :title, clone: false`. Thanks to @katafrakt.
4
+ * When calling `Option::Value#evaluate` with `nil` as context, and the `Value` represents a lambda, the block is called it its original context via `block.call`.
5
+
1
6
  # 0.0.13
2
7
 
3
8
  * Add proc syntax for builders. This allows using `return` in the block. Thanks to @dutow for implementing this.
data/README.md CHANGED
@@ -4,6 +4,8 @@ _Gem-authoring tools like class method inheritance in modules, dynamic options a
4
4
 
5
5
  ## Installation
6
6
 
7
+ [![Gem Version](https://badge.fury.io/rb/uber.svg)](http://badge.fury.io/rb/uber)
8
+
7
9
  Add this line to your application's Gemfile:
8
10
 
9
11
  ```ruby
@@ -55,7 +57,20 @@ Song.properties #=> [:title, :track]
55
57
  It's similar to ActiveSupport's `class_attribute` but with a simpler implementation.
56
58
  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).
57
59
 
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
+ ## Uncloneable Values
61
+
62
+ `::inheritable_attr` will `clone` values to copy them to subclasses. Uber won't attempt to clone `Symbol`, `nil`, `true` and `false` per default.
63
+
64
+ If you assign any other unclonable value you need to tell Uber that.
65
+
66
+ ```ruby
67
+ class Song
68
+ extend Uber::InheritableAttr
69
+ inheritable_attr :properties, clone: false
70
+ ```
71
+
72
+ This won't `clone` but simply pass the value on to the subclass.
73
+
59
74
 
60
75
  # Dynamic Options
61
76
 
@@ -152,6 +167,16 @@ value.evaluate(context, -122.18) #=> 0
152
167
 
153
168
  Use `Options::Value#evaluate` to handle single values.
154
169
 
170
+ If the `Value` represents a lambda and is `evaluate`d with `nil` as context, the block is called in the original context.
171
+
172
+ ```ruby
173
+ volume = 99
174
+ value = Uber::Options::Value.new(lambda { volume })
175
+
176
+ value.evaluate(nil) #=> 99
177
+ ```
178
+
179
+
155
180
  ## Performance
156
181
 
157
182
  Evaluating an options hash can be time-consuming. When `Options` contains static elements only, it behaves *and performs* like an ordinary hash.
@@ -1,6 +1,7 @@
1
1
  module Uber
2
2
  module InheritableAttr
3
- def inheritable_attr(name)
3
+
4
+ def inheritable_attr(name, options={})
4
5
  instance_eval %Q{
5
6
  def #{name}=(v)
6
7
  @#{name} = v
@@ -8,15 +9,17 @@ module Uber
8
9
 
9
10
  def #{name}
10
11
  return @#{name} if instance_variable_defined?(:@#{name})
11
- @#{name} = InheritableAttribute.inherit_for(self, :#{name})
12
+ @#{name} = InheritableAttribute.inherit_for(self, :#{name}, #{options})
12
13
  end
13
14
  }
14
15
  end
15
16
 
16
- def self.inherit_for(klass, name)
17
+ def self.inherit_for(klass, name, options={})
17
18
  return unless klass.superclass.respond_to?(name)
18
19
 
19
- value = klass.superclass.send(name) # could be nil.
20
+ value = klass.superclass.send(name) # could be nil
21
+
22
+ return value if options[:clone] == false
20
23
  Clone.(value) # this could be dynamic, allowing other inheritance strategies.
21
24
  end
22
25
 
@@ -64,6 +64,18 @@ module Uber
64
64
  @dynamic
65
65
  end
66
66
 
67
+ def proc?
68
+ @value.kind_of?(Proc)
69
+ end
70
+
71
+ def callable?
72
+ @value.is_a?(Uber::Callable)
73
+ end
74
+
75
+ def method?
76
+ @value.is_a?(Symbol)
77
+ end
78
+
67
79
  private
68
80
  def evaluate_for(*args)
69
81
  return proc!(*args) if @proc
@@ -77,25 +89,17 @@ module Uber
77
89
  end
78
90
 
79
91
  def proc!(context, *args)
80
- context.instance_exec(*args, &@value)
92
+ if context.nil?
93
+ @value.call(*args)
94
+ else
95
+ context.instance_exec(*args, &@value)
96
+ end
81
97
  end
82
98
 
83
99
  # Callable object is executed in its original context.
84
100
  def callable!(context, *args)
85
101
  @value.call(context, *args)
86
102
  end
87
-
88
- def proc?
89
- @value.kind_of?(Proc)
90
- end
91
-
92
- def callable?
93
- @value.is_a?(Uber::Callable)
94
- end
95
-
96
- def method?
97
- @value.is_a?(Symbol)
98
- end
99
103
  end
100
104
  end
101
105
  end
@@ -1,3 +1,3 @@
1
1
  module Uber
2
- VERSION = "0.0.13"
2
+ VERSION = "0.0.14"
3
3
  end
@@ -8,6 +8,7 @@ class InheritableAttrTest < MiniTest::Spec
8
8
  extend Uber::InheritableAttribute
9
9
  inheritable_attr :drinks
10
10
  inheritable_attr :glass
11
+ inheritable_attr :guests, clone: false
11
12
  end
12
13
  }
13
14
 
@@ -56,6 +57,16 @@ class InheritableAttrTest < MiniTest::Spec
56
57
  assert_equal [:merlot], subklass.drinks # no :cabernet, here
57
58
  end
58
59
 
60
+ it "respects clone: false" do
61
+ subject.guests = 2
62
+ subklass_a = Class.new(subject)
63
+ subklass_b = Class.new(subject)
64
+ subklass_a.guests = 13
65
+
66
+ assert_equal 2, subklass_b.guests
67
+ assert_equal 13, subklass_a.guests
68
+ end
69
+
59
70
  it "does not attempt to clone symbols" do
60
71
  subject.glass = :highball
61
72
  subklass = Class.new(subject)
@@ -36,6 +36,8 @@ class UberOptionTest < MiniTest::Spec
36
36
  it { Value.new(:version).evaluate(object.extend(version)).must_equal 999 }
37
37
  it { Value.new("version", :dynamic => true).evaluate(object.extend(version)).must_equal 999 }
38
38
  it { Value.new(:version, :dynamic => false).evaluate(object.extend(version)).must_equal :version }
39
+ it { Value.new(lambda { self }).evaluate(object).must_equal object }
40
+ it { Value.new(lambda { self }).evaluate(nil).must_equal self }
39
41
 
40
42
  it { Value.new(lambda { :loud }, :dynamic => true).evaluate(object).must_equal :loud }
41
43
 
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.13
4
+ version: 0.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick Sutterer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-14 00:00:00.000000000 Z
11
+ date: 2015-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -89,7 +89,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
89
  version: '0'
90
90
  requirements: []
91
91
  rubyforge_project:
92
- rubygems_version: 2.2.2
92
+ rubygems_version: 2.4.8
93
93
  signing_key:
94
94
  specification_version: 4
95
95
  summary: Gem-authoring tools like class method inheritance in modules, dynamic options
@@ -103,3 +103,4 @@ test_files:
103
103
  - test/test_helper.rb
104
104
  - test/version_test.rb
105
105
  - test/zeugs.rb
106
+ has_rdoc: