All Files
(77.55%
covered at
2.55
hits/line)
5 files in total.
98 relevant lines.
76 lines covered and
22 lines missed
-
# frozen_string_literal: true
-
-
1
require_relative 'interactive_menu'
-
1
require_relative 'fraction_list'
-
-
1
class Application
-
1
def initialize
-
@fraction_list = FractionList.new
-
end
-
-
1
def read_data_from_file
-
puts('Please input the name of the file to read data from')
-
print('> ')
-
file_name = $stdin.gets.strip
-
@fraction_list.read_from_file(file_name)
-
end
-
-
1
def run
-
menu = InteractiveMenu.new
-
menu.add('Read data from the file', -> { read_data_from_file })
-
menu.add_stop_action('Exit application')
-
menu.infinite_run
-
end
-
end
-
# frozen_string_literal: true
-
-
1
class Fraction
-
1
attr_reader :nominator, :denominator
-
-
1
def initialize(nominator, denominator)
-
12
raise '0 can not be a denominator' if denominator.zero?
-
12
if nominator.zero?
-
2
@nominator = 0
-
2
@denominator = denominator
-
2
return
-
end
-
10
nok_nom = nominator
-
10
nok_den = denominator
-
10
while nok_nom != nok_den
-
39
nok_den, nok_nom = [nok_nom, nok_den] if nok_nom > nok_den
-
39
nok_den -= nok_nom
-
end
-
10
@nominator = nominator / nok_nom
-
10
@denominator = denominator / nok_den
-
end
-
-
1
def sum(other)
-
1
nominator = @nominator * other.denominator + other.nominator * @denominator
-
1
denominator = @denominator * other.denominator
-
1
Fraction.new(nominator, denominator)
-
end
-
-
1
def ==(other)
-
5
return false unless other.is_a?(Fraction)
-
-
5
nominator == other.nominator &&
-
denominator == other.denominator
-
end
-
-
1
def to_s
-
"#{nominator} / #{denominator}"
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require_relative 'fraction'
-
-
1
class FractionList
-
-
1
def self.init_from_file
-
fractions = FractionList.new
-
fractions.read_from_file(File.expand_path('../data/input-1.txt', __dir__))
-
fractions
-
end
-
-
1
def initialize
-
2
@fractions = []
-
end
-
-
1
def read_from_file(file_name)
-
1
strings = File.readlines(file_name)
-
1
new_fractions = strings.map do |string|
-
2
parts = string.split('/').map(&:strip).map(&:to_i)
-
2
Fraction.new(*parts)
-
end
-
1
@fractions.concat(new_fractions)
-
end
-
-
1
def summs
-
results = []
-
@fractions.each_with_index do |first, index|
-
@fractions.slice(index + 1, @fractions.size).each do |second|
-
results.append({
-
first: first,
-
second: second,
-
summ: first.sum(second)
-
})
-
end
-
end
-
results
-
end
-
-
1
def each
-
2
return enum_for(:each) unless block_given?
-
1
@fractions.each do |fraction|
-
2
yield fraction
-
end
-
end
-
-
1
def add(fraction)
-
@fractions.append(fraction)
-
end
-
-
1
def by_index(index)
-
@fractions[index]
-
end
-
-
1
def set(index, fraction)
-
@fractions[index] = fraction
-
end
-
end
-
# frozen_string_literal: true
-
-
1
class InteractiveMenu
-
1
def initialize
-
4
@actions = []
-
end
-
-
1
def add(description, action)
-
2
@actions.append(
-
description: description,
-
action: action
-
)
-
end
-
-
1
def run
-
2
puts 'Please select the action to run'
-
2
@actions.each_with_index do |action, index|
-
2
puts "#{index + 1}: #{action[:description]}"
-
end
-
2
print 'action> '
-
2
action_number = $stdin.gets.strip.to_i - 1
-
2
raise 'There is no such action with provided number' if action_number.negative? || action_number >= @actions.size
-
-
1
@actions[action_number][:action].call
-
end
-
-
1
def add_stop_action(description)
-
add(description, -> { :stop })
-
end
-
-
1
def infinite_run
-
2
loop do
-
4
break if run == :stop
-
rescue StandardError => error
-
1
puts error.message
-
end
-
end
-
end
-
# frozen_string_literal: true
-
-
1
require_relative 'fraction_list'
-
-
1
class Menu
-
1
attr_writer :fraction_list
-
-
1
def initialize(fraction_list = FractionList.new)
-
1
@fraction_list = fraction_list
-
end
-
-
1
def read_fractions_from_file
-
1
puts('Please input the name of the file to read data from')
-
1
print('> ')
-
1
file_name = $stdin.gets.strip
-
1
@fraction_list.read_from_file(file_name)
-
end
-
end