uber 0.0.6 → 0.0.7

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c041fd887390fe14c2105874c29871320872be44
4
- data.tar.gz: 255a4728ff3a6e9afd5b37abdc636f69176c6b1e
3
+ metadata.gz: 8b1c72fd6f3f99e2d92ee6bba306785d88b64963
4
+ data.tar.gz: 01443d5ec98cee53824c305ffa5203030849b33a
5
5
  SHA512:
6
- metadata.gz: 920346ed6d820f7ec4d85f9876498a92a98cae168749ef55cb1b7c2a8ad07c1cc897276e4a6bb3f775791e4b52355c398f7ee5798e5b9c04cef5b5f6d61a2b64
7
- data.tar.gz: d7fd5fae927d359a8763bf79557161c688833b4a1fe2f7356a18d1bdbdd13e7c9d76bc2e4dc2d2318d39f0ebbd009592a5d43de94eded447103702d17d4624ba
6
+ metadata.gz: 39ccf2b0c211ae18559d825ae4613c450e2c38b63a692d5ad86b5da51f33f32bc05100869f0ecdcadce2888d01be5af3017bee1db16bbc2eef9b31d1b455e2fa
7
+ data.tar.gz: 11e6460fafc4153976248595236e08bc543c11ba32252bfe0708b454c32b8ba335a01f5c7c149742869ff0c89b2bf1f313cb188746cb0d17d07056c141ddae37
data/CHANGES.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.0.7
2
+
3
+ * Add `Uber::Callable` and support for it in `Options::Value`.
4
+
1
5
  # 0.0.6
2
6
 
3
7
  * Fix `Version#>=` partially.
data/README.md CHANGED
@@ -108,9 +108,32 @@ The default behaviour is to treat `Proc`s, lambdas and symbolized `:method` name
108
108
 
109
109
  This is a pattern well-known from Rails and other frameworks.
110
110
 
111
+ ## Uber::Callable
112
+
113
+ A third way of providing a dynamic option is using a "callable" object. This saves you the unreadable lambda syntax and gives you more flexibility.
114
+
115
+ ```ruby
116
+ require 'uber/callable'
117
+ class Tags
118
+ include Uber::Callable
119
+
120
+ def call(context, *args)
121
+ [:comment]
122
+ end
123
+ end
124
+ ```
125
+
126
+ By including `Uber::Callable`, uber will invoke the `#call` method on the specified object.
127
+
128
+ Note how you simply pass an instance of the callable object into the hash instead of a lambda.
129
+
130
+ ```ruby
131
+ options = Uber::Options.new(tags: Tags.new)
132
+ ```
133
+
111
134
  ## Evaluating Elements
112
135
 
113
- If you wanna evaluate a single option element, use `#eval`.
136
+ If you want to evaluate a single option element, use `#eval`.
114
137
 
115
138
  ```ruby
116
139
  options.eval(:ttl, user) #=> "n/a"
@@ -0,0 +1,7 @@
1
+ module Uber
2
+ # Include this module into a class or extend an object to mark it as callable.
3
+ # E.g., in a dynamic option in Options::Value it will be treated like a Proc object
4
+ # and invoked with +#call+.
5
+ module Callable
6
+ end
7
+ end
data/lib/uber/options.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require 'uber/callable'
2
+
1
3
  module Uber
2
4
  class Options < Hash
3
5
  def initialize(options)
@@ -43,13 +45,13 @@ module Uber
43
45
  def initialize(value, options={})
44
46
  @value, @dynamic = value, options[:dynamic]
45
47
 
46
- @callable = true if @value.kind_of?(Proc)
48
+ @proc = proc?
49
+ @callable = callable?
50
+ @method = method?
47
51
 
48
52
  return if options.has_key?(:dynamic)
49
53
 
50
- # conventional behaviour:
51
- @dynamic = true if @callable
52
- @dynamic = true if @value.is_a?(Symbol)
54
+ @dynamic = @proc || @callable || @method
53
55
  end
54
56
 
55
57
  def evaluate(context, *args)
@@ -64,9 +66,10 @@ module Uber
64
66
 
65
67
  private
66
68
  def evaluate_for(*args)
67
- return method!(*args) unless callable?
69
+ return proc!(*args) if @proc
70
+ return callable!(*args) if @callable
71
+ method!(*args)
68
72
  # TODO: change to context.instance_exec and deprecate first argument.
69
- proc!(*args)
70
73
  end
71
74
 
72
75
  def method!(context, *args)
@@ -77,8 +80,21 @@ module Uber
77
80
  context.instance_exec(*args, &@value)
78
81
  end
79
82
 
83
+ # Callable object is executed in its original context.
84
+ def callable!(context, *args)
85
+ @value.call(context, *args)
86
+ end
87
+
88
+ def proc?
89
+ @value.kind_of?(Proc)
90
+ end
91
+
80
92
  def callable?
81
- @callable
93
+ @value.is_a?(Uber::Callable)
94
+ end
95
+
96
+ def method?
97
+ @value.is_a?(Symbol)
82
98
  end
83
99
  end
84
100
  end
@@ -1,3 +1,3 @@
1
1
  module Uber
2
- VERSION = "0.0.6"
2
+ VERSION = "0.0.7"
3
3
  end
@@ -21,6 +21,9 @@ class InheritanceTest < MiniTest::Spec
21
21
 
22
22
  CODE_BLOCK = lambda { |base| base.class_eval { extend ClassMethods } } # i want that to be executed at every include
23
23
 
24
+ instance_exec do
25
+ @block = CODE_BLOCK # this would happen in inherited_included do .. end
26
+ end
24
27
 
25
28
  def self.included(includer) #
26
29
  # CODE_BLOCK.call(base)
data/test/options_test.rb CHANGED
@@ -5,16 +5,24 @@ class UberOptionTest < MiniTest::Spec
5
5
  Value = Uber::Options::Value
6
6
  let (:object) { Object.new }
7
7
 
8
+ class Callable
9
+ include Uber::Callable
10
+ def call(*); 999 end
11
+ end
12
+
8
13
  describe "#dynamic?" do
9
- it { Value.new(1).dynamic?.must_equal nil }
10
- it { Value.new(true).dynamic?.must_equal nil }
11
- it { Value.new("loud").dynamic?.must_equal nil }
14
+ it { Value.new(1).dynamic?.must_equal false }
15
+ it { Value.new(true).dynamic?.must_equal false }
16
+ it { Value.new("loud").dynamic?.must_equal false }
12
17
  it { Value.new(:loud, :dynamic => false).dynamic?.must_equal false }
13
18
  it { Value.new("loud", :dynamic => true).dynamic?.must_equal true }
14
19
 
15
20
  it { Value.new(lambda {}).dynamic?.must_equal true }
16
21
  it { Value.new(Proc.new{}).dynamic?.must_equal true }
17
22
  it { Value.new(:method).dynamic?.must_equal true }
23
+
24
+ # Uber::Callable
25
+ it { Value.new(Callable.new).dynamic?.must_equal true }
18
26
  end
19
27
 
20
28
  describe "#evaluate" do
@@ -30,14 +38,19 @@ class UberOptionTest < MiniTest::Spec
30
38
  it { Value.new(:version, :dynamic => false).evaluate(object.extend(version)).must_equal :version }
31
39
 
32
40
  it { Value.new(lambda { :loud }, :dynamic => true).evaluate(object).must_equal :loud }
41
+
42
+ # Uber::Callable
43
+ it { Value.new(Callable.new).evaluate(nil).must_equal 999 }
33
44
  end
34
45
 
35
46
  describe "passing options" do
36
47
  let (:version) { Module.new { def version(*args); args.inspect end } }
37
48
  let (:block) { Proc.new { |*args| args.inspect } }
49
+ let (:callable) { (Class.new { include Uber::Callable; def call(*args); args.inspect; end }).new }
38
50
 
39
51
  it { Value.new(:version).evaluate(object.extend(version), 1, 2, 3).must_equal "[1, 2, 3]" }
40
52
  it { Value.new(block).evaluate(object, 1, 2, 3).must_equal "[1, 2, 3]" }
53
+ it { Value.new(callable).evaluate(Object, 1, 2, 3).must_equal "[Object, 1, 2, 3]" }
41
54
  end
42
55
 
43
56
  # it "speed" do
metadata CHANGED
@@ -1,41 +1,41 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: uber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
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-05-08 00:00:00.000000000 Z
11
+ date: 2014-07-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ">="
17
+ - - '>='
18
18
  - !ruby/object:Gem::Version
19
19
  version: 0.10.1
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ">="
24
+ - - '>='
25
25
  - !ruby/object:Gem::Version
26
26
  version: 0.10.1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ">="
31
+ - - '>='
32
32
  - !ruby/object:Gem::Version
33
33
  version: 5.0.0
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ">="
38
+ - - '>='
39
39
  - !ruby/object:Gem::Version
40
40
  version: 5.0.0
41
41
  description: A gem-authoring framework.
@@ -45,13 +45,14 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - ".gitignore"
48
+ - .gitignore
49
49
  - CHANGES.md
50
50
  - Gemfile
51
51
  - LICENSE
52
52
  - README.md
53
53
  - Rakefile
54
54
  - lib/uber.rb
55
+ - lib/uber/callable.rb
55
56
  - lib/uber/inheritable_attr.rb
56
57
  - lib/uber/options.rb
57
58
  - lib/uber/uber_version.rb
@@ -73,17 +74,17 @@ require_paths:
73
74
  - lib
74
75
  required_ruby_version: !ruby/object:Gem::Requirement
75
76
  requirements:
76
- - - ">="
77
+ - - '>='
77
78
  - !ruby/object:Gem::Version
78
79
  version: '0'
79
80
  required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  requirements:
81
- - - ">="
82
+ - - '>='
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
84
85
  requirements: []
85
86
  rubyforge_project:
86
- rubygems_version: 2.2.1
87
+ rubygems_version: 2.2.2
87
88
  signing_key:
88
89
  specification_version: 4
89
90
  summary: Gem-authoring tools like class method inheritance in modules, dynamic options
@@ -95,4 +96,3 @@ test_files:
95
96
  - test/test_helper.rb
96
97
  - test/version_test.rb
97
98
  - test/zeugs.rb
98
- has_rdoc: