inactive_support 0.0.1 → 0.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.
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # InactiveSupport
2
2
 
3
- TODO: Write a gem description
3
+ A collection of utilities for ruby projects.
4
4
 
5
5
  ## Installation
6
6
 
@@ -18,7 +18,7 @@ Or install it yourself as:
18
18
 
19
19
  ## Usage
20
20
 
21
- TODO: Write usage instructions here
21
+ Soon
22
22
 
23
23
  ## Contributing
24
24
 
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency "bundler", "~> 1.3"
22
22
  spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.6"
23
24
  end
@@ -0,0 +1 @@
1
+ require 'inactive_support/hash/delete_blank'
@@ -0,0 +1,28 @@
1
+ class Numeric
2
+ # A more precise rounding. ~4 times slower than simple round
3
+ #
4
+ # 3.904605.round(2)
5
+ # # => 3.9
6
+ # 3.904605.precision(2)
7
+ # # => 3.91
8
+ #
9
+ # 37.9945.round(2)
10
+ # # => 37.99
11
+ # 37.9945.precision(2)
12
+ # # => 38.0
13
+ def precision(precision = 0)
14
+ power = 10 ** precision
15
+
16
+ if precision == 0
17
+ self.round
18
+ else
19
+ powered = self * power
20
+
21
+ (precision - 1).downto(0).each do |i|
22
+ powered = powered.round(i).to_f
23
+ end
24
+
25
+ powered.to_f / power.to_f
26
+ end
27
+ end
28
+ end
@@ -0,0 +1 @@
1
+ require 'inactive_support/numeric/precision'
@@ -0,0 +1,9 @@
1
+ class Object
2
+ def blank?
3
+ respond_to?(:empty?) ? !!empty? : !self
4
+ end
5
+
6
+ def present?
7
+ !blank?
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ class Object
2
+ # Returns self. If given a block, it works a lot like Object#tap
3
+ #
4
+ # ==== Examples
5
+ #
6
+ # [1,2,3,5,7].consecutive_by(:identity)
7
+ # => [[1, 2, 3], [5], [7]]
8
+ def identity
9
+ if block_given?
10
+ yield self
11
+ else
12
+ self
13
+ end
14
+ end
15
+ end
16
+
17
+
@@ -0,0 +1,47 @@
1
+ class Object
2
+
3
+ # Credit goes to the active_support contributors
4
+ # https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/object/try.rb
5
+ #
6
+ # Invokes the public method whose name goes as first argument just like
7
+ # +public_send+ does, except that if the receiver does not respond to it the
8
+ # call returns +nil+ rather than raising an exception.
9
+ def try(*args, &b)
10
+ if args.empty? && block_given?
11
+ yield self
12
+ else
13
+ public_send(*args, &b) if respond_to?(args.first)
14
+ end
15
+ end
16
+
17
+
18
+ # Chained try allows writing
19
+ #
20
+ # str.ctry(:mb_chars, :downcase, :dasherize)
21
+ #
22
+ # instead of
23
+ #
24
+ # str.try(:mb_chars).try(:downcase).try(:dasherize)
25
+ #
26
+ # Only works for methods that don't take arguments.
27
+ def ctry(*args)
28
+ first, *rest = args
29
+ if rest.any?
30
+ self.try(first).ctry(*rest)
31
+ else
32
+ self.try(first)
33
+ end
34
+ end
35
+
36
+ end
37
+
38
+ class NilClass
39
+ def try(*args)
40
+ nil
41
+ end
42
+
43
+ def ctry(*args)
44
+ nil
45
+ end
46
+
47
+ end
@@ -0,0 +1,3 @@
1
+ require 'inactive_support/object/blank'
2
+ require 'inactive_support/object/identity'
3
+ require 'inactive_support/object/try'
@@ -1,3 +1,3 @@
1
1
  module InactiveSupport
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  require "inactive_support/version"
2
2
 
3
3
  module InactiveSupport
4
- # Your code goes here...
5
4
  end
@@ -0,0 +1,77 @@
1
+ require 'spec_helper'
2
+ require 'inactive_support/hash/delete_blank'
3
+
4
+ describe Hash do
5
+
6
+ describe "#delete_blank" do
7
+
8
+ it 'deletes nils' do
9
+ initial = {
10
+ id: 1,
11
+ name: nil
12
+ }
13
+
14
+ expected = {
15
+ id: 1
16
+ }
17
+
18
+ initial.delete_blank.should eq expected
19
+ end
20
+
21
+ it 'deletes empty strings' do
22
+ initial = {
23
+ id: 1,
24
+ name: ""
25
+ }
26
+
27
+ expected = {
28
+ id: 1
29
+ }
30
+
31
+ initial.delete_blank.should eq expected
32
+ end
33
+
34
+ it 'preserves false values' do
35
+ initial = {
36
+ id: 1,
37
+ name: false
38
+ }
39
+
40
+ expected = {
41
+ id: 1,
42
+ name: false
43
+ }
44
+
45
+ initial.delete_blank.should eq expected
46
+ end
47
+
48
+ it 'deletes empty arrays' do
49
+ initial = {
50
+ id: 1,
51
+ name: []
52
+ }
53
+
54
+ expected = {
55
+ id: 1
56
+ }
57
+
58
+ initial.delete_blank.should eq expected
59
+
60
+ end
61
+ it 'deletes empty hashes' do
62
+ initial = {
63
+ id: 1,
64
+ name: {}
65
+ }
66
+
67
+ expected = {
68
+ id: 1
69
+ }
70
+
71
+ initial.delete_blank.should eq expected
72
+
73
+ end
74
+
75
+ end
76
+
77
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'inactive_support/numeric'
3
+
4
+ describe Numeric do
5
+
6
+ describe "#precision" do
7
+
8
+ it 'behaves exactly like Float#round when invoked with no arguments', :brute do
9
+ 1_000_000.times do
10
+ n = rand(1.0..100.0)
11
+ puts n if n.precision != n.round
12
+ n.precision.should eq n.round
13
+ end
14
+ end
15
+
16
+ it 'behaves exactly like Float#round when invoked with no arguments' do
17
+ 1_000.times do
18
+ n = rand(1.0..100.0)
19
+ puts n if n.precision != n.round
20
+ n.precision.should eq n.round
21
+ end
22
+ end
23
+
24
+ describe "reality checks" do
25
+
26
+ it "case#1" do
27
+ 3.904605.precision(0).should eq 4
28
+ 3.904605.precision(1).should eq 3.9
29
+ 3.904605.precision(2).should eq 3.91
30
+ 3.904605.precision(3).should eq 3.905
31
+ 3.904605.precision(4).should eq 3.9046
32
+ end
33
+
34
+ it "case#2" do
35
+ 37.9945.precision(0).should eq 38
36
+ 37.9945.precision(1).should eq 38
37
+ 37.9945.precision(2).should eq 38
38
+ end
39
+
40
+ it "case#3" do
41
+ 37.9944.precision(0).should eq 38
42
+ 37.9944.precision(1).should eq 38
43
+ 37.9944.precision(2).should eq 37.99
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+ require 'inactive_support/object'
3
+
4
+ describe Object do
5
+
6
+ describe "#ctry" do
7
+ it "works with 1 argument" do
8
+ "String".ctry(:downcase).should eq 'string'
9
+ end
10
+
11
+ it "works with multiple arguments" do
12
+ "Nurse I spy gypsies run".ctry(:downcase, :reverse).should eq 'nur seispyg yps i esrun'
13
+ end
14
+
15
+ it 'returns nil when called on nil' do
16
+ nil.ctry(:downcase, :upcase).should eq nil
17
+ end
18
+ end
19
+
20
+ end
@@ -0,0 +1,14 @@
1
+ require 'bundler/setup'
2
+ Bundler.setup
3
+
4
+ require 'inactive_support'
5
+
6
+ RSpec.configure do |config|
7
+ config.treat_symbols_as_metadata_keys_with_true_values = true
8
+ config.run_all_when_everything_filtered = true
9
+ config.filter_run :focus
10
+ config.filter_run_excluding :brute
11
+
12
+
13
+ config.order = 'random'
14
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inactive_support
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-30 00:00:00.000000000 Z
12
+ date: 2014-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -43,6 +43,22 @@ dependencies:
43
43
  - - ! '>='
44
44
  - !ruby/object:Gem::Version
45
45
  version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '2.6'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
46
62
  description: A collection of utilities and extensions
47
63
  email:
48
64
  - gvmitrev@gmail.com
@@ -51,15 +67,26 @@ extensions: []
51
67
  extra_rdoc_files: []
52
68
  files:
53
69
  - .gitignore
70
+ - .rspec
54
71
  - Gemfile
55
72
  - LICENSE.txt
56
73
  - README.md
57
74
  - Rakefile
58
75
  - inactive_support.gemspec
59
76
  - lib/inactive_support.rb
60
- - lib/inactive_support/core_ext/hash.rb
61
- - lib/inactive_support/core_ext/object.rb
77
+ - lib/inactive_support/hash.rb
78
+ - lib/inactive_support/hash/delete_blank.rb
79
+ - lib/inactive_support/numeric.rb
80
+ - lib/inactive_support/numeric/precision.rb
81
+ - lib/inactive_support/object.rb
82
+ - lib/inactive_support/object/blank.rb
83
+ - lib/inactive_support/object/identity.rb
84
+ - lib/inactive_support/object/try.rb
62
85
  - lib/inactive_support/version.rb
86
+ - spec/lib/hash_spec.rb
87
+ - spec/lib/numeric_spec.rb
88
+ - spec/lib/object_spec.rb
89
+ - spec/spec_helper.rb
63
90
  homepage: ''
64
91
  licenses:
65
92
  - MIT
@@ -75,7 +102,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
75
102
  version: '0'
76
103
  segments:
77
104
  - 0
78
- hash: -1865332499091341337
105
+ hash: 106245937
79
106
  required_rubygems_version: !ruby/object:Gem::Requirement
80
107
  none: false
81
108
  requirements:
@@ -84,11 +111,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
84
111
  version: '0'
85
112
  segments:
86
113
  - 0
87
- hash: -1865332499091341337
114
+ hash: 106245937
88
115
  requirements: []
89
116
  rubyforge_project:
90
117
  rubygems_version: 1.8.23
91
118
  signing_key:
92
119
  specification_version: 3
93
120
  summary: Snippets and useful stuff extracted from my projects
94
- test_files: []
121
+ test_files:
122
+ - spec/lib/hash_spec.rb
123
+ - spec/lib/numeric_spec.rb
124
+ - spec/lib/object_spec.rb
125
+ - spec/spec_helper.rb
@@ -1,36 +0,0 @@
1
- class Object
2
-
3
- # Chained try
4
- #
5
- # Allows writing
6
- #
7
- # str.ctry(:mb_chars, :downcase, :dasherize)
8
- #
9
- # instead of
10
- #
11
- # str.try(:mb_chars).try(:downcase).try(:dasherize)
12
- #
13
- # Only works for methods that dont have any arguments
14
- def ctry(*args)
15
- first, *rest = args
16
- if rest.any?
17
- self.try(first).ctry(*rest)
18
- else
19
- self.try(first)
20
- end
21
- end
22
-
23
- # Returns self. If given a block, it works a lot like Object#tap
24
- #
25
- # ==== Examples
26
- #
27
- # [1,2,3,5,7].consecutive_by(:identity)
28
- # => [[1, 2, 3], [5], [7]]
29
- def identity
30
- if block_given?
31
- yield self
32
- else
33
- self
34
- end
35
- end
36
- end