lite-measurements 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: dce6204f52df08a3479f361d7f8612d71205c9d80ebda70e7417a35f7f296e3e
4
+ data.tar.gz: ccbc500e250c208a001959d6724350d4465efff254e9510d9efe072b2b984ccd
5
+ SHA512:
6
+ metadata.gz: 7364280b98ab082c6c9a4ebb7876d6188bf4126c039ab5eb59a28535d25b2ef9d323875d8080268c4b29c32d71b67577a5523dedf512d9180ab51cf41f5ef3ad
7
+ data.tar.gz: 74893ab678f85e53957e03ab6e3f59e308470b3282148ec2baf5edc7bf8182e7fec7aaefced5595c1e022bde09db67c4fc7b4b3fc1e2449b999dc90115a9b386
data/.fasterer.yml ADDED
@@ -0,0 +1,19 @@
1
+ speedups:
2
+ block_vs_symbol_to_proc: true
3
+ each_with_index_vs_while: false
4
+ fetch_with_argument_vs_block: true
5
+ for_loop_vs_each: true
6
+ getter_vs_attr_reader: true
7
+ gsub_vs_tr: true
8
+ hash_merge_bang_vs_hash_brackets: true
9
+ keys_each_vs_each_key: true
10
+ map_flatten_vs_flat_map: true
11
+ module_eval: true
12
+ proc_call_vs_yield: true
13
+ rescue_vs_respond_to: true
14
+ reverse_each_vs_reverse_each: true
15
+ select_first_vs_detect: true
16
+ select_last_vs_reverse_detect: true
17
+ setter_vs_attr_writer: true
18
+ shuffle_first_vs_sample: true
19
+ sort_vs_sort_by: true
data/.gitignore ADDED
@@ -0,0 +1,11 @@
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
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --backtrace
2
+ --color
3
+ --format progress
4
+ --order random
data/.rubocop.yml ADDED
@@ -0,0 +1,24 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rspec
4
+ AllCops:
5
+ TargetRubyVersion: 2.6
6
+ DisplayCopNames: true
7
+ DisplayStyleGuide: true
8
+ LineLength:
9
+ Max: 100
10
+ Layout/EmptyLinesAroundBlockBody:
11
+ Exclude:
12
+ - 'spec/**/**/*'
13
+ Layout/EmptyLinesAroundClassBody:
14
+ EnforcedStyle: empty_lines_except_namespace
15
+ Layout/EmptyLinesAroundModuleBody:
16
+ EnforcedStyle: empty_lines_except_namespace
17
+ Metrics/BlockLength:
18
+ Exclude:
19
+ - 'spec/**/**/*'
20
+ - '*.gemspec'
21
+ Style/Documentation:
22
+ Enabled: false
23
+ Style/ExpandPathArguments:
24
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,24 @@
1
+ sudo: false
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.5
6
+ - 2.6
7
+ - ruby-head
8
+ matrix:
9
+ fast_finish: true
10
+ allow_failures:
11
+ - rvm: ruby-head
12
+ before_install:
13
+ - gem update --system
14
+ - gem install bundler
15
+ install:
16
+ - bundle install --jobs=3 --retry=3
17
+ script:
18
+ - bundle exec rspec
19
+ - bundle exec rubocop
20
+ - bundle exec fasterer
21
+ notifications:
22
+ email: false
23
+ slack:
24
+ secure: kcnsiwyN/hdAvi6qcR8EtDAp7WB0LCIVTVvewGbiN++k7AhZjpvjxjGD8k5IkZie3EcRWQKrA1MoUrhnro85YzYI6UhAx+rUE2fyZFoMV9B1dfQC0Hk/jGM7ZKelipS1yvEAECrpiAb/lifJyQINHQU+GolgHZ7mzfCA3M3MOxq0u1tPVyt6OaPk2r8tDQARbE3WBba3Cx9jUjxoFhS0qCY8/U7Z0P0p6OCpgJQSQiJ+qQ483jbzV4Guxe295MmlaZvrFhgomj5Wd8K1QLfQUZ60mYjCqUUvsA51hCL16H+kSdi+syhTp9KmJJP6lPA+TJN74BGfnoz7Jwhqyi303KDao/iedcxEhOqCpzyM+OijrfARrvvDRikIzfzt074ZOcl7ujm7KZDMF18TJLGYYp9ZU2KjFq9UL3ZEM72oH33U9NaZj2wf3HB3vxNNNfB4ltIAwcucAZXWoQfTrbf03bZx+bvaV7o3+H4+913gbqdafB+NWYtEddO4qBRTV7ashk+8Bbsd5AdCUbXF8OoMQOywm9BJ9+JavgQdugqwuxX6bCoZp7chhYhJ7vhrvGg+3ateuYqfjbW2chUnooNIEObwToeqWxuRYtSQqZb9tdpbmIYiQhg3jjdtVcEWvEu/dciPgRGmubefgO6TjsTnyv0Ka43PgRT3sq5iIsjlTmw=
data/CHANGELOG.md ADDED
@@ -0,0 +1,11 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
5
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [1.0.0] - 2019-07-19
10
+ ### Added
11
+ - Initial project version
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at j.gomez@drexed.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in lite-measurements.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,119 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ lite-measurements (1.0.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ actionpack (5.2.3)
10
+ actionview (= 5.2.3)
11
+ activesupport (= 5.2.3)
12
+ rack (~> 2.0)
13
+ rack-test (>= 0.6.3)
14
+ rails-dom-testing (~> 2.0)
15
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
16
+ actionview (5.2.3)
17
+ activesupport (= 5.2.3)
18
+ builder (~> 3.1)
19
+ erubi (~> 1.4)
20
+ rails-dom-testing (~> 2.0)
21
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
22
+ activesupport (5.2.3)
23
+ concurrent-ruby (~> 1.0, >= 1.0.2)
24
+ i18n (>= 0.7, < 2)
25
+ minitest (~> 5.1)
26
+ tzinfo (~> 1.1)
27
+ ast (2.4.0)
28
+ builder (3.2.3)
29
+ colorize (0.8.1)
30
+ concurrent-ruby (1.1.5)
31
+ crass (1.0.4)
32
+ diff-lcs (1.3)
33
+ erubi (1.8.0)
34
+ fasterer (0.6.0)
35
+ colorize (~> 0.7)
36
+ ruby_parser (>= 3.13.0)
37
+ generator_spec (0.9.4)
38
+ activesupport (>= 3.0.0)
39
+ railties (>= 3.0.0)
40
+ i18n (1.6.0)
41
+ concurrent-ruby (~> 1.0)
42
+ jaro_winkler (1.5.3)
43
+ loofah (2.2.3)
44
+ crass (~> 1.0.2)
45
+ nokogiri (>= 1.5.9)
46
+ method_source (0.9.2)
47
+ mini_portile2 (2.4.0)
48
+ minitest (5.11.3)
49
+ nokogiri (1.10.3)
50
+ mini_portile2 (~> 2.4.0)
51
+ parallel (1.17.0)
52
+ parser (2.6.3.0)
53
+ ast (~> 2.4.0)
54
+ rack (2.0.7)
55
+ rack-test (1.1.0)
56
+ rack (>= 1.0, < 3)
57
+ rails-dom-testing (2.0.3)
58
+ activesupport (>= 4.2.0)
59
+ nokogiri (>= 1.6)
60
+ rails-html-sanitizer (1.0.4)
61
+ loofah (~> 2.2, >= 2.2.2)
62
+ railties (5.2.3)
63
+ actionpack (= 5.2.3)
64
+ activesupport (= 5.2.3)
65
+ method_source
66
+ rake (>= 0.8.7)
67
+ thor (>= 0.19.0, < 2.0)
68
+ rainbow (3.0.0)
69
+ rake (12.3.2)
70
+ rspec (3.8.0)
71
+ rspec-core (~> 3.8.0)
72
+ rspec-expectations (~> 3.8.0)
73
+ rspec-mocks (~> 3.8.0)
74
+ rspec-core (3.8.2)
75
+ rspec-support (~> 3.8.0)
76
+ rspec-expectations (3.8.4)
77
+ diff-lcs (>= 1.2.0, < 2.0)
78
+ rspec-support (~> 3.8.0)
79
+ rspec-mocks (3.8.1)
80
+ diff-lcs (>= 1.2.0, < 2.0)
81
+ rspec-support (~> 3.8.0)
82
+ rspec-support (3.8.2)
83
+ rubocop (0.72.0)
84
+ jaro_winkler (~> 1.5.1)
85
+ parallel (~> 1.10)
86
+ parser (>= 2.6)
87
+ rainbow (>= 2.2.2, < 4.0)
88
+ ruby-progressbar (~> 1.7)
89
+ unicode-display_width (>= 1.4.0, < 1.7)
90
+ rubocop-performance (1.4.0)
91
+ rubocop (>= 0.71.0)
92
+ rubocop-rspec (1.33.0)
93
+ rubocop (>= 0.60.0)
94
+ ruby-progressbar (1.10.1)
95
+ ruby_parser (3.13.1)
96
+ sexp_processor (~> 4.9)
97
+ sexp_processor (4.12.1)
98
+ thor (0.20.3)
99
+ thread_safe (0.3.6)
100
+ tzinfo (1.2.5)
101
+ thread_safe (~> 0.1)
102
+ unicode-display_width (1.6.0)
103
+
104
+ PLATFORMS
105
+ ruby
106
+
107
+ DEPENDENCIES
108
+ bundler
109
+ fasterer
110
+ generator_spec
111
+ lite-measurements!
112
+ rake
113
+ rspec
114
+ rubocop
115
+ rubocop-performance
116
+ rubocop-rspec
117
+
118
+ BUNDLED WITH
119
+ 2.0.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Juan Gomez
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/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # Lite::Measurements
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/lite-measurements.svg)](http://badge.fury.io/rb/lite-measurements)
4
+ [![Build Status](https://travis-ci.org/drexed/lite-measurements.svg?branch=master)](https://travis-ci.org/drexed/lite-measurements)
5
+
6
+ Lite::Measurements is a library for converting measurements to different unit sizes or types.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'lite-measurements'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install lite-measurements
23
+
24
+ ## Table of Contents
25
+
26
+ * [Configurations](#configurations)
27
+ * [Measurements](#measurements)
28
+ * [Monkey patches](#monkey-patches)
29
+
30
+ ## Configurations
31
+
32
+ `rails g lite:measurements:install` will generate the following file:
33
+ `../config/initalizers/lite-measurements.rb`
34
+
35
+ ```ruby
36
+ Lite::Measurements.configure do |config|
37
+ config.monkey_patches = true
38
+ end
39
+ ```
40
+
41
+ ## Measurements
42
+
43
+ * [Digital Storage](https://github.com/drexed/lite-ruby/blob/master/docs/DIGITAL_STORAGE.md)
44
+ * [Length](https://github.com/drexed/lite-ruby/blob/master/docs/LENGTH.md)
45
+ * [Mass](https://github.com/drexed/lite-ruby/blob/master/docs/MASS.md)
46
+ * [Temperature](https://github.com/drexed/lite-ruby/blob/master/docs/TEMPERATURE.md)
47
+ * [Time](https://github.com/drexed/lite-ruby/blob/master/docs/TIME.md)
48
+
49
+ ## Monkey patches
50
+
51
+ Including monkey patches will give you `Numeric` access to conversions.
52
+
53
+ ```ruby
54
+ 2.convert_temperature(from: :fahrenheit, to: :celsius) #=> -16.666666666666668
55
+ ```
56
+
57
+ ## Development
58
+
59
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
60
+
61
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
62
+
63
+ ## Contributing
64
+
65
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/lite-measurements. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
66
+
67
+ ## License
68
+
69
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
70
+
71
+ ## Code of Conduct
72
+
73
+ Everyone interacting in the Lite::Measurements project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/lite-measurements/blob/master/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/_config.yml ADDED
@@ -0,0 +1 @@
1
+ theme: jekyll-theme-leap-day
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 'lite/measurements'
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
@@ -0,0 +1,22 @@
1
+ # Digital Storage
2
+
3
+ Type | Keys
4
+ --- | ---
5
+ Units | `:bytes`, `:kilobytes`, `:megabytes`, `:gigabytes`, `:terabytes`, `:petabytes`, `:exabytes`
6
+
7
+ Option | Type | Default
8
+ --- | --- | ---
9
+ from, to | symbol | ---
10
+
11
+ ```ruby
12
+ klass = Lite::Measurements::DigitalStorage.new(2)
13
+ klass = klass.convert(from: :bytes, to: :kilobytes)
14
+
15
+ # - or -
16
+
17
+ Lite::Measurements::DigitalStorage.convert(2, from: :megabytes, to: :bytes)
18
+
19
+ # - or -
20
+
21
+ 2.convert_digital_storage(from: :terabytes, to: :petabytes)
22
+ ```
data/docs/LENGTH.md ADDED
@@ -0,0 +1,23 @@
1
+ # Length
2
+
3
+ Type | Keys
4
+ --- | ---
5
+ Imperical | `:inches`, `:feet`, `:yards`, `:miles`, `:nautical_miles`
6
+ Metric | `:micrometers`, `:millimeters`, `:centimeters`, `:decimeters`, `:meters`, `:dekameters`, `:hectometers`, `:kilometers`
7
+
8
+ Option | Type | Default
9
+ --- | --- | ---
10
+ from, to | symbol | ---
11
+
12
+ ```ruby
13
+ klass = Lite::Measurements::Length.new(2)
14
+ klass = klass.convert(from: :inches, to: :yards)
15
+
16
+ # - or -
17
+
18
+ Lite::Measurements::Length.convert(2, from: :meters, to: :feet)
19
+
20
+ # - or -
21
+
22
+ 2.convert_length(from: :miles, to: :millimeters)
23
+ ```
data/docs/MASS.md ADDED
@@ -0,0 +1,23 @@
1
+ # Mass
2
+
3
+ Type | Keys
4
+ --- | ---
5
+ Imperical | `:ounces`, `:pounds`, `:stones`, `:us_tons`, `:imperial_tons`
6
+ Metric | `:micrograms`, `:milligrams`, `:centigrams`, `:decigrams`, `:grams`, `:dekagrams`, `:hectograms`, `:kilograms`, `:metric_tons`
7
+
8
+ Option | Type | Default
9
+ --- | --- | ---
10
+ from, to | symbol | ---
11
+
12
+ ```ruby
13
+ klass = Lite::Measurements::Mass.new(2)
14
+ klass = klass.convert(from: :ounces, to: :pounds)
15
+
16
+ # - or -
17
+
18
+ Lite::Measurements::Mass.convert(2, from: :kilograms, to: :us_tons)
19
+
20
+ # - or -
21
+
22
+ 2.convert_mass(from: :pounds, to: :micrograms)
23
+ ```
@@ -0,0 +1,22 @@
1
+ # Temperature
2
+
3
+ Type | Keys
4
+ --- | ---
5
+ Units | `:celsius`, `:fahrenheit`, `:kelvin`
6
+
7
+ Option | Type | Default
8
+ --- | --- | ---
9
+ from, to | symbol | ---
10
+
11
+ ```ruby
12
+ klass = Lite::Measurements::Temperature.new(2)
13
+ klass = klass.convert(from: :fahrenheit, to: :celsius)
14
+
15
+ # - or -
16
+
17
+ Lite::Measurements::Temperature.convert(2, from: :celsius, to: :kelvin)
18
+
19
+ # - or -
20
+
21
+ 2.convert_temperature(from: :kelvin, to: :fahrenheit)
22
+ ```
data/docs/TIME.md ADDED
@@ -0,0 +1,22 @@
1
+ # Time
2
+
3
+ Type | Keys
4
+ --- | ---
5
+ Units | `:milliseconds`, `:seconds`, `:minutes`, `:hours`, `:days`, `:weeks`, `:years`, `:decades`, `:centuries`, `:millenniums`
6
+
7
+ Option | Type | Default
8
+ --- | --- | ---
9
+ from, to | symbol | ---
10
+
11
+ ```ruby
12
+ klass = Lite::Measurements::Time.new(2)
13
+ klass = klass.convert(from: :seconds, to: :minutes)
14
+
15
+ # - or -
16
+
17
+ Lite::Measurements::Time.convert(2, from: :centuries, to: :decades)
18
+
19
+ # - or -
20
+
21
+ 2.convert_time(from: :hours, to: :millenniums)
22
+ ```
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/generators'
4
+
5
+ module Lite
6
+ module Measurements
7
+ class InstallGenerator < Rails::Generators::Base
8
+
9
+ source_root File.expand_path('../templates', __FILE__)
10
+
11
+ def copy_initializer_file
12
+ copy_file('install.rb', 'config/initializers/lite-measurements.rb')
13
+ end
14
+
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Lite::Measurements.configure do |config|
4
+ config.monkey_patches = true
5
+ end
Binary file
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class Base
6
+
7
+ attr_accessor :amount
8
+
9
+ def initialize(amount)
10
+ @amount = amount
11
+ end
12
+
13
+ class << self
14
+
15
+ def convert(amount, from:, to:)
16
+ klass = new(amount)
17
+ klass.convert(from: from, to: to)
18
+ end
19
+
20
+ end
21
+
22
+ private
23
+
24
+ def assert_all_valid_keys!(from, to, valid_keys)
25
+ [from, to].each { |key| assert_valid_keys!(key, valid_keys) }
26
+ end
27
+
28
+ # rubocop:disable Style/GuardClause
29
+ def assert_valid_keys!(key, valid_keys)
30
+ if valid_keys.empty?
31
+ raise ArgumentError, 'Missing key: conversion key must be provided'
32
+ elsif !valid_keys.include?(key)
33
+ raise ArgumentError,
34
+ "Invalid key: #{key.inspect}." \
35
+ "Allowed keys are: #{valid_keys.map(&:inspect).join(', ')}"
36
+ end
37
+ end
38
+ # rubocop:enable Style/GuardClause
39
+
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+
6
+ class Configuration
7
+
8
+ attr_accessor :monkey_patches
9
+
10
+ def initialize
11
+ @monkey_patches = true
12
+ end
13
+
14
+ end
15
+
16
+ def self.configuration
17
+ @configuration ||= Configuration.new
18
+ end
19
+
20
+ def self.configuration=(config)
21
+ @configuration = config
22
+ end
23
+
24
+ def self.configure
25
+ yield(configuration)
26
+ end
27
+
28
+ end
29
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class DigitalStorage < Lite::Measurements::Base
6
+
7
+ include Lite::Measurements::Helpers::ShiftHelper
8
+
9
+ DIGITAL_STORAGE_UNITS ||= {
10
+ bytes: 1.0, kilobytes: 1024.0, megabytes: 1024.0**2, gigabytes: 1024.0**3,
11
+ terabytes: 1024.0**4, petabytes: 1024.05**5, exabytes: 1024**6
12
+ }.freeze
13
+
14
+ def convert(from:, to:)
15
+ assert_all_valid_keys!(from, to, DIGITAL_STORAGE_UNITS.keys)
16
+ return amount if equal_units?(from, to)
17
+
18
+ shift_units(amount, type: DIGITAL_STORAGE_UNITS, from: from, to: to)
19
+ end
20
+
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,57 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ module Helpers
6
+ module ConversionHelper
7
+
8
+ private
9
+
10
+ def all_keys
11
+ [imperical_keys, metric_keys].flatten
12
+ end
13
+
14
+ # rubocop:disable Metrics/LineLength
15
+ def convert_to_imperical_units(units, from: nil, convert_to:, convert_from:, to: nil)
16
+ units = shift_units(units, type: klass::METRIC_UNITS, from: from, to: convert_to)
17
+ shift_units(units / klass::CONVERTER, type: klass::IMPERICAL_UNITS, from: convert_from, to: to)
18
+ end
19
+
20
+ def convert_to_metric_units(units, from: nil, convert_to:, convert_from:, to: nil)
21
+ units = shift_units(units, type: klass::IMPERICAL_UNITS, from: from, to: convert_to)
22
+ shift_units(units * klass::CONVERTER, type: klass::METRIC_UNITS, from: convert_from, to: to)
23
+ end
24
+ # rubocop:enable Metrics/LineLength
25
+
26
+ def convert_to_imperical_units?(from, to)
27
+ metric_keys.include?(from) && imperical_keys.include?(to)
28
+ end
29
+
30
+ def convert_to_metric_units?(from, to)
31
+ imperical_keys.include?(from) && metric_keys.include?(to)
32
+ end
33
+
34
+ def klass
35
+ self.class
36
+ end
37
+
38
+ def imperical_keys
39
+ @imperical_keys ||= klass::IMPERICAL_UNITS.keys
40
+ end
41
+
42
+ def metric_keys
43
+ @metric_keys ||= klass::METRIC_UNITS.keys
44
+ end
45
+
46
+ def shift_between_imperical_units?(from, to)
47
+ imperical_keys.include?(from) && imperical_keys.include?(to)
48
+ end
49
+
50
+ def shift_between_metric_units?(from, to)
51
+ metric_keys.include?(from) && metric_keys.include?(to)
52
+ end
53
+
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ module Helpers
6
+ module ShiftHelper
7
+
8
+ private
9
+
10
+ def change_units(units, type:, from: nil, to: nil)
11
+ if from
12
+ units * type[from]
13
+ else
14
+ units / type[to]
15
+ end
16
+ end
17
+
18
+ def equal_units?(from, to)
19
+ from == to
20
+ end
21
+
22
+ def shift_units(units, type:, from: nil, to: nil)
23
+ units = change_units(units, type: type, from: from)
24
+ change_units(units, type: type, to: to)
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class Length < Lite::Measurements::Base
6
+
7
+ include Lite::Measurements::Helpers::ConversionHelper
8
+ include Lite::Measurements::Helpers::ShiftHelper
9
+
10
+ CONVERTER ||= 0.0254
11
+
12
+ IMPERICAL_UNITS ||= {
13
+ inches: 1.0, feet: 12.0, yards: 36.0, miles: 63_360.0, nautical_miles: 72_913.386
14
+ }.freeze
15
+ METRIC_UNITS ||= {
16
+ micrometers: 0.000001, millimeters: 0.001, centimeters: 0.01, decimeters: 0.1, meters: 1.0,
17
+ dekameters: 10.0, hectometers: 100.0, kilometers: 1_000.0
18
+ }.freeze
19
+
20
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
21
+ def convert(from:, to:)
22
+ assert_all_valid_keys!(from, to, all_keys)
23
+
24
+ if equal_units?(from, to)
25
+ amount
26
+ elsif shift_between_imperical_units?(from, to)
27
+ shift_units(amount, type: IMPERICAL_UNITS, from: from, to: to)
28
+ elsif shift_between_metric_units?(from, to)
29
+ shift_units(amount, type: METRIC_UNITS, from: from, to: to)
30
+ elsif convert_to_metric_units?(from, to)
31
+ convert_to_metric_units(amount, from: from, convert_to: :inches, convert_from: :meters, to: to)
32
+ else
33
+ convert_to_imperical_units(amount, from: from, convert_to: :meters, convert_from: :inches, to: to)
34
+ end
35
+ end
36
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class Mass < Lite::Measurements::Base
6
+
7
+ include Lite::Measurements::Helpers::ConversionHelper
8
+ include Lite::Measurements::Helpers::ShiftHelper
9
+
10
+ CONVERTER ||= 28.349523125
11
+
12
+ IMPERICAL_UNITS ||= {
13
+ ounces: 1.0, pounds: 16.0, stones: 224.0, us_tons: 32_000.0, imperial_tons: 35_840.0
14
+ }.freeze
15
+ METRIC_UNITS ||= {
16
+ micrograms: 0.000001, milligrams: 0.001, centigrams: 0.01, decigrams: 0.1, grams: 1.0,
17
+ dekagrams: 10.0, hectograms: 100.0, kilograms: 1_000.0, metric_tons: 1_000_000.0
18
+ }.freeze
19
+
20
+ # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
21
+ def convert(from:, to:)
22
+ assert_all_valid_keys!(from, to, all_keys)
23
+
24
+ if equal_units?(from, to)
25
+ amount
26
+ elsif shift_between_imperical_units?(from, to)
27
+ shift_units(amount, type: IMPERICAL_UNITS, from: from, to: to)
28
+ elsif shift_between_metric_units?(from, to)
29
+ shift_units(amount, type: METRIC_UNITS, from: from, to: to)
30
+ elsif convert_to_metric_units?(from, to)
31
+ convert_to_metric_units(amount, from: from, convert_to: :ounces, convert_from: :grams, to: to)
32
+ else
33
+ convert_to_imperical_units(amount, from: from, convert_to: :grams, convert_from: :ounces, to: to)
34
+ end
35
+ end
36
+ # rubocop:enable Metrics/AbcSize, Metrics/MethodLength, Metrics/LineLength
37
+
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Numeric
4
+
5
+ {
6
+ digital_storage: Lite::Measurements::DigitalStorage,
7
+ length: Lite::Measurements::Length,
8
+ mass: Lite::Measurements::Mass,
9
+ temperature: Lite::Measurements::Temperature,
10
+ time: Lite::Measurements::Time
11
+ }.each do |name, klass|
12
+ define_method("convert_#{name}") do |from:, to:|
13
+ klass.convert(self, from: from, to: to)
14
+ end
15
+ end
16
+
17
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class Temperature < Lite::Measurements::Base
6
+
7
+ C_FREEZING_POINT ||= 5.0 / 9.0
8
+ C_INVERSED_POINT ||= 9.0 / 5.0
9
+ F_FREEZING_POINT ||= 32.0
10
+ K_FREEZING_POINT ||= 273.15
11
+
12
+ VALID_KEYS ||= %i[
13
+ celsius fahrenheit kelvin
14
+ ].freeze
15
+
16
+ def convert(from:, to:)
17
+ assert_all_valid_keys!(from, to, VALID_KEYS)
18
+
19
+ case to
20
+ when from then amount
21
+ when :celsius then send("celsius_from_#{from}")
22
+ when :fahrenheit then send("fahrenheit_from_#{from}")
23
+ when :kelvin then send("kelvin_from_#{from}")
24
+ end
25
+ end
26
+
27
+ private
28
+
29
+ def celsius_from_fahrenheit
30
+ (amount - F_FREEZING_POINT) * C_FREEZING_POINT
31
+ end
32
+
33
+ def celsius_from_kelvin
34
+ amount - K_FREEZING_POINT
35
+ end
36
+
37
+ def fahrenheit_from_celsius
38
+ (amount * C_INVERSED_POINT) + F_FREEZING_POINT
39
+ end
40
+
41
+ def fahrenheit_from_kelvin
42
+ celsius_from_kelvin * C_INVERSED_POINT + F_FREEZING_POINT
43
+ end
44
+
45
+ def kelvin_from_celsius
46
+ amount + K_FREEZING_POINT
47
+ end
48
+
49
+ def kelvin_from_fahrenheit
50
+ celsius_from_fahrenheit + K_FREEZING_POINT
51
+ end
52
+
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+ class Time < Lite::Measurements::Base
6
+
7
+ include Lite::Measurements::Helpers::ShiftHelper
8
+
9
+ TIME_UNITS ||= {
10
+ milliseconds: 0.001, seconds: 1.0, minutes: 60.0, hours: 3600.0, days: 86_400.0,
11
+ weeks: 604_800.0, years: 31_557_600.0, decades: 31_557_600.0 * 10.0,
12
+ centuries: 31_557_600.0 * 100.0, millenniums: 31_557_600.0 * 1_000.0
13
+ }.freeze
14
+
15
+ def convert(from:, to:)
16
+ assert_all_valid_keys!(from, to, TIME_UNITS.keys)
17
+ return amount if equal_units?(from, to)
18
+
19
+ shift_units(amount, type: TIME_UNITS, from: from, to: to)
20
+ end
21
+
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Lite
4
+ module Measurements
5
+
6
+ VERSION ||= '1.0.0'
7
+
8
+ end
9
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'lite/measurements/version'
4
+ require 'lite/measurements/configuration'
5
+ require 'lite/measurements/helpers/conversion_helper'
6
+
7
+ %w[conversion shift].each do |filename|
8
+ require "lite/measurements/helpers/#{filename}_helper"
9
+ end
10
+
11
+ %w[base digital_storage length mass temperature time].each do |filename|
12
+ require "lite/measurements/#{filename}"
13
+ end
14
+
15
+ require 'lite/measurements/monkey_patches' if Lite::Measurements.configuration.monkey_patches
16
+
17
+ require 'generators/lite/measurements/install_generator'
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'lite/measurements/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'lite-measurements'
9
+ spec.version = Lite::Measurements::VERSION
10
+ spec.authors = ['Juan Gomez']
11
+ spec.email = %w[j.gomez@drexed.com]
12
+
13
+ spec.summary = 'Convert measurements to different unit sizes or types'
14
+ spec.homepage = 'http://drexed.github.io/lite-measurements'
15
+ spec.license = 'MIT'
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata.merge(
21
+ 'allowed_push_host' => 'https://rubygems.org',
22
+ 'changelog_uri' => 'https://github.com/drexed/lite-measurements/blob/master/CHANGELOG.md',
23
+ 'homepage_uri' => spec.homepage,
24
+ 'source_code_uri' => 'https://github.com/drexed/lite-measurements'
25
+ )
26
+ else
27
+ raise 'RubyGems 2.0 or newer is required to protect against ' \
28
+ 'public gem pushes.'
29
+ end
30
+
31
+ # Specify which files should be added to the gem when it is released.
32
+ # The `git ls-files -z` loads the files in the MeasurementsGem that have been added into git.
33
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
34
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
35
+ end
36
+ spec.bindir = 'exe'
37
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
38
+ spec.require_paths = %w[lib]
39
+
40
+ spec.add_development_dependency 'bundler'
41
+ spec.add_development_dependency 'fasterer'
42
+ spec.add_development_dependency 'generator_spec'
43
+ spec.add_development_dependency 'rake'
44
+ spec.add_development_dependency 'rspec'
45
+ spec.add_development_dependency 'rubocop'
46
+ spec.add_development_dependency 'rubocop-performance'
47
+ spec.add_development_dependency 'rubocop-rspec'
48
+ end
metadata ADDED
@@ -0,0 +1,191 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: lite-measurements
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Juan Gomez
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-07-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: fasterer
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: generator_spec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rubocop-performance
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop-rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ - j.gomez@drexed.com
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - ".fasterer.yml"
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".rubocop.yml"
136
+ - ".travis.yml"
137
+ - CHANGELOG.md
138
+ - CODE_OF_CONDUCT.md
139
+ - Gemfile
140
+ - Gemfile.lock
141
+ - LICENSE.txt
142
+ - README.md
143
+ - Rakefile
144
+ - _config.yml
145
+ - bin/console
146
+ - bin/setup
147
+ - docs/DIGITAL_STORAGE.md
148
+ - docs/LENGTH.md
149
+ - docs/MASS.md
150
+ - docs/TEMPERATURE.md
151
+ - docs/TIME.md
152
+ - lib/generators/lite/measurements/install_generator.rb
153
+ - lib/generators/lite/measurements/templates/install.rb
154
+ - lib/lite/measurements.rb
155
+ - lib/lite/measurements/.DS_Store
156
+ - lib/lite/measurements/base.rb
157
+ - lib/lite/measurements/configuration.rb
158
+ - lib/lite/measurements/digital_storage.rb
159
+ - lib/lite/measurements/helpers/conversion_helper.rb
160
+ - lib/lite/measurements/helpers/shift_helper.rb
161
+ - lib/lite/measurements/length.rb
162
+ - lib/lite/measurements/mass.rb
163
+ - lib/lite/measurements/monkey_patches.rb
164
+ - lib/lite/measurements/temperature.rb
165
+ - lib/lite/measurements/time.rb
166
+ - lib/lite/measurements/version.rb
167
+ - lite-measurements.gemspec
168
+ homepage: http://drexed.github.io/lite-measurements
169
+ licenses:
170
+ - MIT
171
+ metadata: {}
172
+ post_install_message:
173
+ rdoc_options: []
174
+ require_paths:
175
+ - lib
176
+ required_ruby_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - ">="
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ required_rubygems_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ requirements: []
187
+ rubygems_version: 3.0.4
188
+ signing_key:
189
+ specification_version: 4
190
+ summary: Convert measurements to different unit sizes or types
191
+ test_files: []