lotus-utils 0.1.1 → 0.2.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.
@@ -0,0 +1,149 @@
1
+ require 'lotus/utils/kernel'
2
+
3
+ module Lotus
4
+ module Utils
5
+ # A collection of loading paths.
6
+ #
7
+ # @since 0.2.0
8
+ class LoadPaths
9
+ # Initialize a new collection for the given paths
10
+ #
11
+ # @param paths [String, Pathname, Array<String>, Array<Pathname>] A single
12
+ # or a collection of objects that can be converted into a Pathname
13
+ #
14
+ # @return [Lotus::Utils::LoadPaths] self
15
+ #
16
+ # @since 0.2.0
17
+ #
18
+ # @see http://ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
19
+ # @see Lotus::Utils::Kernel.Pathname
20
+ def initialize(*paths)
21
+ @paths = Array(paths)
22
+ end
23
+
24
+ # It specifies the policy for initialize copies of the object, when #clone
25
+ # or #dup are invoked.
26
+ #
27
+ # @api private
28
+ # @since 0.2.0
29
+ #
30
+ # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-clone
31
+ # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-dup
32
+ #
33
+ # @example
34
+ # require 'lotus/utils/load_paths'
35
+ #
36
+ # paths = Lotus::Utils::LoadPaths.new '.'
37
+ # paths2 = paths.dup
38
+ #
39
+ # paths << '..'
40
+ # paths2 << '../..'
41
+ #
42
+ # paths
43
+ # # => #<Lotus::Utils::LoadPaths:0x007f84e0cad430 @paths=[".", ".."]>
44
+ #
45
+ # paths2
46
+ # # => #<Lotus::Utils::LoadPaths:0x007faedc4ad3e0 @paths=[".", "../.."]>
47
+ def initialize_copy(original)
48
+ @paths = original.instance_variable_get(:@paths).dup
49
+ end
50
+
51
+ # Iterates thru the collection and yields the given block.
52
+ # It skips duplications and raises an error in case one of the paths
53
+ # doesn't exist.
54
+ #
55
+ # @param blk [Proc] the block of code to be yielded
56
+ #
57
+ # @return [void]
58
+ #
59
+ # @raise [Errno::ENOENT] if one of the paths doesn't exist
60
+ #
61
+ # @since 0.2.0
62
+ def each(&blk)
63
+ Utils::Kernel.Array(@paths).each do |path|
64
+ blk.call realpath(path)
65
+ end
66
+ end
67
+
68
+ # Adds the given path(s).
69
+ #
70
+ # It returns self, so that multiple operations can be performed.
71
+ #
72
+ # @param paths [String, Pathname, Array<String>, Array<Pathname>] A single
73
+ # or a collection of objects that can be converted into a Pathname
74
+ #
75
+ # @return [Lotus::Utils::LoadPaths] self
76
+ #
77
+ # @raise [RuntimeError] if the object was previously frozen
78
+ #
79
+ # @since 0.2.0
80
+ #
81
+ # @see http://ruby-doc.org/stdlib-2.1.2/libdoc/pathname/rdoc/Pathname.html
82
+ # @see Lotus::Utils::Kernel.Pathname
83
+ # @see Lotus::Utils::LoadPaths#freeze
84
+ #
85
+ # @example Basic usage
86
+ # require 'lotus/utils/load_paths'
87
+ #
88
+ # paths = Lotus::Utils::LoadPaths.new
89
+ # paths.push '.'
90
+ # paths.push '..', '../..'
91
+ #
92
+ # @example Chainable calls
93
+ # require 'lotus/utils/load_paths'
94
+ #
95
+ # paths = Lotus::Utils::LoadPaths.new
96
+ # paths.push('.')
97
+ # .push('..', '../..')
98
+ #
99
+ # @example Shovel alias (#<<)
100
+ # require 'lotus/utils/load_paths'
101
+ #
102
+ # paths = Lotus::Utils::LoadPaths.new
103
+ # paths << '.'
104
+ # paths << ['..', '../..']
105
+ #
106
+ # @example Chainable calls with shovel alias (#<<)
107
+ # require 'lotus/utils/load_paths'
108
+ #
109
+ # paths = Lotus::Utils::LoadPaths.new
110
+ # paths << '.' << '../..'
111
+ def push(*paths)
112
+ @paths.push(*paths)
113
+ self
114
+ end
115
+
116
+ alias_method :<<, :push
117
+
118
+ # It freezes the object by preventing further modifications.
119
+ #
120
+ # @since 0.2.0
121
+ #
122
+ # @see http://ruby-doc.org/core-2.1.2/Object.html#method-i-freeze
123
+ #
124
+ # @example
125
+ # require 'lotus/utils/load_paths'
126
+ #
127
+ # paths = Lotus::Utils::LoadPaths.new
128
+ # paths.freeze
129
+ #
130
+ # paths.frozen? # => true
131
+ #
132
+ # paths.push '.' # => RuntimeError
133
+ def freeze
134
+ super
135
+ @paths.freeze
136
+ end
137
+
138
+ private
139
+ # Allow subclasses to define their own policy to discover the realpath
140
+ # of the given path.
141
+ #
142
+ # @since 0.2.0
143
+ # @api private
144
+ def realpath(path)
145
+ Utils::Kernel.Pathname(path).realpath
146
+ end
147
+ end
148
+ end
149
+ end
@@ -78,6 +78,24 @@ module Lotus
78
78
  split(NAMESPACE_SEPARATOR).last
79
79
  end
80
80
 
81
+ # Return the top level namespace name
82
+ #
83
+ # @return [String] the transformed string
84
+ #
85
+ # @since 0.1.2
86
+ #
87
+ # @example
88
+ # require 'lotus/utils/string'
89
+ #
90
+ # string = Lotus::Utils::String.new 'Lotus::Utils::String'
91
+ # string.namespace # => 'Lotus'
92
+ #
93
+ # string = Lotus::Utils::String.new 'String'
94
+ # string.namespace # => 'String'
95
+ def namespace
96
+ split(NAMESPACE_SEPARATOR).first
97
+ end
98
+
81
99
  # It iterates thru the tokens and calls the given block.
82
100
  # A token is a substring wrapped by `()` and separated by `|`.
83
101
  #
@@ -1,5 +1,5 @@
1
1
  module Lotus
2
2
  module Utils
3
- VERSION = '0.1.1'
3
+ VERSION = '0.2.0'
4
4
  end
5
5
  end
data/lotus-utils.gemspec CHANGED
@@ -13,11 +13,11 @@ Gem::Specification.new do |spec|
13
13
  spec.homepage = 'http://lotusrb.org'
14
14
  spec.license = 'MIT'
15
15
 
16
- spec.files = `git ls-files`.split($/)
16
+ spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md lotus-utils.gemspec`.split($/)
17
17
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
18
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.add_development_dependency 'bundler', '~> 1.5'
21
+ spec.add_development_dependency 'bundler', '~> 1.6'
22
22
  spec.add_development_dependency 'rake', '~> 10'
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lotus-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-04-23 00:00:00.000000000 Z
11
+ date: 2014-06-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '1.6'
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
- version: '1.5'
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rake
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -45,15 +45,9 @@ executables: []
45
45
  extensions: []
46
46
  extra_rdoc_files: []
47
47
  files:
48
- - ".gitignore"
49
- - ".travis.yml"
50
- - ".yardopts"
51
48
  - CHANGELOG.md
52
- - CONTRIBUTING.md
53
- - Gemfile
54
49
  - LICENSE.md
55
50
  - README.md
56
- - Rakefile
57
51
  - lib/lotus/utils.rb
58
52
  - lib/lotus/utils/callbacks.rb
59
53
  - lib/lotus/utils/class.rb
@@ -61,20 +55,11 @@ files:
61
55
  - lib/lotus/utils/hash.rb
62
56
  - lib/lotus/utils/io.rb
63
57
  - lib/lotus/utils/kernel.rb
58
+ - lib/lotus/utils/load_paths.rb
64
59
  - lib/lotus/utils/path_prefix.rb
65
60
  - lib/lotus/utils/string.rb
66
61
  - lib/lotus/utils/version.rb
67
62
  - lotus-utils.gemspec
68
- - test/callbacks_test.rb
69
- - test/class_attribute_test.rb
70
- - test/class_test.rb
71
- - test/hash_test.rb
72
- - test/io_test.rb
73
- - test/kernel_test.rb
74
- - test/path_prefix_test.rb
75
- - test/string_test.rb
76
- - test/test_helper.rb
77
- - test/version_test.rb
78
63
  homepage: http://lotusrb.org
79
64
  licenses:
80
65
  - MIT
@@ -99,15 +84,5 @@ rubygems_version: 2.2.2
99
84
  signing_key:
100
85
  specification_version: 4
101
86
  summary: Ruby core extentions and Louts utilities
102
- test_files:
103
- - test/callbacks_test.rb
104
- - test/class_attribute_test.rb
105
- - test/class_test.rb
106
- - test/hash_test.rb
107
- - test/io_test.rb
108
- - test/kernel_test.rb
109
- - test/path_prefix_test.rb
110
- - test/string_test.rb
111
- - test/test_helper.rb
112
- - test/version_test.rb
87
+ test_files: []
113
88
  has_rdoc:
data/.gitignore DELETED
@@ -1,20 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .devnotes
6
- .greenbar
7
- .yardoc
8
- Gemfile.lock
9
- InstalledFiles
10
- _yardoc
11
- coverage
12
- doc/
13
- lib/bundler/man
14
- pkg
15
- rdoc
16
- spec/reports
17
- test/tmp
18
- test/version_tmp
19
- tmp
20
- bin
data/.travis.yml DELETED
@@ -1,6 +0,0 @@
1
- language: ruby
2
- script: 'bundle exec rake test:coverage'
3
- rvm:
4
- - 2.0.0
5
- - 2.1.0
6
- - 2.1.1
data/.yardopts DELETED
@@ -1,3 +0,0 @@
1
- -
2
- LICENSE.txt
3
- lib/**/*.rb
data/CONTRIBUTING.md DELETED
@@ -1,44 +0,0 @@
1
- ## Contributing
2
- In the spirit of [free software][free-sw], **everyone** is encouraged to help
3
- improve this project.
4
-
5
- [free-sw]: http://www.fsf.org/licensing/essays/free-sw.html
6
-
7
- Here are some ways *you* can contribute:
8
-
9
- * by using alpha, beta, and prerelease versions
10
- * by reporting bugs
11
- * by suggesting new features
12
- * by writing or editing documentation
13
- * by writing specifications
14
- * by writing code (**no patch is too small**: fix typos, add comments, clean up
15
- inconsistent whitespace)
16
- * by refactoring code
17
- * by closing [issues][]
18
- * by reviewing patches
19
-
20
- [issues]: https://github.com/lotus/utils/issues
21
-
22
- ## Submitting an Issue
23
- We use the [GitHub issue tracker][issues] to track bugs and features. Before
24
- submitting a bug report or feature request, check to make sure it hasn't
25
- already been submitted. When submitting a bug report, please include a [Gist][]
26
- that includes a stack trace and any details that may be necessary to reproduce
27
- the bug, including your gem version, Ruby version, and operating system.
28
- Ideally, a bug report should include a pull request with failing specs.
29
-
30
- [gist]: https://gist.github.com/
31
-
32
- ## Submitting a Pull Request
33
- 1. [Fork the repository.][fork]
34
- 2. [Create a topic branch.][branch]
35
- 3. Add specs for your unimplemented feature or bug fix.
36
- 4. Run `bundle exec rake`. If your specs pass, return to step 3.
37
- 5. Implement your feature or bug fix.
38
- 6. Run `bundle exec rake`. If your specs fail, return to step 5.
39
- 7. Add, commit, and push your changes.
40
- 8. [Submit a pull request.][pr]
41
-
42
- [fork]: http://help.github.com/fork-a-repo/
43
- [branch]: http://learn.github.com/p/branching.html
44
- [pr]: http://help.github.com/send-pull-requests/
data/Gemfile DELETED
@@ -1,10 +0,0 @@
1
- source 'https://rubygems.org'
2
- gemspec
3
-
4
- unless ENV['TRAVIS']
5
- gem 'byebug', require: false, platforms: :ruby if RUBY_VERSION == '2.1.1'
6
- gem 'yard', require: false
7
- end
8
-
9
- gem 'simplecov', require: false
10
- gem 'coveralls', require: false
data/Rakefile DELETED
@@ -1,17 +0,0 @@
1
- require 'rake'
2
- require 'rake/testtask'
3
- require 'bundler/gem_tasks'
4
-
5
- Rake::TestTask.new do |t|
6
- t.pattern = 'test/**/*_test.rb'
7
- t.libs.push 'test'
8
- end
9
-
10
- namespace :test do
11
- task :coverage do
12
- ENV['COVERAGE'] = 'true'
13
- Rake::Task['test'].invoke
14
- end
15
- end
16
-
17
- task default: :test
@@ -1,213 +0,0 @@
1
- require 'test_helper'
2
- require 'lotus/utils/callbacks'
3
-
4
- class Callable
5
- def call
6
- end
7
- end
8
-
9
- class Action
10
- attr_reader :logger
11
-
12
- def initialize
13
- @logger = Array.new
14
- end
15
-
16
- private
17
- def authenticate!
18
- logger.push 'authenticate!'
19
- end
20
-
21
- def set_article(params)
22
- logger.push "set_article: #{ params[:id] }"
23
- end
24
- end
25
-
26
- describe Lotus::Utils::Callbacks::Chain do
27
- before do
28
- @chain = Lotus::Utils::Callbacks::Chain.new
29
- end
30
-
31
- describe '#add' do
32
- it 'wraps the given callback with a callable object' do
33
- @chain.add :symbolize!
34
-
35
- cb = @chain.first
36
- cb.must_respond_to(:call)
37
- end
38
-
39
- describe 'when a callable object is passed' do
40
- before do
41
- @chain.add callback
42
- end
43
-
44
- let(:callback) { Callable.new }
45
-
46
- it 'includes the given callback' do
47
- cb = @chain.first
48
- cb.callback.must_equal(callback)
49
- end
50
- end
51
-
52
- describe 'when a Symbol is passed' do
53
- before do
54
- @chain.add callback
55
- end
56
-
57
- let(:callback) { :upcase }
58
-
59
- it 'includes the given callback' do
60
- cb = @chain.first
61
- cb.callback.must_equal(callback)
62
- end
63
-
64
- it 'guarantees unique entries' do
65
- # add the callback again, see before block
66
- @chain.add callback
67
- @chain.size.must_equal(1)
68
- end
69
- end
70
-
71
- describe 'when a block is passed' do
72
- before do
73
- @chain.add(&callback)
74
- end
75
-
76
- let(:callback) { Proc.new{} }
77
-
78
- it 'includes the given callback' do
79
- cb = @chain.first
80
- assert_equal cb.callback, callback
81
- end
82
- end
83
-
84
- describe 'when multiple callbacks are passed' do
85
- before do
86
- @chain.add *callbacks
87
- end
88
-
89
- let(:callbacks) { [:upcase, Callable.new, Proc.new{}] }
90
-
91
- it 'includes all the given callbacks' do
92
- @chain.size.must_equal(callbacks.size)
93
- end
94
-
95
- it 'all the included callbacks are callable' do
96
- @chain.each do |callback|
97
- callback.must_respond_to(:call)
98
- end
99
- end
100
- end
101
- end
102
-
103
- describe '#run' do
104
- let(:action) { Action.new }
105
- let(:params) { Hash[id: 23] }
106
-
107
- describe 'when symbols are passed' do
108
- before do
109
- @chain.add :authenticate!, :set_article
110
- @chain.run action, params
111
- end
112
-
113
- it 'executes the callbacks' do
114
- authenticate = action.logger.shift
115
- authenticate.must_equal 'authenticate!'
116
-
117
- set_article = action.logger.shift
118
- set_article.must_equal "set_article: #{ params[:id] }"
119
- end
120
- end
121
-
122
- describe 'when procs are passed' do
123
- before do
124
- @chain.add do
125
- logger.push 'authenticate!'
126
- end
127
-
128
- @chain.add do |params|
129
- logger.push "set_article: #{ params[:id] }"
130
- end
131
-
132
- @chain.run action, params
133
- end
134
-
135
- it 'executes the callbacks' do
136
- authenticate = action.logger.shift
137
- authenticate.must_equal 'authenticate!'
138
-
139
- set_article = action.logger.shift
140
- set_article.must_equal "set_article: #{ params[:id] }"
141
- end
142
- end
143
- end
144
- end
145
-
146
- describe Lotus::Utils::Callbacks::Factory do
147
- describe '.fabricate' do
148
- before do
149
- @callback = Lotus::Utils::Callbacks::Factory.fabricate(callback)
150
- end
151
-
152
- describe 'when a callable is passed' do
153
- let(:callback) { Callable.new }
154
-
155
- it 'fabricates a Callback' do
156
- @callback.must_be_kind_of(Lotus::Utils::Callbacks::Callback)
157
- end
158
-
159
- it 'wraps the given callback' do
160
- @callback.callback.must_equal(callback)
161
- end
162
- end
163
-
164
- describe 'when a symbol is passed' do
165
- let(:callback) { :symbolize! }
166
-
167
- it 'fabricates a MethodCallback' do
168
- @callback.must_be_kind_of(Lotus::Utils::Callbacks::MethodCallback)
169
- end
170
-
171
- it 'wraps the given callback' do
172
- @callback.callback.must_equal(callback)
173
- end
174
- end
175
- end
176
- end
177
-
178
- describe Lotus::Utils::Callbacks::Callback do
179
- before do
180
- @callback = Lotus::Utils::Callbacks::Callback.new(callback)
181
- end
182
-
183
- let(:callback) { Proc.new{|params| logger.push("set_article: #{ params[:id] }") } }
184
-
185
- it 'executes self within the given context' do
186
- context = Action.new
187
- @callback.call(context, { id: 23 })
188
-
189
- invokation = context.logger.shift
190
- invokation.must_equal("set_article: 23")
191
- end
192
- end
193
-
194
- describe Lotus::Utils::Callbacks::MethodCallback do
195
- before do
196
- @callback = Lotus::Utils::Callbacks::MethodCallback.new(callback)
197
- end
198
-
199
- let(:callback) { :set_article }
200
-
201
- it 'executes self within the given context' do
202
- context = Action.new
203
- @callback.call(context, { id: 23 })
204
-
205
- invokation = context.logger.shift
206
- invokation.must_equal("set_article: 23")
207
- end
208
-
209
- it 'implements #hash' do
210
- cb = Lotus::Utils::Callbacks::MethodCallback.new(callback)
211
- cb.send(:hash).must_equal(@callback.send(:hash))
212
- end
213
- end