sumhash 0.0.2 → 0.0.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/lib/sumhash.rb +9 -3
- data/lib/sumhash/hash.rb +8 -15
- data/lib/sumhash/ostruct.rb +20 -21
- data/lib/sumhash/version.rb +1 -2
- data/sumhash.gemspec +4 -7
- data/test/test_division.rb +1 -3
- data/test/test_multiplication.rb +1 -3
- data/test/test_plus.rb +1 -3
- data/test/test_unary_minus.rb +1 -3
- metadata +14 -16
- data/test/suite.rb +0 -5
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 6821cbf43c71f33fb60986e6673e5a5b04f006f4
|
4
|
+
data.tar.gz: e550b4a42a77731938b66faacc91eca6cb487ab5
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9dffd044887b13b9dc554fe45218bc8194c2c105fe62bd6684f8dd5db55240c347b8e93461b888b6d20d4ddf2e8608c1b0e8600119e8d18ff062a4c41daed36c
|
7
|
+
data.tar.gz: 86e4a7d2c1477d4cf12718cacbb0cebde19f3fcd25c9616e94e957c3f7888c41085e3020a6641cb545b12374f7467d82c439058a5906b80d803864a92c9f19bb
|
data/lib/sumhash.rb
CHANGED
@@ -1,6 +1,12 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
require 'ostruct'
|
1
|
+
require 'sumhash/version'
|
4
2
|
require 'sumhash/hash'
|
5
3
|
require 'sumhash/ostruct'
|
4
|
+
require 'ostruct'
|
5
|
+
|
6
|
+
class Hash
|
7
|
+
include Sumhash::Hash
|
8
|
+
end
|
6
9
|
|
10
|
+
class OpenStruct
|
11
|
+
include Sumhash::OpenStruct
|
12
|
+
end
|
data/lib/sumhash/hash.rb
CHANGED
@@ -1,21 +1,15 @@
|
|
1
|
-
|
1
|
+
module Sumhash::Hash
|
2
2
|
NUMBER_CLASSES = [Integer, Fixnum, Bignum, Float, Rational]
|
3
3
|
SUPPORTED_CLASSES = NUMBER_CLASSES + [Hash, OpenStruct]
|
4
4
|
|
5
5
|
# Plus
|
6
6
|
def +(hash)
|
7
|
-
|
8
|
-
sum[k] = sum(self[k], hash[k])
|
9
|
-
sum
|
10
|
-
end
|
7
|
+
self.merge(hash) { |_, v1, v2| sum(v1, v2) }
|
11
8
|
end
|
12
9
|
|
13
10
|
# Minus
|
14
11
|
def -(hash)
|
15
|
-
|
16
|
-
sum[k] = sum(self[k], hash[k], :-)
|
17
|
-
sum
|
18
|
-
end
|
12
|
+
self.merge(hash) { |_, v1, v2| sum(v1, v2, :-) }
|
19
13
|
end
|
20
14
|
|
21
15
|
# Unary minus
|
@@ -35,7 +29,7 @@ class Hash
|
|
35
29
|
def /(num)
|
36
30
|
raise TypeError, "#{num.class} can't be coerced into Float" unless NUMBER_CLASSES.include? num.class
|
37
31
|
self.inject({}) do |res, (k, v)|
|
38
|
-
res[k] = SUPPORTED_CLASSES.include?(v.class) ? v/num : v
|
32
|
+
res[k] = SUPPORTED_CLASSES.include?(v.class) ? v / num : v
|
39
33
|
res
|
40
34
|
end
|
41
35
|
end
|
@@ -44,19 +38,18 @@ class Hash
|
|
44
38
|
def *(num)
|
45
39
|
raise TypeError, "#{num.class} can't be coerced into Float" unless NUMBER_CLASSES.include? num.class
|
46
40
|
self.inject({}) do |res, (k, v)|
|
47
|
-
res[k] = SUPPORTED_CLASSES.include?(v.class) ? v*num : v
|
41
|
+
res[k] = SUPPORTED_CLASSES.include?(v.class) ? v * num : v
|
48
42
|
res
|
49
43
|
end
|
50
44
|
end
|
51
45
|
|
52
|
-
|
46
|
+
private
|
53
47
|
# Can sum Objects if: 1) both numbers; 2) both Hashes; 3) both OpenStructs.
|
54
48
|
def sum(n, m, sign=:+)
|
55
|
-
if (NUMBER_CLASSES.include?(n.class) && NUMBER_CLASSES.include?(m.class)) || (n.
|
56
|
-
n.
|
49
|
+
if (NUMBER_CLASSES.include?(n.class) && NUMBER_CLASSES.include?(m.class)) || (n.is_a?(Hash) && m.is_a?(Hash)) || (n.is_a?(OpenStruct) && m.is_a?(OpenStruct))
|
50
|
+
n.__send__(sign, m)
|
57
51
|
else
|
58
52
|
n || m
|
59
53
|
end
|
60
54
|
end
|
61
55
|
end
|
62
|
-
|
data/lib/sumhash/ostruct.rb
CHANGED
@@ -1,32 +1,32 @@
|
|
1
|
-
|
1
|
+
module Sumhash::OpenStruct
|
2
2
|
NUMBER_CLASSES = [Integer, Fixnum, Bignum, Float, Rational]
|
3
3
|
SUPPORTED_CLASSES = NUMBER_CLASSES + [Hash, OpenStruct]
|
4
4
|
|
5
5
|
def _fields
|
6
|
-
@table.keys.map
|
6
|
+
@table.keys.map(&:to_sym)
|
7
7
|
end
|
8
8
|
|
9
9
|
# Plus
|
10
10
|
def +(os)
|
11
|
-
(self._fields + os._fields).inject(OpenStruct.new
|
12
|
-
sum.
|
11
|
+
(self._fields + os._fields).inject(OpenStruct.new) do |sum, f|
|
12
|
+
sum.__send__(:"#{f}=", sum(self.__send__(f), os.__send__(f)))
|
13
13
|
sum
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
17
17
|
# Minus
|
18
18
|
def -(os)
|
19
|
-
(self._fields + os._fields).inject(OpenStruct.new
|
20
|
-
sum.
|
19
|
+
(self._fields + os._fields).inject(OpenStruct.new) do |sum, f|
|
20
|
+
sum.__send__(:"#{f}=", sum(self.__send__(f), os.__send__(f), :-))
|
21
21
|
sum
|
22
22
|
end
|
23
23
|
end
|
24
24
|
|
25
25
|
# Unary minus
|
26
26
|
def -@
|
27
|
-
self._fields.inject(OpenStruct.new
|
28
|
-
v = self.
|
29
|
-
res.
|
27
|
+
self._fields.inject(OpenStruct.new) do |res, f|
|
28
|
+
v = self.__send__(f)
|
29
|
+
res.__send__(:"#{f}=", SUPPORTED_CLASSES.include?(v.class) ? -v : v)
|
30
30
|
res
|
31
31
|
end
|
32
32
|
end
|
@@ -38,32 +38,31 @@ class OpenStruct
|
|
38
38
|
|
39
39
|
# Division
|
40
40
|
def /(num)
|
41
|
-
raise TypeError, "#{num.class} can't be coerced into Float"
|
42
|
-
self._fields.inject(OpenStruct.new
|
43
|
-
v = self.
|
44
|
-
res.
|
41
|
+
raise TypeError, "#{num.class} can't be coerced into Float" unless NUMBER_CLASSES.include? num.class
|
42
|
+
self._fields.inject(OpenStruct.new) do |res, f|
|
43
|
+
v = self.__send__(f)
|
44
|
+
res.__send__(:"#{f}=", SUPPORTED_CLASSES.include?(v.class) ? v / num.to_f : v)
|
45
45
|
res
|
46
46
|
end
|
47
47
|
end
|
48
48
|
|
49
49
|
# Multiplication
|
50
50
|
def *(num)
|
51
|
-
raise TypeError, "#{num.class} can't be coerced into Float"
|
52
|
-
self._fields.inject(OpenStruct.new
|
53
|
-
v = self.
|
54
|
-
res.
|
51
|
+
raise TypeError, "#{num.class} can't be coerced into Float" unless NUMBER_CLASSES.include? num.class
|
52
|
+
self._fields.inject(OpenStruct.new) do |res, f|
|
53
|
+
v = self.__send__(f)
|
54
|
+
res.__send__(:"#{f}=", SUPPORTED_CLASSES.include?(v.class) && !v.kind_of?(String) ? v * num.to_f : v)
|
55
55
|
res
|
56
56
|
end
|
57
57
|
end
|
58
58
|
|
59
|
-
|
59
|
+
private
|
60
60
|
# Can sum Objects if: 1) both numbers; 2) both Hashes; 3) both OpenStructs.
|
61
61
|
def sum(n, m, sign=:+)
|
62
|
-
if (NUMBER_CLASSES.include?(n.class) && NUMBER_CLASSES.include?(m.class)) || (n.
|
63
|
-
n.
|
62
|
+
if (NUMBER_CLASSES.include?(n.class) && NUMBER_CLASSES.include?(m.class)) || (n.is_a?(Hash) && m.is_a?(Hash)) || (n.is_a?(OpenStruct) && m.is_a?(OpenStruct))
|
63
|
+
n.__send__(sign, m)
|
64
64
|
else
|
65
65
|
n || m
|
66
66
|
end
|
67
67
|
end
|
68
68
|
end
|
69
|
-
|
data/lib/sumhash/version.rb
CHANGED
data/sumhash.gemspec
CHANGED
@@ -6,26 +6,24 @@ Gem::Specification.new do |s|
|
|
6
6
|
s.name = "sumhash"
|
7
7
|
s.version = Sumhash::VERSION
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
|
-
s.authors = ["
|
9
|
+
s.authors = ["Oleksandr Avoiants", "Andrii Dmytrenko", "Alexey Grachov"]
|
10
10
|
s.email = ["shhavel@gmail.com"]
|
11
11
|
s.homepage = "https://github.com/shhavel/sumhash"
|
12
|
-
s.summary = %q{
|
13
|
-
s.description = %q{
|
12
|
+
s.summary = %q{Summation operation for Hash and OpenStruct}
|
13
|
+
s.description = %q{Provides summation operation to Hash and OpenStruct. Works with nested structures.}
|
14
14
|
s.rubyforge_project = "sumhash"
|
15
15
|
s.files = [
|
16
16
|
"sumhash.gemspec",
|
17
17
|
"lib/sumhash.rb",
|
18
18
|
"lib/sumhash/hash.rb",
|
19
19
|
"lib/sumhash/ostruct.rb",
|
20
|
-
"lib/sumhash/version.rb",
|
21
|
-
"test/suite.rb",
|
20
|
+
"lib/sumhash/version.rb",
|
22
21
|
"test/test_division.rb",
|
23
22
|
"test/test_multiplication.rb",
|
24
23
|
"test/test_plus.rb",
|
25
24
|
"test/test_unary_minus.rb",
|
26
25
|
]
|
27
26
|
s.test_files = [
|
28
|
-
"test/suite.rb",
|
29
27
|
"test/test_division.rb",
|
30
28
|
"test/test_multiplication.rb",
|
31
29
|
"test/test_plus.rb",
|
@@ -33,4 +31,3 @@ Gem::Specification.new do |s|
|
|
33
31
|
]
|
34
32
|
s.require_paths = ["lib"]
|
35
33
|
end
|
36
|
-
|
data/test/test_division.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "#{File.expand_path('../')}/lib/sumhash.rb"
|
1
|
+
require "sumhash"
|
3
2
|
require "test/unit"
|
4
3
|
|
5
4
|
class TestDivision < Test::Unit::TestCase
|
@@ -11,4 +10,3 @@ class TestDivision < Test::Unit::TestCase
|
|
11
10
|
assert_raise(TypeError) { OpenStruct.new({ n: 10, m: 14 })/"2" }
|
12
11
|
end
|
13
12
|
end
|
14
|
-
|
data/test/test_multiplication.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "#{File.expand_path('../')}/lib/sumhash.rb"
|
1
|
+
require "sumhash"
|
3
2
|
require "test/unit"
|
4
3
|
|
5
4
|
class TestMultiplication < Test::Unit::TestCase
|
@@ -11,4 +10,3 @@ class TestMultiplication < Test::Unit::TestCase
|
|
11
10
|
assert_raise(TypeError) { OpenStruct.new({ n: 5, m: 7 }) * "2" }
|
12
11
|
end
|
13
12
|
end
|
14
|
-
|
data/test/test_plus.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "#{File.expand_path('../')}/lib/sumhash.rb"
|
1
|
+
require "sumhash"
|
3
2
|
require "test/unit"
|
4
3
|
|
5
4
|
class TestPlus < Test::Unit::TestCase
|
@@ -58,4 +57,3 @@ class TestPlus < Test::Unit::TestCase
|
|
58
57
|
{ n: 2.1, m: 3.2, os: OpenStruct.new(x: 1.1, y: 2.1), s: 'str' } * 2)
|
59
58
|
end
|
60
59
|
end
|
61
|
-
|
data/test/test_unary_minus.rb
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
require "
|
2
|
-
require "#{File.expand_path('../')}/lib/sumhash.rb"
|
1
|
+
require "sumhash"
|
3
2
|
require "test/unit"
|
4
3
|
|
5
4
|
class TestUnaryMinus < Test::Unit::TestCase
|
@@ -8,4 +7,3 @@ class TestUnaryMinus < Test::Unit::TestCase
|
|
8
7
|
-{ n: 4.2, m: 3.3, os: OpenStruct.new(x: 2.2, y: -4.2), s: 'str' })
|
9
8
|
end
|
10
9
|
end
|
11
|
-
|
metadata
CHANGED
@@ -1,60 +1,58 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sumhash
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
5
|
-
prerelease:
|
4
|
+
version: 0.0.4
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
|
-
-
|
7
|
+
- Oleksandr Avoiants
|
8
|
+
- Andrii Dmytrenko
|
9
|
+
- Alexey Grachov
|
9
10
|
autorequire:
|
10
11
|
bindir: bin
|
11
12
|
cert_chain: []
|
12
|
-
date:
|
13
|
+
date: 2016-01-10 00:00:00.000000000 Z
|
13
14
|
dependencies: []
|
14
|
-
description:
|
15
|
-
|
15
|
+
description: Provides summation operation to Hash and OpenStruct. Works with nested
|
16
|
+
structures.
|
16
17
|
email:
|
17
18
|
- shhavel@gmail.com
|
18
19
|
executables: []
|
19
20
|
extensions: []
|
20
21
|
extra_rdoc_files: []
|
21
22
|
files:
|
22
|
-
- sumhash.gemspec
|
23
23
|
- lib/sumhash.rb
|
24
24
|
- lib/sumhash/hash.rb
|
25
25
|
- lib/sumhash/ostruct.rb
|
26
26
|
- lib/sumhash/version.rb
|
27
|
-
-
|
27
|
+
- sumhash.gemspec
|
28
28
|
- test/test_division.rb
|
29
29
|
- test/test_multiplication.rb
|
30
30
|
- test/test_plus.rb
|
31
31
|
- test/test_unary_minus.rb
|
32
32
|
homepage: https://github.com/shhavel/sumhash
|
33
33
|
licenses: []
|
34
|
+
metadata: {}
|
34
35
|
post_install_message:
|
35
36
|
rdoc_options: []
|
36
37
|
require_paths:
|
37
38
|
- lib
|
38
39
|
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
-
none: false
|
40
40
|
requirements:
|
41
|
-
- -
|
41
|
+
- - ">="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
version: '0'
|
44
44
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
-
none: false
|
46
45
|
requirements:
|
47
|
-
- -
|
46
|
+
- - ">="
|
48
47
|
- !ruby/object:Gem::Version
|
49
48
|
version: '0'
|
50
49
|
requirements: []
|
51
50
|
rubyforge_project: sumhash
|
52
|
-
rubygems_version:
|
51
|
+
rubygems_version: 2.4.8
|
53
52
|
signing_key:
|
54
|
-
specification_version:
|
55
|
-
summary:
|
53
|
+
specification_version: 4
|
54
|
+
summary: Summation operation for Hash and OpenStruct
|
56
55
|
test_files:
|
57
|
-
- test/suite.rb
|
58
56
|
- test/test_division.rb
|
59
57
|
- test/test_multiplication.rb
|
60
58
|
- test/test_plus.rb
|