ruby_smart-support 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 9ddd076dedba91f00229b1a827c9d07f4d97670cfbac540ad050cb0b20817079
4
+ data.tar.gz: 22d199ba3c75ab029323ff44589234e839b84a72de6cd45374300c85bba49d2a
5
+ SHA512:
6
+ metadata.gz: e53d6679db01e5bcd40503cc6ced2657bcf431e465c7c45d877ca01fce9e4d3a3fbd142cdfb193582a843d9fe9e45019a31033bab34af762a530aadea2b57c45
7
+ data.tar.gz: b0176b30822758271ea3028e0f15ca9020f529760bd7738b38dcba58a0594c5e3536cf078ad313bc65942dc1a53f952e84b39c02839fac46007dd8f66f434c1e
@@ -0,0 +1,38 @@
1
+ # This workflow uses actions that are not certified by GitHub.
2
+ # They are provided by a third-party and are governed by
3
+ # separate terms of service, privacy policy, and support
4
+ # documentation.
5
+ # This workflow will download a prebuilt Ruby version, install dependencies and run tests with Rake
6
+ # For more information see: https://github.com/marketplace/actions/setup-ruby-jruby-and-truffleruby
7
+
8
+ name: Test
9
+
10
+ on:
11
+ push:
12
+ branches: [ "main" ]
13
+ pull_request:
14
+ branches: [ "main" ]
15
+
16
+ permissions:
17
+ contents: read
18
+
19
+ jobs:
20
+ test:
21
+
22
+ runs-on: ubuntu-latest
23
+ strategy:
24
+ matrix:
25
+ ruby-version: ['3.1', '3.0', '2.7', '2.6']
26
+
27
+ steps:
28
+ - uses: actions/checkout@v3
29
+ - name: Set up Ruby
30
+ # To automatically get bug fixes and new Ruby versions for ruby/setup-ruby,
31
+ # change this to (see https://github.com/ruby/setup-ruby#versioning):
32
+ # uses: ruby/setup-ruby@v1
33
+ uses: ruby/setup-ruby@0a29871fe2b0200a17a4497bae54fe5df0d973aa # v1.115.3
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
37
+ - name: Run tests
38
+ run: bundle exec rake
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ # IDE related files
14
+ /.idea
15
+ /.ruby-version
16
+ /.bundle-version
17
+
18
+ # Github testing
19
+ Gemfile.lock
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in ruby_smart-support.gemspec
6
+ gemspec
7
+
data/README.md ADDED
@@ -0,0 +1,200 @@
1
+ # RubySmart::Support
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/ruby_smart-support.svg)](https://badge.fury.io/rb/ruby_smart-support)
4
+ [![Test](https://github.com/ruby-smart/support/actions/workflows/ruby.yml/badge.svg)](https://github.com/ruby-smart/support/actions/workflows/ruby.yml)
5
+ [![RubySmart::Support Build Status](https://shields.io/github/workflow/status/ruby-smart/support/Test)](https://github.com/ruby-smart/support/actions)
6
+
7
+ A toolkit of support libraries including GemInfo, ThreadInfo, Ruby core extensions & optionally activesupport extensions.
8
+
9
+ _RubySmart::Support is a toolkit of support libraries for Ruby - major features includes GemInfo & ThreadInfo, as well core extensions for Ruby & activesupport (if installed)._
10
+
11
+ -----
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ ```ruby
18
+ gem 'ruby_smart-support'
19
+ ```
20
+
21
+ And then execute:
22
+
23
+ $ bundle install
24
+
25
+ Or install it yourself as:
26
+
27
+ $ gem install ruby_smart-support
28
+
29
+ ## Features
30
+ * validate & check gems through GemInfo
31
+ * resolve information about the current ruby's thread through ThreadInfo
32
+ * extensions for Ruby
33
+ * *Array* `#only!`, `#only`
34
+ * *Float* `#round_down`, `#round_up`
35
+ * *Hash* `#to_md5`, `#product`
36
+ * *Object* `#numeric?`, `#boolean?`, `#missing_method?`, `#alias_missing_method`
37
+ * *String* `#to_boolean`, `#to_md5`
38
+
39
+ * extensions for activesupport
40
+ * *Hash* `#only!`, `#without!`, `#deep_reject`
41
+
42
+ * extensions for Rake-Tasks
43
+ * to `append` & `prepend` additional blocks
44
+ * to check task-state with `#invoked?`, `#performed?` & `#running?`
45
+
46
+ -----
47
+
48
+ ## ThreadInfo module
49
+
50
+ The `ThreadInfo` module provides information about the current thread.
51
+
52
+ ### Usage Examples
53
+
54
+ ```ruby
55
+ require 'thread_info'
56
+
57
+ # returns true, if this is a console
58
+ ThreadInfo.console?
59
+ # > true
60
+
61
+ # returns the current thread name (rake / rails)
62
+ ThreadInfo.name
63
+ # > true
64
+
65
+ # returns true if thread has a 'window'
66
+ ThreadInfo.window?
67
+ # > true
68
+
69
+ # returns the thread type string
70
+ ThreadInfo.info
71
+ # > "$534435 [#235] @ console :: RakeTaskName"
72
+ ```
73
+
74
+ ### Available methods
75
+ * .rake?
76
+ * .rails?
77
+ * .console?
78
+ * .irb?
79
+ * .pry?
80
+ * .server?
81
+ * .rails_console?
82
+ * .io_console?
83
+ * .thread?
84
+ * .thread
85
+ * .thread_id
86
+ * .process_object_id
87
+ * .id
88
+ * .name
89
+ * .type
90
+ * .info
91
+ * .windowed?
92
+ * .winsize
93
+ * .stdout?
94
+
95
+ ## GemInfo module
96
+
97
+ The `GemInfo` module provides information about the installed and loaded gems & features.
98
+
99
+ ### Usage Examples
100
+
101
+ ```ruby
102
+ require 'gem_info'
103
+
104
+ # returns a hash of all installed gems with it's versions
105
+ # (gems within the currently installed ruby version)
106
+ GemInfo.installed
107
+ # > {'bundler' => ['2.2.30', '1.2.3'], ...}
108
+
109
+ # returns a hash of all loaded gems with its current version
110
+ # (gems from the Gemfile)
111
+ GemInfo.loaded
112
+ # > {'bundler' => '2.2.30', ...}
113
+
114
+ # returns an array of all active gems
115
+ GemInfo.active
116
+ # > ['bundler', ...]
117
+
118
+ # returns an array of all loaded features
119
+ GemInfo.features
120
+ # > ['active_support','bundler']
121
+
122
+ # safe requires a feature by provided name & optional gem
123
+ GemInfo.safe_require('activesupport')
124
+ # > false
125
+ GemInfo.safe_require('active_support')
126
+ # > true
127
+ GemInfo.safe_require('action_view/helpers/date_helper','actionview', '> 0.1.0')
128
+ # > true
129
+
130
+ # compares two versions against each other
131
+ GemInfo.match?('4.3.0', '4.3.0')
132
+ # > true
133
+ #
134
+ GemInfo.match?('>= 3.0', '4.3.0')
135
+ # > true
136
+ #
137
+ GemInfo.match?( '~> 3.1', '3.3.0')
138
+ # > true
139
+ #
140
+ GemInfo.match?( '~> 1.1.0', '0.1.0')
141
+ # > false
142
+ ```
143
+
144
+ ### Available methods
145
+ * .installed
146
+ * .installed?
147
+ * .loaded
148
+ * .loaded?
149
+ * .active
150
+ * .active?
151
+ * .features
152
+ * .feature?
153
+ * .version
154
+ * .safe_require
155
+ * .match?
156
+
157
+ ## Rake::Task extensions
158
+
159
+ With the new methods `append` & `prepend` you can now patch existing rake tasks.
160
+
161
+ ```ruby
162
+ # lib/tasks/patch_db_migrate.rake
163
+
164
+ namespace :db do
165
+ task(:migrate).prepend do |t|
166
+ if t.invoked?
167
+ puts "This task is invoked by another task!"
168
+ end
169
+
170
+ if t.performed?
171
+ raise "This task already performed - no need to execute again!"
172
+ end
173
+ end
174
+
175
+ task(:migrate).append do |t|
176
+ puts "execution done!"
177
+ end
178
+ end
179
+ ```
180
+
181
+ -----
182
+
183
+ ## Docs
184
+
185
+ [CHANGELOG](./docs/CHANGELOG.md)
186
+
187
+ ## Contributing
188
+
189
+ Bug reports and pull requests are welcome on GitHub at [elasticsearch_record](https://github.com/ruby-smart/support).
190
+ This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](./docs/CODE_OF_CONDUCT.md).
191
+
192
+ ## License
193
+
194
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
195
+
196
+ A copy of the [LICENSE](./docs/LICENSE.txt) can be found @ the docs.
197
+
198
+ ## Code of Conduct
199
+
200
+ Everyone interacting in the project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [CODE OF CONDUCT](./docs/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: :spec
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "ruby_smart/support"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/docs/CHANGELOG.md ADDED
@@ -0,0 +1,15 @@
1
+ # RubySmart::Support - CHANGELOG
2
+
3
+ ## [1.0.0] - 2022-11-08
4
+ * **[add]** `GemInfo` module
5
+ * **[add]** `ThreadInfo` module
6
+ * **[add]** core extensions for ruby
7
+ * **[add]** extensions for rails & rake
8
+ * **[add]** extensions for optional gem 'activesupport' / hash methods
9
+ * **[add]** additional properties to `Rails::Info` - load them after ActiveSupport hooks
10
+ * **[add]** rspec & simplecov
11
+ * **[add]** Github workflow Test
12
+
13
+ ## [0.1.0] - 2022-11-08
14
+ * Initial commit
15
+ * docs, version, structure
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at tg@reimbursement.institute. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/docs/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Ruby Smart
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/lib/gem_info.rb ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'ruby_smart/support/gem_info'
4
+
5
+ # Alias to <tt>ruby_smart/support/gem_info</tt>
6
+ GemInfo = RubySmart::Support::GemInfo
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/core_ext/hash'
4
+ require 'active_support/core_ext/object/deep_dup'
5
+
6
+ unless Hash.method_defined? "only!"
7
+ class Hash
8
+ # Replaces the hash with only the given keys (if exists),
9
+ # but returns the same hash (not the removed keys - this differs to *Hash#slice!*)
10
+ #
11
+ # hsh = {a: 1, b: 2, c: 3}
12
+ # hsh.only!(:a, :d)
13
+ # > {a: 1}
14
+ # > hsh == {a: 1}
15
+ #
16
+ # @param [Object] *keys
17
+ # @return [Hash] self
18
+ def only!(*keys)
19
+ slice!(*keys)
20
+ self
21
+ end
22
+ end
23
+ end
24
+
25
+ unless Hash.method_defined? "without!"
26
+ class Hash
27
+ # removes the given keys from hash and returns those key => value pairs
28
+ # (this differs to *Hash#except!*)
29
+ #
30
+ # hsh = {a: 1, b: 2, c: 3}
31
+ # hsh.without!(:a, :d)
32
+ # > {a: 1}
33
+ # > hsh == {b: 2, c: 3}
34
+ #
35
+ # @param [Object] *keys
36
+ # @return [Hash] self
37
+ def without!(*keys)
38
+ Hash[self.to_a - self.except!(*keys).to_a]
39
+ end
40
+ end
41
+ end
42
+
43
+ unless Hash.method_defined? "deep_reject"
44
+ class Hash
45
+ # returns a new Hash with items that the block evaluates to true removed, also to deep Hashes.
46
+ def deep_reject(&blk)
47
+ self.deep_dup.deep_reject!(&blk)
48
+ end
49
+
50
+ # deep reject by provided block
51
+ # deep remove keys that the block evaluates to true
52
+ #
53
+ # hsh = {a: 1, b: 2, c: 3, d: {a: 1, b: 2, c: 3}}
54
+ # hsh.deep_reject! {|_k,v| v.is_a?(Numeric) && v > 2}
55
+ # > hsh == {a: 1, b: 2, d: {a: 1, b: 2}}
56
+ #
57
+ # hsh = {a: 1, b: 2, c: 3, d: {a: 1, b: 2, c: 3}}
58
+ # hsh.deep_reject! {|k,v| k == :d || v == 2}
59
+ # > hsh == {a: 1, c: 3}
60
+ def deep_reject!(&blk)
61
+ self.each do |k, v|
62
+ if blk.(k, v)
63
+ self.delete(k)
64
+ elsif v.is_a?(Hash)
65
+ v.deep_reject!(&blk)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/info'
4
+
5
+ # add additional property for info
6
+ # The current thread information
7
+ Rails::Info.property 'Thread' do
8
+ ::RubySmart::Support::ThreadInfo.info
9
+ end
10
+
11
+ # The current Bundler version
12
+ Rails::Info.property 'Bundler' do
13
+ ::Bundler::VERSION if defined?(::Bundler::VERSION)
14
+ end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rake/task'
4
+
5
+ module Rake
6
+ class Task
7
+ module StatePatch
8
+ # overwriting the execute method to track the current state
9
+ def execute(*args)
10
+ @state = :running
11
+ super
12
+ @state = :done
13
+ end
14
+ end
15
+
16
+ prepend StatePatch
17
+
18
+ attr_reader :state
19
+
20
+ # returns true, if this task was invoked
21
+ # @return [Boolean]
22
+ def invoked?
23
+ !!@already_invoked
24
+ end
25
+
26
+ # returns true, if this task performed (executed)
27
+ # @return [Boolean]
28
+ def performed?
29
+ @state == :done
30
+ end
31
+
32
+ # returns true, if this task is currently running
33
+ # @return [Boolean]
34
+ def running?
35
+ @state == :running
36
+ end
37
+
38
+ # append the given block to the 'actions'-array
39
+ # this method is chainable and returns self
40
+ #
41
+ # @param [Proc] block
42
+ # @return [Rake::Task] self
43
+ def append(&block)
44
+ @actions << block
45
+ self
46
+ end
47
+
48
+ # prepends the given block to the 'actions'-array
49
+ # this method is chainable and returns self
50
+ #
51
+ # @param [Proc] block
52
+ # @return [Rake::Task] self
53
+ def prepend(&block)
54
+ @actions.unshift(block)
55
+ self
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ unless Array.method_defined? :only!
4
+ class Array
5
+ # refactors the same array with only given values (if exists)
6
+ #
7
+ # @example
8
+ # ary = [:foo, :bar, :bat]
9
+ # ary.only!(:bar, :moon)
10
+ # > ary == [:bar]
11
+ #
12
+ # @param [Object] *values
13
+ # @return [Array] self
14
+ def only!(*values)
15
+ reject! {|value| !values.include?(value)}
16
+ self
17
+ end
18
+ end
19
+ end
20
+
21
+ unless Array.method_defined? :only
22
+ class Array
23
+ # returns a new array with only given values (if exists)
24
+ #
25
+ # @example
26
+ # ary = [:foo, :bar, :bat]
27
+ # ary.only(:bar, :bat, :moon)
28
+ # > [:bar, :bat]
29
+ #
30
+ # @param [Object] *values
31
+ # @return [Array] ary
32
+ def only(*values)
33
+ dup.only!(*values)
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ unless Float.method_defined? :round_down
4
+ class Float
5
+ # returns a new float and rounds down
6
+ #
7
+ # @example
8
+ # nb = 45.5678
9
+ # nb.round_down(2)
10
+ # > 45.56
11
+ #
12
+ # @param [Integer] exp - amount of decimals
13
+ # @return [Float] rounded number
14
+ def round_down(exp = 0)
15
+ multiplier = 10 ** exp
16
+ ((self * multiplier).floor).to_f/multiplier.to_f
17
+ end
18
+ end
19
+ end
20
+
21
+ unless Float.method_defined? :round_up
22
+ class Float
23
+ # returns a new float and rounds up
24
+ #
25
+ # @example
26
+ # nb = 45.5678
27
+ # nb.round_up(2)
28
+ # > 45.57
29
+ #
30
+ # @param [Integer] exp - amount of decimals
31
+ # @return [Float] rounded number
32
+ def round_up(exp = 0)
33
+ multiplier = 10 ** exp
34
+ ((self * multiplier).ceil).to_f/multiplier.to_f
35
+ end
36
+ end
37
+ end