fuelator 0.1.3 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: d77c1686b54d71b05bf14019a0836fdb1792153f
4
- data.tar.gz: 8ef9a1116108e0e74f02fbc0a32d879982c32008
2
+ SHA256:
3
+ metadata.gz: f14896dfaa72e8e708d299a5d9a2d92d86d26379bb146f9d4efdf0b97b02567e
4
+ data.tar.gz: b968191c04bfb2d9b892a635571036a52f6366239739feb3ecbbf042311e311d
5
5
  SHA512:
6
- metadata.gz: 58090a226ecd35fc4e9e44a75d6e2a970ba037abc4007a635067a8ff6733c56aa23dba796a4091e01007afec4d7b05589c286be6fdb6d6eb656c08cdd90e61b7
7
- data.tar.gz: 12f7ea9902bb986c56ceb814a1925afec374d6ac654789c78d07cc18c1fcafe7b80939425e9daddeb2e02a42d86f614ecfefe2de11b4abd2ee4370939d44f27c
6
+ metadata.gz: 4732f1251452d02ca01d76a8c710c6813c8a5c364db3fc981d5c46ea1634caf34e3b16367b84d92dbe7d71c2282b4906a9ddf2150ac1020dfe34a1754140dee9
7
+ data.tar.gz: 3315e505c1fb8e3227a0c7318c1d1296b892e9dcc0145bdf34c2b937f823468f4046bf54e3a0265d3815bc43d21f0889c79318382ce81b8f6c8dae0b2241a68b
@@ -1,6 +1,14 @@
1
- ---
2
1
  language: ruby
3
2
  cache: bundler
4
3
  rvm:
5
- - 2.7.1
4
+ - 2.7.1
6
5
  before_install: gem install bundler -v 2.1.4
6
+ deploy:
7
+ provider: rubygems
8
+ api_key:
9
+ secure: MoYFYQizeO0Ii291702jmbSVPUdzPUzvql3jvyaicMvQGeKFVFzlEHAUedzolUvb8hlC83MQ2PzfxRZawhv7zZuMGFbtMyxF/3PyrZLey3U9eOFuaGal/ZRl7VL8ZAILLdjTR7y3E8LnTWZDmPJBQFhJIRKYQnv1DjxHSV3JYx1eEA1HgnDGlfCALcNsGmLWPhv5aiuSfUjtkJT0EB2nGgfvsyZ9+qzLMfy9w1cXwnytDAjruwSzUnvLEjO3VA5DDIzi74kbWVEM16HIJYtq5wwf9BZ29AYRzdcev/FQwxWcCo/mVLzV1NsCCRZUeeHu8RQt2GVnNFgu/ojMdHDJTtEw8fIfQ8jmKUlD6uDriFBxc/nWaTtvTFX1aaSCV/SyeJhF7YiXF/S6+OiJbe6VMnWPehmie0YE2bMXR90BQlsNE+EcbIgdArOVMX9GMinv60rfJudQMcd4+obaVPjOtmEQEVXbbQLSNgU/qh+ZPyzJIHDgDH2Z22zqor0p5rYIdO3pTOgLnY/afTnmt6nZn2S/oCJyNMJZ8sDHzkEU8A6kpyvK3k1n8S3y0/74SeWHf9dDbWju/+QA/PFpkVcN5jqoS/djbPAJzv494ytrnNyBbgByHaEY1znFl40hjLryIWpUHo3wedQW7NmZaZtUEClZRwtwmA/jeVq/9NYDr8E=
10
+ gem: fuelator
11
+ on:
12
+ tags: true
13
+ repo: 645383/fuelator
14
+ skip_cleanup: 'true'
data/Gemfile CHANGED
@@ -5,3 +5,4 @@ gemspec
5
5
 
6
6
  gem "rake", "~> 12.0"
7
7
  gem "rspec", "~> 3.0"
8
+ gem 'pry'
@@ -1,12 +1,17 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fuelator (0.1.3)
4
+ fuelator (0.2.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
+ coderay (1.1.3)
9
10
  diff-lcs (1.3)
11
+ method_source (1.0.0)
12
+ pry (0.13.1)
13
+ coderay (~> 1.1)
14
+ method_source (~> 1.0)
10
15
  rake (12.3.3)
11
16
  rspec (3.8.0)
12
17
  rspec-core (~> 3.8.0)
@@ -27,6 +32,7 @@ PLATFORMS
27
32
 
28
33
  DEPENDENCIES
29
34
  fuelator!
35
+ pry
30
36
  rake (~> 12.0)
31
37
  rspec (~> 3.0)
32
38
 
data/README.md CHANGED
@@ -22,7 +22,19 @@ Or install it yourself as:
22
22
  ## Usage
23
23
  If you plan a trip to Moon, once you are at fuel station, run:
24
24
 
25
- $ fuelator 28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]]
25
+ $ fuelator -m 28801 -p launch,9.807 -p land,1.62 -p launch,1.62 -p land,9.807
26
+
27
+ Or use API provided:
28
+ ```ruby
29
+ require 'fuelator'
30
+
31
+ Fuelator.calculate(28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]])
32
+ # => 51898
33
+ ```
34
+ Be ready do handle errors:
35
+ ```ruby
36
+ Fuelator::Parameters::Error
37
+ ```
26
38
 
27
39
  ## Development
28
40
 
@@ -1,47 +1,85 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
3
  require "fuelator"
4
- require 'optionparser'
4
+ require 'optparse'
5
5
 
6
- def log(object)
7
- STDERR.puts object
8
- end
6
+ class OptsParser
7
+ Version = Fuelator::VERSION
8
+
9
+ class Options
10
+ attr_accessor :verbose, :mass, :pairs
11
+
12
+ def initialize
13
+ @verbose = false
14
+ @mass = 0
15
+ @pairs = []
16
+ end
17
+ end
9
18
 
10
- verbose = false
19
+ def self.parse
20
+ @options = Options.new
21
+ parser.parse!
22
+ @options
23
+ end
11
24
 
12
- optparse = OptionParser.new do |opts|
13
- opts.banner = "Usage: fuelator [args]"
25
+ def self.parser
26
+ @parser ||= OptionParser.new do |parser|
27
+ parser.banner = "Usage: fuelator [options]"
28
+ parser.separator ""
29
+ parser.separator "Specific options:"
14
30
 
15
- opts.separator "Args sample: 28801 launch 9.807 land 1.62 launch 1.62 land 9.807"
16
- opts.separator "Where 28801 - flight ship mass"
17
- opts.separator "directive/gravity pairs:"
18
- opts.separator " launch - flight directive"
19
- opts.separator " 1.62 - target planet gravity"
20
- opts.separator "Add as many directive/gravity pairs as you wish. Note, you can not pass 2 same directives in a sequence."
31
+ boolean_verbose_option parser
32
+ float_mass_option(parser)
33
+ list_pairs_option(parser)
21
34
 
22
- opts.on("-v", "--verbose", "Verbose mode") do |v|
23
- verbose = v
35
+ parser.separator ""
36
+ parser.separator "Common options:"
37
+ # No argument, shows at tail. This will print an options summary.
38
+ # Try it and see!
39
+ parser.on_tail("-h", "--help", "Show this message") do
40
+ puts parser
41
+ exit
42
+ end
43
+ # Another typical switch to print the version.
44
+ parser.on_tail("--version", "Show version") do
45
+ puts Version
46
+ exit
47
+ end
48
+ end
24
49
  end
25
- end
26
- optparse.parse!
27
50
 
28
- def run(params, verbose)
29
- if params.count == 0
30
- log("run `fuelator -h` for help")
31
- return
51
+ def self.boolean_verbose_option(parser)
52
+ # Boolean switch.
53
+ parser.on("-v", "--[no-]verbose", "Show error backtrace") do |v|
54
+ @options.verbose = v
55
+ end
56
+ end
57
+
58
+ def self.float_mass_option(parser)
59
+ # Mass required float option.
60
+ parser.on("-m", "--mass MASS", Float, "Space ship mass") do |v|
61
+ @options.mass = v
62
+ end
63
+ end
64
+
65
+ def self.list_pairs_option(parser)
66
+ # List of arguments.
67
+ parser.on("-p", "--pair PAIRS", Array, "Directive/gravity comma separated pairs: -p launch,9.2 -p land,2.2") do |list|
68
+ list = list.map { |v| (!!Float(v) rescue false) ? v.to_f : v.to_sym }
69
+ @options.pairs << list
70
+ end
32
71
  end
33
- result = Fuelator.calculate(params)
34
- log "#{result.to_i} kg"
35
- rescue Fuelator::Error, Fuelator::Parameters::Error => e
36
- log_error(e, verbose)
37
- rescue StandardError => e
38
- log_error(e, verbose)
39
72
  end
40
73
 
41
- def log_error(e, verbose)
42
- log e
43
- log e.backtrace if verbose
44
- false
74
+ def run_script
75
+ options = OptsParser.parse
76
+ begin
77
+ puts "#{Fuelator::Calc.new(options.mass, options.pairs).run} kg"
78
+ rescue Fuelator::Parameters::Error, StandardError => e
79
+ puts e
80
+ puts e.backtrace if options.verbose
81
+ end
45
82
  end
46
83
 
47
- exit 1 unless run ARGV, verbose
84
+ ARGV << '-h' if ARGV.empty?
85
+ run_script
@@ -1,56 +1,8 @@
1
1
  require "fuelator/version"
2
+ require 'fuelator/parameters/validator'
3
+ require 'fuelator/parameters/pair'
2
4
  require "fuelator/parameters"
3
- # Calculation of fuel required for flight based on input params
5
+ require "fuelator/calc"
4
6
 
5
7
  module Fuelator
6
- # Calculate fuel for a flight
7
- #
8
- # Example:
9
- # >> Fuelator.calculate(28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]])
10
- # => 51898
11
-
12
- class Error < StandardError; end
13
-
14
- FLIGHT_CONSTANTS = {
15
- launch: {
16
- theta: 0.042,
17
- bias: 33,
18
- },
19
- land: {
20
- theta: 0.033,
21
- bias: 42,
22
- },
23
- }
24
-
25
- # @param [Array] params
26
- # [123, [[:launch, 123]]] or flatten [123, :launch, 123]
27
- # params are validated and validation error may be raised
28
- # @return Float - fuel for a flight
29
- def self.calculate(*params)
30
- parameters = Parameters.new(params)
31
-
32
- full_mass = parameters.mass
33
-
34
- parameters.dir_grav_pairs.reverse.map do |val|
35
- full_mass += recursive_calculate(full_mass, val)
36
- end.last - parameters.mass
37
- end
38
-
39
- private
40
-
41
- def self.recursive_calculate(mass, params)
42
- directive = params[0]
43
- gravity = params[1]
44
- constants = FLIGHT_CONSTANTS[directive]
45
-
46
- begin
47
- fuel_mass = (mass * gravity * constants[:theta] - constants[:bias]).to_i # rounded down
48
- rescue StandardError => e
49
- raise Error.new(e.message)
50
- end
51
-
52
- return 0 if fuel_mass <= 0
53
- result = recursive_calculate(fuel_mass, params)
54
- fuel_mass + result
55
- end
56
8
  end
@@ -0,0 +1,46 @@
1
+ # Calculation of fuel required for flight based on input params
2
+
3
+ module Fuelator
4
+ # Calculate fuel for a flight
5
+ #
6
+ # Example:
7
+ # >> Fuelator.Calc.new(28801, [[:launch, 9.807], [:land, 1.62], [:launch, 1.62], [:land, 9.807]]).run
8
+ # => 51898
9
+ class Calc
10
+ attr_reader :parameters
11
+
12
+ FLIGHT_CONSTANTS = {
13
+ launch: {
14
+ theta: 0.042,
15
+ bias: 33,
16
+ },
17
+ land: {
18
+ theta: 0.033,
19
+ bias: 42,
20
+ },
21
+ }
22
+
23
+ def initialize(mass, pairs)
24
+ @parameters = Parameters.new(mass, pairs)
25
+ end
26
+
27
+ def run
28
+ full_mass = parameters.mass
29
+
30
+ parameters.reversed.map do |val|
31
+ full_mass += recursive_calculate(full_mass, val)
32
+ end.last - parameters.mass
33
+ end
34
+
35
+ private
36
+
37
+ def recursive_calculate(mass, pair)
38
+ constants = FLIGHT_CONSTANTS[pair.directive]
39
+ fuel_mass = (mass * pair.gravity * constants[:theta] - constants[:bias]).to_i # rounded down
40
+
41
+ return 0 if fuel_mass <= 0
42
+ result = recursive_calculate(fuel_mass, pair)
43
+ fuel_mass + result
44
+ end
45
+ end
46
+ end
@@ -1,41 +1,48 @@
1
- require 'fuelator/parameters/error'
2
1
  # For flight parameters validation purposes
3
2
 
4
3
  module Fuelator
5
4
  class Parameters
6
- attr_reader :mass, :dir_grav_pairs
5
+ include Validator
7
6
 
8
- def initialize(params)
9
- params = normalize(params)
10
- @mass = params[0]
11
- @dir_grav_pairs = params[1..-1].each_slice(2).to_a
7
+ class Error < StandardError; end
8
+
9
+ attr_reader :mass, :pairs, :order
10
+
11
+ def initialize(mass, pairs)
12
+ @mass = mass
13
+ @pairs = pairs.map { |pair| Pair.new(pair) }
14
+ @order = define_order
12
15
 
13
16
  validate!
14
17
  end
15
18
 
19
+ def reversed
20
+ pairs.reverse
21
+ end
22
+
16
23
  private
17
24
 
18
- def normalize(params)
19
- params.flatten.map { |v| (!!Float(v) rescue false) ? v.to_f : v.to_sym }
25
+ def valid_mass?
26
+ !mass.nil? && mass.is_a?(Numeric)
20
27
  end
21
28
 
22
- def validate!
23
- error(:mass, mass) unless mass.is_a?(Float)
24
- validate_pairs!
29
+ def valid_pairs?
30
+ !pairs.empty?
25
31
  end
26
32
 
27
- def validate_pairs!
28
- dir = nil
29
- dir_grav_pairs.each do |pair|
30
- error(:pairs, pair.count) if (pair.count % 2) != 0
31
- error(:format, pair[0], pair[1]) unless pair[0].is_a?(Symbol) && pair[1].is_a?(Float)
32
- error(:sequence, dir, pair[0]) if dir == pair[0] # no same directives allowed in a sequence
33
- dir = pair[0]
34
- end
33
+ def valid_order?
34
+ !order
35
35
  end
36
36
 
37
- def error(type, *args)
38
- raise Error.new Error::MESSAGES[type] % args
37
+ # you can only calculate if directives passed in right order - :launch->:land
38
+ def define_order
39
+ dir = nil
40
+ pairs.map do |pair|
41
+ return true if dir == pair.directive
42
+
43
+ dir = pair.directive
44
+ end
45
+ false
39
46
  end
40
47
  end
41
48
  end
@@ -0,0 +1,25 @@
1
+ module Fuelator
2
+ class Parameters
3
+ class Pair
4
+ include Validator
5
+ DIRECTIVES = [:launch, :land]
6
+
7
+ attr_reader :directive, :gravity
8
+
9
+ def initialize(pair)
10
+ @directive = pair[0]
11
+ @gravity = pair[1]
12
+
13
+ validate!
14
+ end
15
+
16
+ def valid_directive?
17
+ DIRECTIVES.include?(directive) && directive.is_a?(Symbol)
18
+ end
19
+
20
+ def valid_gravity?
21
+ !gravity.nil? && gravity.is_a?(Numeric)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module Fuelator
2
+ class Parameters
3
+ module Validator
4
+
5
+ # runs #valid_[variable]? method for every instance variable
6
+ # raises Error defined in parent namespace
7
+ def validate!
8
+ instance_variables.each do |v|
9
+ v_name = v.to_s.gsub(/^@/, '')
10
+ raise Error.new("value for #{v_name} is invalid") unless send("valid_#{v_name}?")
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,3 +1,3 @@
1
1
  module Fuelator
2
- VERSION = "0.1.3"
2
+ VERSION = "0.2.1"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fuelator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergii Brytiuk
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-07-24 00:00:00.000000000 Z
11
+ date: 2020-07-27 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: |-
14
14
  The goal of this application is to calculate fuel
@@ -26,10 +26,8 @@ files:
26
26
  - ".gitignore"
27
27
  - ".rspec"
28
28
  - ".travis.yml"
29
- - CODE_OF_CONDUCT.md
30
29
  - Gemfile
31
30
  - Gemfile.lock
32
- - LICENSE.txt
33
31
  - README.md
34
32
  - Rakefile
35
33
  - bin/console
@@ -37,8 +35,10 @@ files:
37
35
  - bin/setup
38
36
  - fuelator.gemspec
39
37
  - lib/fuelator.rb
38
+ - lib/fuelator/calc.rb
40
39
  - lib/fuelator/parameters.rb
41
- - lib/fuelator/parameters/error.rb
40
+ - lib/fuelator/parameters/pair.rb
41
+ - lib/fuelator/parameters/validator.rb
42
42
  - lib/fuelator/version.rb
43
43
  homepage: https://github.com/645383/fuelator
44
44
  licenses:
@@ -61,10 +61,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
61
61
  - !ruby/object:Gem::Version
62
62
  version: '0'
63
63
  requirements: []
64
- rubyforge_project:
65
- rubygems_version: 2.5.2
64
+ rubygems_version: 3.0.8
66
65
  signing_key:
67
66
  specification_version: 4
68
67
  summary: They do not trust Elon Musk any more
69
68
  test_files: []
70
- has_rdoc:
@@ -1,74 +0,0 @@
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 sbrytiuk@lgscout.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 [https://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: https://contributor-covenant.org
74
- [version]: https://contributor-covenant.org/version/1/4/
@@ -1,21 +0,0 @@
1
- The MIT License (MIT)
2
-
3
- Copyright (c) 2020 Sergii Brytiuk
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.
@@ -1,15 +0,0 @@
1
- # Flight parameters validation error,
2
- # Add error descriptive message to map of messages when new validation case added
3
-
4
- module Fuelator
5
- class Parameters
6
- class Error < StandardError
7
- MESSAGES = {
8
- format: "directive/gravity pairs should be in format string/float: directive: %s, gravity: %s",
9
- sequence: "you can not pass 2 same directives in a sequence: %s => %s",
10
- mass: "mass should be float: %s",
11
- pairs: "provide pairs for directive/gravity: %s elements"
12
- }
13
- end
14
- end
15
- end