lingohub_utils 1.0.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.rspec +2 -0
- data/lib/lh/core_extensions/array/raise_if_item_nil.rb +23 -0
- data/lib/lh/core_extensions/array/to_h.rb +11 -0
- data/lib/lh/core_extensions/hash/raise_if_value_nil.rb +9 -0
- data/lib/lh/core_extensions/hash/raise_unless_has_key.rb +9 -0
- data/lib/lh/core_extensions/object/raise_if_nil.rb +10 -0
- data/lib/lh/core_extensions/struct/to_hash.rb +12 -0
- data/lib/lingohub_utils/version.rb +1 -1
- data/lib/lingohub_utils.rb +5 -2
- data/spec/lh/core_extensions/array/raise_if_item_nil_spec.rb +21 -0
- data/spec/lh/core_extensions/array/to_h_spec.rb +21 -0
- data/spec/lh/core_extensions/hash/raise_if_value_nil_spec.rb +21 -0
- data/spec/lh/core_extensions/hash/raise_unless_has_key_spec.rb +21 -0
- data/spec/lh/core_extensions/object/raise_if_nil_spec.rb +21 -0
- data/spec/spec_helper.rb +11 -0
- metadata +21 -2
data/.rspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require "lh/core_extensions/object/raise_if_nil"
|
3
|
+
|
4
|
+
class Array
|
5
|
+
|
6
|
+
# Raises an ArgumentError if the array contains any item that is nil.
|
7
|
+
# @param message [String] optional parameter that is the error message
|
8
|
+
# @see Object#raise_if_nil
|
9
|
+
def raise_if_item_nil(message = nil)
|
10
|
+
indices_w_nil_values = []
|
11
|
+
each_index do |index|
|
12
|
+
indices_w_nil_values << index if self[index].nil?
|
13
|
+
end
|
14
|
+
|
15
|
+
unless indices_w_nil_values.empty?
|
16
|
+
raise ArgumentError.new(message) if message
|
17
|
+
compiled_message = "Objects at position #{indices_w_nil_values.join(", ")} are nil"
|
18
|
+
raise ArgumentError.new(compiled_message)
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
class Object
|
3
|
+
|
4
|
+
# Raises an ArgumentError if the object is nil.
|
5
|
+
# @param message [String] optional parameter that is the error message
|
6
|
+
def raise_if_nil(message="Object must not be undefined")
|
7
|
+
raise ArgumentError.new(message) if self.nil?
|
8
|
+
end
|
9
|
+
|
10
|
+
end
|
data/lib/lingohub_utils.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Array do
|
6
|
+
|
7
|
+
describe "#raise_if_item_nil" do
|
8
|
+
context "not nil" do
|
9
|
+
it "should not raise ArgumentError" do
|
10
|
+
lambda { ["asdf", 1].raise_if_item_nil }.should_not raise_error ArgumentError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "nil" do
|
15
|
+
it "should raise ArgumentError" do
|
16
|
+
lambda { ["asdf", 1, nil].raise_if_item_nil }.should raise_error ArgumentError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Array do
|
6
|
+
|
7
|
+
describe "#to_h" do
|
8
|
+
context "empty array" do
|
9
|
+
it "should return empty hash" do
|
10
|
+
[].to_h.should eql({})
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "with items" do
|
15
|
+
it "should transform ['a', 'b', 'c'] to {'a' => 0, 'b' => 1, 'c' => 2}" do
|
16
|
+
['a', 'b', 'c'].to_h.should eql({'a' => 0, 'b' => 1, 'c' => 2})
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Hash do
|
6
|
+
|
7
|
+
describe "#raise_if_value_nil" do
|
8
|
+
context "not nil" do
|
9
|
+
it "should not raise ArgumentError" do
|
10
|
+
lambda { {:test => "test"}.raise_if_value_nil :test }.should_not raise_error ArgumentError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "nil" do
|
15
|
+
it "should raise ArgumentError" do
|
16
|
+
lambda { {}.raise_if_value_nil :test }.should_not raise_error ArgumentError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Hash do
|
6
|
+
|
7
|
+
describe "#raise_unless_has_key" do
|
8
|
+
context "has key" do
|
9
|
+
it "should not raise Error" do
|
10
|
+
lambda { {:test => "test"}.raise_unless_has_key :test }.should_not raise_error RuntimeError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "hasn't key" do
|
15
|
+
it "should raise Error" do
|
16
|
+
lambda { {}.raise_unless_has_key :test }.should raise_error RuntimeError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require "spec_helper"
|
4
|
+
|
5
|
+
describe Object do
|
6
|
+
|
7
|
+
describe "#raise_if_nil" do
|
8
|
+
context "not nil" do
|
9
|
+
it "should not raise ArgumentError" do
|
10
|
+
lambda { Object.new.raise_if_nil }.should_not raise_error ArgumentError
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
context "nil" do
|
15
|
+
it "should raise ArgumentError" do
|
16
|
+
lambda { nil.raise_if_nil }.should raise_error ArgumentError
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler/setup'
|
3
|
+
|
4
|
+
require 'lingohub_utils'
|
5
|
+
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lingohub_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -19,13 +19,26 @@ extensions: []
|
|
19
19
|
extra_rdoc_files: []
|
20
20
|
files:
|
21
21
|
- .gitignore
|
22
|
+
- .rspec
|
22
23
|
- Gemfile
|
23
24
|
- LICENSE
|
24
25
|
- README.md
|
25
26
|
- Rakefile
|
27
|
+
- lib/lh/core_extensions/array/raise_if_item_nil.rb
|
28
|
+
- lib/lh/core_extensions/array/to_h.rb
|
29
|
+
- lib/lh/core_extensions/hash/raise_if_value_nil.rb
|
30
|
+
- lib/lh/core_extensions/hash/raise_unless_has_key.rb
|
31
|
+
- lib/lh/core_extensions/object/raise_if_nil.rb
|
32
|
+
- lib/lh/core_extensions/struct/to_hash.rb
|
26
33
|
- lib/lingohub_utils.rb
|
27
34
|
- lib/lingohub_utils/version.rb
|
28
35
|
- lingohub_utils.gemspec
|
36
|
+
- spec/lh/core_extensions/array/raise_if_item_nil_spec.rb
|
37
|
+
- spec/lh/core_extensions/array/to_h_spec.rb
|
38
|
+
- spec/lh/core_extensions/hash/raise_if_value_nil_spec.rb
|
39
|
+
- spec/lh/core_extensions/hash/raise_unless_has_key_spec.rb
|
40
|
+
- spec/lh/core_extensions/object/raise_if_nil_spec.rb
|
41
|
+
- spec/spec_helper.rb
|
29
42
|
homepage: http://lingohub.com
|
30
43
|
licenses: []
|
31
44
|
post_install_message:
|
@@ -51,4 +64,10 @@ signing_key:
|
|
51
64
|
specification_version: 3
|
52
65
|
summary: This gem contains general util classes and extensions used by other lingohub
|
53
66
|
gems and programms.
|
54
|
-
test_files:
|
67
|
+
test_files:
|
68
|
+
- spec/lh/core_extensions/array/raise_if_item_nil_spec.rb
|
69
|
+
- spec/lh/core_extensions/array/to_h_spec.rb
|
70
|
+
- spec/lh/core_extensions/hash/raise_if_value_nil_spec.rb
|
71
|
+
- spec/lh/core_extensions/hash/raise_unless_has_key_spec.rb
|
72
|
+
- spec/lh/core_extensions/object/raise_if_nil_spec.rb
|
73
|
+
- spec/spec_helper.rb
|