print_primes_table 0.0.2
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 +7 -0
- data/.gitignore +12 -0
- data/.travis.yml +3 -0
- data/.yardoc/checksums +10 -0
- data/.yardoc/object_types +0 -0
- data/.yardoc/objects/root.dat +0 -0
- data/.yardoc/proxy_types +0 -0
- data/Gemfile +14 -0
- data/LICENSE.txt +22 -0
- data/README.md +78 -0
- data/Rakefile +2 -0
- data/bin/print_primes_table +10 -0
- data/doc/PrintPrimesTable.html +146 -0
- data/doc/PrintPrimesTable/Client.html +368 -0
- data/doc/PrintPrimesTable/Options.html +142 -0
- data/doc/PrintPrimesTable/Options/DefaultOption.html +247 -0
- data/doc/PrintPrimesTable/Options/HelpOption.html +311 -0
- data/doc/PrintPrimesTable/Options/OnlyPrimesOption.html +289 -0
- data/doc/PrintPrimesTable/Primes.html +496 -0
- data/doc/PrintPrimesTable/Validators.html +142 -0
- data/doc/PrintPrimesTable/Validators/DefaultValidator.html +284 -0
- data/doc/PrintPrimesTable/Validators/ErrorMessage.html +405 -0
- data/doc/PrintPrimesTable/Validators/HelpValidator.html +285 -0
- data/doc/_index.html +228 -0
- data/doc/class_list.html +58 -0
- data/doc/css/common.css +1 -0
- data/doc/css/full_list.css +57 -0
- data/doc/css/style.css +339 -0
- data/doc/file.README.html +122 -0
- data/doc/file_list.html +60 -0
- data/doc/frames.html +26 -0
- data/doc/index.html +122 -0
- data/doc/js/app.js +219 -0
- data/doc/js/full_list.js +181 -0
- data/doc/js/jquery.js +4 -0
- data/doc/method_list.html +135 -0
- data/doc/top-level-namespace.html +131 -0
- data/features/command_print_primes_table.feature +57 -0
- data/features/help_option.feature +24 -0
- data/features/support/env.rb +2 -0
- data/features/validate_arguments.feature +14 -0
- data/lib/print_primes_table.rb +4 -0
- data/lib/print_primes_table/client.rb +40 -0
- data/lib/print_primes_table/options/default_option.rb +28 -0
- data/lib/print_primes_table/options/help_option.rb +47 -0
- data/lib/print_primes_table/options/only_primes_option.rb +29 -0
- data/lib/print_primes_table/primes.rb +52 -0
- data/lib/print_primes_table/validators/default_validator.rb +39 -0
- data/lib/print_primes_table/validators/error_message.rb +31 -0
- data/lib/print_primes_table/validators/help_validator.rb +28 -0
- data/lib/print_primes_table/version.rb +10 -0
- data/print_primes_table.gemspec +29 -0
- metadata +132 -0
@@ -0,0 +1,57 @@
|
|
1
|
+
Feature: Command print_primes_table
|
2
|
+
Scenario: Printing only 10 primes
|
3
|
+
When I run `print_primes_table --only-primes`
|
4
|
+
Then it should pass with:
|
5
|
+
"""
|
6
|
+
2 3 5 7 11 13 17 19 23 29
|
7
|
+
"""
|
8
|
+
|
9
|
+
Scenario: Printing only 15 primes
|
10
|
+
When I run `print_primes_table --only-primes 15`
|
11
|
+
Then it should pass with:
|
12
|
+
"""
|
13
|
+
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
|
14
|
+
"""
|
15
|
+
|
16
|
+
Scenario: Printing table
|
17
|
+
When I run `print_primes_table`
|
18
|
+
Then it should pass with:
|
19
|
+
"""
|
20
|
+
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
|
21
|
+
| | 2 | 3 | 5 | 7 | 11 | 13 | 17 | 19 | 23 | 29 |
|
22
|
+
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
|
23
|
+
| 2 | 4 | 6 | 10 | 14 | 22 | 26 | 34 | 38 | 46 | 58 |
|
24
|
+
| 3 | 6 | 9 | 15 | 21 | 33 | 39 | 51 | 57 | 69 | 87 |
|
25
|
+
| 5 | 10 | 15 | 25 | 35 | 55 | 65 | 85 | 95 | 115 | 145 |
|
26
|
+
| 7 | 14 | 21 | 35 | 49 | 77 | 91 | 119 | 133 | 161 | 203 |
|
27
|
+
| 11 | 22 | 33 | 55 | 77 | 121 | 143 | 187 | 209 | 253 | 319 |
|
28
|
+
| 13 | 26 | 39 | 65 | 91 | 143 | 169 | 221 | 247 | 299 | 377 |
|
29
|
+
| 17 | 34 | 51 | 85 | 119 | 187 | 221 | 289 | 323 | 391 | 493 |
|
30
|
+
| 19 | 38 | 57 | 95 | 133 | 209 | 247 | 323 | 361 | 437 | 551 |
|
31
|
+
| 23 | 46 | 69 | 115 | 161 | 253 | 299 | 391 | 437 | 529 | 667 |
|
32
|
+
| 29 | 58 | 87 | 145 | 203 | 319 | 377 | 493 | 551 | 667 | 841 |
|
33
|
+
+----+----+----+-----+-----+-----+-----+-----+-----+-----+-----+
|
34
|
+
"""
|
35
|
+
|
36
|
+
Scenario: Printing table with arguments
|
37
|
+
When I run `print_primes_table 6`
|
38
|
+
Then it should pass with:
|
39
|
+
"""
|
40
|
+
+----+----+----+----+----+-----+-----+
|
41
|
+
| | 2 | 3 | 5 | 7 | 11 | 13 |
|
42
|
+
+----+----+----+----+----+-----+-----+
|
43
|
+
| 2 | 4 | 6 | 10 | 14 | 22 | 26 |
|
44
|
+
| 3 | 6 | 9 | 15 | 21 | 33 | 39 |
|
45
|
+
| 5 | 10 | 15 | 25 | 35 | 55 | 65 |
|
46
|
+
| 7 | 14 | 21 | 35 | 49 | 77 | 91 |
|
47
|
+
| 11 | 22 | 33 | 55 | 77 | 121 | 143 |
|
48
|
+
| 13 | 26 | 39 | 65 | 91 | 143 | 169 |
|
49
|
+
+----+----+----+----+----+-----+-----+
|
50
|
+
|
51
|
+
"""
|
52
|
+
Scenario: Printing table with quantity less 1
|
53
|
+
When I run `print_primes_table -6`
|
54
|
+
Then it should pass with:
|
55
|
+
"""
|
56
|
+
The minimun quantity is 1, so -6 is not valid
|
57
|
+
"""
|
@@ -0,0 +1,24 @@
|
|
1
|
+
Feature: Help Option
|
2
|
+
|
3
|
+
Scenario: Sending the option --help
|
4
|
+
When I run `print_primes_table --help`
|
5
|
+
Then it should pass with:
|
6
|
+
"""
|
7
|
+
+---------------+----------------------------------------------------------+
|
8
|
+
| Option | Description |
|
9
|
+
+---------------+----------------------------------------------------------+
|
10
|
+
| default | Print a table across the top and down the left |
|
11
|
+
| | side are the 10 primes, and the body of the table |
|
12
|
+
| without | should contain the product of multiplying these numbers. |
|
13
|
+
| option | This option accept one param the quantity. |
|
14
|
+
| | For example, if you want to print 20 type this |
|
15
|
+
| | print_table_primes 20 |
|
16
|
+
| | |
|
17
|
+
| --only-primes | Print a list of 10 primes. |
|
18
|
+
| | This option accept one param the quantity. |
|
19
|
+
| | For example, if you want to print 20 type this |
|
20
|
+
| | print_table_primes --only-primes 20 |
|
21
|
+
| | |
|
22
|
+
| --only-primes | Print this help. |
|
23
|
+
+---------------+----------------------------------------------------------+
|
24
|
+
"""
|
@@ -0,0 +1,14 @@
|
|
1
|
+
Feature: Validate Arguments
|
2
|
+
|
3
|
+
Scenario: Argument wrong type
|
4
|
+
When I run `print_primes_table Mdd`
|
5
|
+
Then it should pass with:
|
6
|
+
"""
|
7
|
+
Sorry, Mdd is not an integer
|
8
|
+
"""
|
9
|
+
Scenario: Argument number of arguments
|
10
|
+
When I run `print_primes_table 10 44`
|
11
|
+
Then it should pass with:
|
12
|
+
"""
|
13
|
+
the program allowed 1 argument maximun, you typed 2
|
14
|
+
"""
|
@@ -0,0 +1,40 @@
|
|
1
|
+
require 'readline'
|
2
|
+
|
3
|
+
module PrintPrimesTable
|
4
|
+
# @author Diego Hernán Piccinini Lagos
|
5
|
+
class Client
|
6
|
+
class << self
|
7
|
+
# capture command arguments and options
|
8
|
+
def get_command
|
9
|
+
begin
|
10
|
+
|
11
|
+
options = OPTIONS & ARGV
|
12
|
+
args = ARGV - options
|
13
|
+
process_command(args, options)
|
14
|
+
|
15
|
+
rescue => err
|
16
|
+
puts err.message
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
# when the command has options, the option is executed
|
21
|
+
# when there isn't options execute the default option
|
22
|
+
# @param args [Array] arguments captured in a console standard input
|
23
|
+
# @param options [Array] the program is prepared to get one option but in
|
24
|
+
# I think in future is possible to include more in the same instruction
|
25
|
+
def process_command(args,options)
|
26
|
+
|
27
|
+
options.each do |option|
|
28
|
+
case option
|
29
|
+
when '--only-primes'
|
30
|
+
Options::OnlyPrimesOption.process(args, options)
|
31
|
+
when '--help'
|
32
|
+
Options::HelpOption.process(args, options)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
Options::DefaultOption.process(args) if options.count < 1
|
36
|
+
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
module PrintPrimesTable
|
3
|
+
# @author Diego Hernán Piccinini Lagos
|
4
|
+
module Options
|
5
|
+
class DefaultOption
|
6
|
+
class << self
|
7
|
+
# procces the default option to print the table.
|
8
|
+
# The validator print an error if there is an exception
|
9
|
+
# @param args [Array] the first argument is a integer quantity of prime collection
|
10
|
+
def process(args)
|
11
|
+
validator = Validators::DefaultValidator.validate(args)
|
12
|
+
if validator == :valid
|
13
|
+
if args.count>0
|
14
|
+
total = Integer(args[0])
|
15
|
+
primes = Primes.new total
|
16
|
+
else
|
17
|
+
primes= Primes.new
|
18
|
+
end
|
19
|
+
primes.print_table
|
20
|
+
|
21
|
+
else
|
22
|
+
puts validator
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
module PrintPrimesTable
|
3
|
+
|
4
|
+
module Options
|
5
|
+
# @author Diego Hernán Piccinini Lagos
|
6
|
+
# to handle the help messages output
|
7
|
+
class HelpOption
|
8
|
+
MESSAGE = {:headings=> ['Option','Description'],
|
9
|
+
:rows => [
|
10
|
+
["default\n\nwithout\noption" , <<EOF
|
11
|
+
Print a table across the top and down the left
|
12
|
+
side are the 10 primes, and the body of the table
|
13
|
+
should contain the product of multiplying these numbers.
|
14
|
+
This option accept one param the quantity.
|
15
|
+
For example, if you want to print 20 type this
|
16
|
+
print_table_primes 20
|
17
|
+
EOF
|
18
|
+
],[' ',' '],
|
19
|
+
["--only-primes" , <<EOF
|
20
|
+
Print a list of 10 primes.
|
21
|
+
This option accept one param the quantity.
|
22
|
+
For example, if you want to print 20 type this
|
23
|
+
print_table_primes --only-primes 20
|
24
|
+
EOF
|
25
|
+
],[' ',' '],
|
26
|
+
["--only-primes",'Print this help.']
|
27
|
+
]
|
28
|
+
}
|
29
|
+
|
30
|
+
class << self
|
31
|
+
# process the --help option to show the options availables.
|
32
|
+
# @param args [Array] in this case is empty at first
|
33
|
+
# @param options [Array] if in future we will want to add options
|
34
|
+
def process(args,options)
|
35
|
+
validator = Validators::HelpValidator.validate(args)
|
36
|
+
if validator == :valid
|
37
|
+
|
38
|
+
table = Terminal::Table.new :headings => MESSAGE[:headings], :rows => MESSAGE[:rows]
|
39
|
+
puts table
|
40
|
+
else
|
41
|
+
puts validator
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
|
2
|
+
module PrintPrimesTable
|
3
|
+
|
4
|
+
module Options
|
5
|
+
# @author Diego Hernán Piccinini Lagos
|
6
|
+
# to print only a list of primes collection
|
7
|
+
class OnlyPrimesOption
|
8
|
+
class << self
|
9
|
+
# process the input to print the list if it pass the DefaultValidator.validate
|
10
|
+
# @param args [Array] the first argument is the quantity of primes collection
|
11
|
+
# @param options [Array] if in future we will want to add options
|
12
|
+
def process(args,options)
|
13
|
+
validator = Validators::DefaultValidator.validate(args)
|
14
|
+
if validator == :valid
|
15
|
+
if args.count>0
|
16
|
+
total = Integer(args[0])
|
17
|
+
primes = Primes.new total
|
18
|
+
else
|
19
|
+
primes= Primes.new
|
20
|
+
end
|
21
|
+
primes.list_numbers
|
22
|
+
else
|
23
|
+
puts validator
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
require 'readline'
|
2
|
+
require 'terminal-table'
|
3
|
+
|
4
|
+
module PrintPrimesTable
|
5
|
+
|
6
|
+
# @author Diego Hernán Piccinini Lagos
|
7
|
+
# To handle the primes numbers
|
8
|
+
class Primes
|
9
|
+
|
10
|
+
# @param total [Integer] the quantity of the primes collection
|
11
|
+
def initialize(total = DEFAULT_TOTAL)
|
12
|
+
@total=total
|
13
|
+
@collection = []
|
14
|
+
fill_collection
|
15
|
+
end
|
16
|
+
|
17
|
+
# fill the collection with primes number end when the collection.count is equal total
|
18
|
+
def fill_collection
|
19
|
+
eval_number = 2
|
20
|
+
begin
|
21
|
+
has_divisors = false
|
22
|
+
@collection.each do |prime|
|
23
|
+
if (eval_number % prime) == 0
|
24
|
+
has_divisors = true
|
25
|
+
break
|
26
|
+
end
|
27
|
+
end
|
28
|
+
# only the numbers has not divisors are primes
|
29
|
+
@collection.push eval_number unless has_divisors
|
30
|
+
eval_number += 1
|
31
|
+
end until @collection.count == @total
|
32
|
+
end
|
33
|
+
# print the list of primes collection separed by tab
|
34
|
+
def list_numbers
|
35
|
+
puts @collection.join("\t")
|
36
|
+
end
|
37
|
+
|
38
|
+
# print the table of TOTAL x TOTAL collection
|
39
|
+
# each coordinate contain the product
|
40
|
+
def print_table
|
41
|
+
headings =[' '] + @collection
|
42
|
+
rows=[]
|
43
|
+
@collection.each do |prime|
|
44
|
+
rows.push [prime] + @collection.map { |a| prime * a}
|
45
|
+
end
|
46
|
+
table = Terminal::Table.new :headings => headings, :rows => rows
|
47
|
+
puts table
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|
51
|
+
|
52
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
|
2
|
+
module PrintPrimesTable
|
3
|
+
# @author Diego Hernán Piccinini Lagos
|
4
|
+
module Validators
|
5
|
+
class DefaultValidator
|
6
|
+
|
7
|
+
class << self
|
8
|
+
# validate the args before a method to return a message to the user
|
9
|
+
# @param args [Array] in this case is valid 1 argument integer
|
10
|
+
# @return [Symbol | String] :valid or an Error Message
|
11
|
+
def validate(args)
|
12
|
+
begin
|
13
|
+
raise ArgumentError.new(
|
14
|
+
ErrorMessage.new(:command_wrong_arguments,
|
15
|
+
{:max => 1, :argument_num => args.count }
|
16
|
+
).show_content) if args.count > 1
|
17
|
+
if args.count > 0
|
18
|
+
|
19
|
+
begin
|
20
|
+
total = Integer(args[0])
|
21
|
+
rescue
|
22
|
+
raise TypeError.new(
|
23
|
+
ErrorMessage.new(:command_wrong_type_arguments,
|
24
|
+
{ :argument => args[0] }).show_content)
|
25
|
+
end
|
26
|
+
|
27
|
+
raise RangeError.new(
|
28
|
+
ErrorMessage.new(:less_than_min,
|
29
|
+
{ :min => 1, :argument => args[0] }).show_content) if total < 1
|
30
|
+
end
|
31
|
+
:valid
|
32
|
+
rescue => err
|
33
|
+
err.message
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module PrintPrimesTable
|
2
|
+
module Validators
|
3
|
+
# @author Diego Hernán Piccinini Lagos
|
4
|
+
# An intance contains te error_type and a message to the user
|
5
|
+
class ErrorMessage
|
6
|
+
MESSAGES = {
|
7
|
+
|
8
|
+
:command_wrong_arguments => "the program allowed %{max} argument maximun, you typed %{argument_num}",
|
9
|
+
:command_wrong_type_arguments => "Sorry, %{argument} is not an integer",
|
10
|
+
:less_than_min => "The minimun quantity is %{min}, so %{argument} is not valid"
|
11
|
+
}
|
12
|
+
# initialize the message
|
13
|
+
# @param content [String] text message to the user
|
14
|
+
# @param error_type [Symbol] to qualifies the error
|
15
|
+
# @param args [Hash] to include params in the response
|
16
|
+
def initialize(error_type = :invalid, args = {}, content = nil)
|
17
|
+
unless content
|
18
|
+
@content=MESSAGES[error_type]
|
19
|
+
else
|
20
|
+
@content = content
|
21
|
+
end
|
22
|
+
@error_type = error_type
|
23
|
+
@args = args
|
24
|
+
end
|
25
|
+
# @return text message [String] it could contain arguments
|
26
|
+
def show_content
|
27
|
+
"\n" + @content % @args
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
|
2
|
+
module PrintPrimesTable
|
3
|
+
|
4
|
+
module Validators
|
5
|
+
# @author Diego Hernán Piccinini Lagos
|
6
|
+
# to validate the --help option
|
7
|
+
class HelpValidator
|
8
|
+
|
9
|
+
class << self
|
10
|
+
# validate the arguments of --help option
|
11
|
+
# @param args [Array] the help option hasn't arguments
|
12
|
+
# @return [Symbol | String] :valid or an Error Message
|
13
|
+
def validate(args)
|
14
|
+
begin
|
15
|
+
raise ArgumentError.new(
|
16
|
+
ErrorMessage.new(:command_wrong_arguments,
|
17
|
+
{:max => 1, :argument_num => args.count }
|
18
|
+
).show_content) if args.count > 0
|
19
|
+
|
20
|
+
:valid
|
21
|
+
rescue => err
|
22
|
+
err.message
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Dir[File.join(File.dirname(__FILE__),'*.rb')].each {|file| require file }
|
2
|
+
Dir[File.join(File.dirname(__FILE__),'validators','*.rb')].each {|file| require file }
|
3
|
+
Dir[File.join(File.dirname(__FILE__),'options','*.rb')].each {|file| require file }
|
4
|
+
|
5
|
+
module PrintPrimesTable
|
6
|
+
VERSION = "0.0.2"
|
7
|
+
OPTIONS =['--only-primes','--help']
|
8
|
+
DEFAULT_TOTAL = 10
|
9
|
+
DEFAULT_START = 2
|
10
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
require 'print_primes_table/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = "print_primes_table"
|
9
|
+
spec.version = PrintPrimesTable::VERSION
|
10
|
+
spec.platform = Gem::Platform::RUBY
|
11
|
+
spec.authors = ["Diego Hernán Piccinini Lagos"]
|
12
|
+
spec.email = ["diego@guiasrails.es"]
|
13
|
+
spec.summary = %q{This program prints out a multiplication table of the first 10 prime numbers.
|
14
|
+
|
15
|
+
}
|
16
|
+
spec.description = %q{The program run from a command line and print to screen one table.
|
17
|
+
Across the top and down the left side should be the 10 primes, and the body of the table should contain the
|
18
|
+
product of multiplying these numbers.}
|
19
|
+
spec.homepage = "https://github.com/diegopiccinini/print_primes_table"
|
20
|
+
spec.license = "MIT"
|
21
|
+
spec.files = `git ls-files -z`.split("\x0")
|
22
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
spec.required_ruby_version = ">= 2.0.0"
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.has_rdoc = 'yard'
|
29
|
+
end
|