lingohub_utils 1.0.0 → 1.0.1

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.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
@@ -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,11 @@
1
+ require "enumerator"
2
+
3
+ class Array
4
+ def to_h
5
+ hash = Hash.new
6
+ self.each_with_index do |item, index|
7
+ hash[item] = index
8
+ end
9
+ hash
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Hash
3
+
4
+ def raise_if_value_nil(key, message=nil)
5
+ message = "Value for '#{key}' must be not nil".if message.nil?
6
+ self[key].raise_if_nil(message)
7
+ end
8
+
9
+ end
@@ -0,0 +1,9 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Hash
3
+
4
+ def raise_unless_has_key(key, message=nil)
5
+ message = "Hash must have key '#{key}'." if message.nil?
6
+ raise message unless self.has_key?(key)
7
+ end
8
+
9
+ 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
@@ -0,0 +1,12 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Struct
3
+
4
+ def to_hash
5
+ hash = {}
6
+ self.each_pair do |key, value|
7
+ hash[key] = value
8
+ end
9
+ hash
10
+ end
11
+
12
+ end
@@ -1,3 +1,3 @@
1
1
  module LingohubUtils
2
- VERSION = "1.0.0"
2
+ VERSION = "1.0.1"
3
3
  end
@@ -1,5 +1,8 @@
1
- require "lingohub_utils/version"
1
+ # load core extensions per default
2
+ Dir[File.join(File.dirname(__FILE__) + "/lh/core_extensions", "**/*.rb")].each do |f|
3
+ require f
4
+ end
2
5
 
3
6
  module LingohubUtils
4
- # Your code goes here...
7
+
5
8
  end
@@ -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
@@ -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.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