proc-value 1.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/.gitignore +17 -0
- data/.travis.yml +8 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +54 -0
- data/Rakefile +5 -0
- data/lib/proc/value.rb +52 -0
- data/proc-value.gemspec +24 -0
- data/spec/proc_value_spec.rb +68 -0
- data/spec/spec_helper.rb +13 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 216ae423164f4d842e72e576fc515b67bd7eceb7
|
4
|
+
data.tar.gz: 681156a9c901b568009652ff6348ec116904f1e2
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8e1ee349825a2d38c52eca137c65228f7738aa526d05beba6ad3e67d6772cfa6c4d40154bf794a859f7205380ef734deac950515631c18411be43864d10f75b2
|
7
|
+
data.tar.gz: a3ed8d87926c5cdd020d8c0763a2a45c655aff8a21c37f9f870d15beabd88417716272b6bffe8eaf8e2fd1341a4bd2c2d419bbd6cdf335cca8f7f45f69b4d20f
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Jan Graichen
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
# Proc::Value [](http://badge.fury.io/rb/proc-value)
|
2
|
+
[](https://travis-ci.org/jgraichen/proc-value)
|
3
|
+
[](https://coveralls.io/r/jgraichen/proc-value)
|
4
|
+
|
5
|
+
**Proc::Value** extends Ruby Objects with a Smalltalk like `#value` method that always returns the object except when called on a block. In this case the block will be evaluated and the result returned. This allows the transparent exchange of static object with lazy evaluated blocks e.g. for configuration classes.
|
6
|
+
|
7
|
+
```ruby
|
8
|
+
class Configuration
|
9
|
+
def initialize(options)
|
10
|
+
@options = options
|
11
|
+
end
|
12
|
+
|
13
|
+
def path
|
14
|
+
@options[:path].value.to_s
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
config1 = Configuration.new path: proc { "config/#{RAILS_ENV}/config.yml" }
|
19
|
+
config2 = Configuration.new path: 'config/config.yml'
|
20
|
+
|
21
|
+
config1.path # `path` block will be evaluated here!
|
22
|
+
# => "config/development/config.yml"
|
23
|
+
config2.path # Same code. No extra if statement needed.
|
24
|
+
# => "config/config.yml"
|
25
|
+
```
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
gem 'proc-value'
|
32
|
+
|
33
|
+
And then execute:
|
34
|
+
|
35
|
+
$ bundle
|
36
|
+
|
37
|
+
Or install it yourself as:
|
38
|
+
|
39
|
+
$ gem install proc-value
|
40
|
+
|
41
|
+
## Usage
|
42
|
+
|
43
|
+
See example above.
|
44
|
+
|
45
|
+
You can also use `Proc.val(proc_or_object)` that does not depend on any injected method and only looks if given object implements a `#call` method or not.
|
46
|
+
|
47
|
+
## Contributing
|
48
|
+
|
49
|
+
1. Fork it
|
50
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
51
|
+
3. Add specs for your feature
|
52
|
+
4. Add and commit your changes (`git commit -am 'Add some feature'`)
|
53
|
+
5. Push to the branch (`git push origin my-new-feature`)
|
54
|
+
6. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/proc/value.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
class Proc
|
2
|
+
module ValueClassPatch
|
3
|
+
# Evaluate block if block is given or return
|
4
|
+
# unmodified object if no block is given. Additional
|
5
|
+
# arguments are given to `Proc#call`.
|
6
|
+
#
|
7
|
+
# Can be used to allow transparent usage of blocks.
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# config_option = Proc.val(config_option).to_s
|
11
|
+
#
|
12
|
+
# @see Object#value
|
13
|
+
# @see Proc#value
|
14
|
+
#
|
15
|
+
def val(proc_or_object, *args)
|
16
|
+
proc_or_object.respond_to?(:call) ? proc_or_object.call(*args) : proc_or_object
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
module ValueInstancePatch
|
21
|
+
# Evaluate block. Arguments are given to `Proc#call`.
|
22
|
+
#
|
23
|
+
# @example
|
24
|
+
# config_option = proc { "#{lazy_var}/path/to/file.rb" }
|
25
|
+
# config_option.value.to_s\
|
26
|
+
#
|
27
|
+
def value(*args)
|
28
|
+
send :call, *args
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
module ValueObjectPatch
|
33
|
+
# Return self. Allows object to be transparently switched
|
34
|
+
# with blocks.
|
35
|
+
#
|
36
|
+
# @example
|
37
|
+
# config_option = "path/to/file.rb"
|
38
|
+
# config_option.value.to_s
|
39
|
+
#
|
40
|
+
# config_option = proc { "#{lazy_var}/path/to/file.rb" }
|
41
|
+
# config_option.value.to_s
|
42
|
+
#
|
43
|
+
def value(*args)
|
44
|
+
self
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
include ValueInstancePatch
|
49
|
+
extend ValueClassPatch
|
50
|
+
end
|
51
|
+
|
52
|
+
Object.send :include, Proc::ValueObjectPatch
|
data/proc-value.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)\
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'proc-value'
|
7
|
+
spec.version = '1.0'
|
8
|
+
spec.authors = ['Jan Graichen']
|
9
|
+
spec.email = ['jg@altimos.de']
|
10
|
+
spec.description = %q{Proc::Value allows transparent block evaluation on all objects using #value method.}
|
11
|
+
spec.summary = %q{Proc::Value allows transparent block evaluation on all objects using #value method.}
|
12
|
+
spec.homepage = 'https://github.com/jgraichen/proc-value'
|
13
|
+
spec.license = 'MIT'
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
21
|
+
spec.add_development_dependency "rake"
|
22
|
+
spec.add_development_dependency "rspec"
|
23
|
+
spec.add_development_dependency "coveralls"
|
24
|
+
end
|
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'Proc::Value' do
|
4
|
+
|
5
|
+
it 'should allow transparent usage of blocks' do
|
6
|
+
option1 = 'path/to/file.rb'
|
7
|
+
expect(option1.value.to_s).to be == 'path/to/file.rb'
|
8
|
+
|
9
|
+
option2 = proc { 'path/to/file.rb' }
|
10
|
+
expect(option2.value.to_s).to be == 'path/to/file.rb'
|
11
|
+
|
12
|
+
option3 = lambda { 'path/to/file.rb' }
|
13
|
+
expect(option3.value.to_s).to be == 'path/to/file.rb'
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'Object' do
|
17
|
+
let(:sample) { Object.new }
|
18
|
+
subject { sample }
|
19
|
+
|
20
|
+
it 'should have #value method' do
|
21
|
+
expect(subject).to respond_to(:value)
|
22
|
+
end
|
23
|
+
|
24
|
+
context '#value' do
|
25
|
+
subject { sample.value }
|
26
|
+
|
27
|
+
it 'should return same object' do
|
28
|
+
expect(subject).to be === subject
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
context 'Proc' do
|
34
|
+
let(:sample) { Object.new }
|
35
|
+
let(:block) { proc { sample } }
|
36
|
+
subject { block }
|
37
|
+
|
38
|
+
it 'should have #value method' do
|
39
|
+
expect(subject).to respond_to(:value)
|
40
|
+
end
|
41
|
+
|
42
|
+
context '#value' do
|
43
|
+
subject { block.value }
|
44
|
+
|
45
|
+
it 'should return block result' do
|
46
|
+
expect(subject).to be === sample
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context '.val' do
|
51
|
+
context 'with block' do
|
52
|
+
subject { Proc.val(block) }
|
53
|
+
|
54
|
+
it 'should evaluate block' do
|
55
|
+
expect(subject).to be === sample
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'with object' do
|
60
|
+
subject { Proc.val(sample) }
|
61
|
+
|
62
|
+
it 'should evaluate block' do
|
63
|
+
expect(subject).to be === sample
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: proc-value
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '1.0'
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jan Graichen
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-03 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ~>
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.3'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ~>
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.3'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: coveralls
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: 'Proc::Value allows transparent block evaluation on all objects using
|
70
|
+
#value method.'
|
71
|
+
email:
|
72
|
+
- jg@altimos.de
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- .gitignore
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- lib/proc/value.rb
|
84
|
+
- proc-value.gemspec
|
85
|
+
- spec/proc_value_spec.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
homepage: https://github.com/jgraichen/proc-value
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata: {}
|
91
|
+
post_install_message:
|
92
|
+
rdoc_options: []
|
93
|
+
require_paths:
|
94
|
+
- lib
|
95
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
96
|
+
requirements:
|
97
|
+
- - '>='
|
98
|
+
- !ruby/object:Gem::Version
|
99
|
+
version: '0'
|
100
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
101
|
+
requirements:
|
102
|
+
- - '>='
|
103
|
+
- !ruby/object:Gem::Version
|
104
|
+
version: '0'
|
105
|
+
requirements: []
|
106
|
+
rubyforge_project:
|
107
|
+
rubygems_version: 2.0.3
|
108
|
+
signing_key:
|
109
|
+
specification_version: 4
|
110
|
+
summary: 'Proc::Value allows transparent block evaluation on all objects using #value
|
111
|
+
method.'
|
112
|
+
test_files:
|
113
|
+
- spec/proc_value_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
has_rdoc:
|