codebreaker_sn 0.1.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 8b4bc64fa02fe1525021ca41c2f25989ad79ef13c7f52df4e54f272b6526c2d3
4
+ data.tar.gz: ac89230e4c36e093aa783dace5d1f9c7e12d380107a55f7d450f531eddbe2843
5
+ SHA512:
6
+ metadata.gz: 3002e08c2f92d7bbe4813389ebde8fc31a123c15c919b823d33ee719320e9f1455c8f40e8350c9ec7e65567aa999998b9ce33a25a2f7f349274ca62b265a6af6
7
+ data.tar.gz: 034aa1c0516dc53a557f5f672028bb13a57a706f349dc5f19473bf7d746761629195ff36fbdeddd90694afd02fd1ac7ff0c72e05a4c9030a674d2e42d3875f4d
@@ -0,0 +1,66 @@
1
+ version: 2.1
2
+
3
+ executors:
4
+ default:
5
+ working_directory: ~/repo
6
+ description: The official CircleCI Ruby Docker image
7
+ docker:
8
+ - image: circleci/ruby:2.7.0
9
+
10
+ caches:
11
+ - &bundle_cache_full v2-repo-{{ checksum "Gemfile.lock" }}
12
+ - &bundle_cache v2-repo-
13
+
14
+ commands:
15
+ defaults:
16
+ steps:
17
+ - checkout
18
+ - restore_cache:
19
+ keys:
20
+ - *bundle_cache_full
21
+ - *bundle_cache
22
+ - run: bundle config set path 'vendor/bundle'
23
+ - save_cache:
24
+ key: *bundle_cache_full
25
+ paths:
26
+ - vendor/bundle
27
+ run_linters:
28
+ description: command to start linters
29
+ steps:
30
+ - run:
31
+ name: rubocop
32
+ command: bundle exec rubocop
33
+ - run:
34
+ name: fasterer
35
+ command: bundle exec fasterer
36
+ run_specs:
37
+ steps:
38
+ - run:
39
+ name: run specs
40
+ command: |
41
+ mkdir /tmp/test-results
42
+ TEST_FILES="$(circleci tests glob "spec/**/*_spec.rb" | circleci tests split --split-by=timings)"
43
+ bundle exec rspec --format progress \
44
+ --out /tmp/test-results/rspec.xml \
45
+ $TEST_FILES
46
+
47
+ jobs:
48
+ lintering:
49
+ executor: default
50
+ steps:
51
+ - defaults
52
+ - run_linters
53
+ run_specs:
54
+ executor: default
55
+ steps:
56
+ - defaults
57
+ - run_specs
58
+
59
+ workflows:
60
+ version: 2
61
+ build:
62
+ jobs:
63
+ - lintering
64
+ - run_specs:
65
+ requires:
66
+ - lintering
@@ -0,0 +1,4 @@
1
+ speedups:
2
+ each_with_index_vs_while: false
3
+ exclude_paths:
4
+ - "vendor/**/*.rb"
@@ -0,0 +1,13 @@
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
+ .ruby-gemset
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ require: rubocop-rspec
2
+
3
+ Style/Documentation:
4
+ Enabled: false
5
+ Metrics/MethodLength:
6
+ Max: 12
@@ -0,0 +1 @@
1
+ 2.7.0
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.0
6
+ before_install: gem install bundler -v 2.1.4
@@ -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 nakul.sv@gmail.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/
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 codebreaker.gemspec
6
+ gemspec
@@ -0,0 +1,72 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ codebreaker (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ colorize (0.8.1)
11
+ diff-lcs (1.4.4)
12
+ docile (1.3.2)
13
+ fasterer (0.8.3)
14
+ colorize (~> 0.7)
15
+ ruby_parser (>= 3.14.1)
16
+ parallel (1.19.2)
17
+ parser (2.7.1.4)
18
+ ast (~> 2.4.1)
19
+ rainbow (3.0.0)
20
+ rake (12.3.3)
21
+ regexp_parser (1.7.1)
22
+ rexml (3.2.4)
23
+ rspec (3.9.0)
24
+ rspec-core (~> 3.9.0)
25
+ rspec-expectations (~> 3.9.0)
26
+ rspec-mocks (~> 3.9.0)
27
+ rspec-core (3.9.2)
28
+ rspec-support (~> 3.9.3)
29
+ rspec-expectations (3.9.2)
30
+ diff-lcs (>= 1.2.0, < 2.0)
31
+ rspec-support (~> 3.9.0)
32
+ rspec-mocks (3.9.1)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-support (3.9.3)
36
+ rubocop (0.86.0)
37
+ parallel (~> 1.10)
38
+ parser (>= 2.7.0.1)
39
+ rainbow (>= 2.2.2, < 4.0)
40
+ regexp_parser (>= 1.7)
41
+ rexml
42
+ rubocop-ast (>= 0.0.3, < 1.0)
43
+ ruby-progressbar (~> 1.7)
44
+ unicode-display_width (>= 1.4.0, < 2.0)
45
+ rubocop-ast (0.1.0)
46
+ parser (>= 2.7.0.1)
47
+ rubocop-rspec (1.41.0)
48
+ rubocop (>= 0.68.1)
49
+ ruby-progressbar (1.10.1)
50
+ ruby_parser (3.14.2)
51
+ sexp_processor (~> 4.9)
52
+ sexp_processor (4.15.0)
53
+ simplecov (0.18.5)
54
+ docile (~> 1.1)
55
+ simplecov-html (~> 0.11)
56
+ simplecov-html (0.12.2)
57
+ unicode-display_width (1.7.0)
58
+
59
+ PLATFORMS
60
+ ruby
61
+
62
+ DEPENDENCIES
63
+ codebreaker!
64
+ fasterer
65
+ rake
66
+ rspec
67
+ rubocop
68
+ rubocop-rspec
69
+ simplecov
70
+
71
+ BUNDLED WITH
72
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Sergio
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.
@@ -0,0 +1,44 @@
1
+ # Codebreaker
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/codebreaker`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'codebreaker'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle install
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install codebreaker
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ 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.
30
+
31
+ 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).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/codebreaker. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
41
+
42
+ ## Code of Conduct
43
+
44
+ Everyone interacting in the Codebreaker project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/codebreaker/blob/master/CODE_OF_CONDUCT.md).
@@ -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
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'codebreaker'
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__)
@@ -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,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/codebreaker/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'codebreaker_sn'
7
+ spec.version = Codebreaker::VERSION
8
+ spec.authors = ['Sergio']
9
+ spec.email = ['nakul.sv@gmail.com']
10
+ spec.summary = 'Codebreaker game'
11
+ spec.homepage = 'https://github.com/sergeynakul/codebreaker'
12
+ spec.license = 'MIT'
13
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
14
+
15
+ spec.metadata['homepage_uri'] = 'https://github.com/sergeynakul/codebreaker'
16
+ spec.metadata['source_code_uri'] = 'https://github.com/sergeynakul/codebreaker'
17
+
18
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+ spec.bindir = 'exe'
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ['lib']
24
+ spec.add_development_dependency 'fasterer'
25
+ spec.add_development_dependency 'rake'
26
+ spec.add_development_dependency 'rspec'
27
+ spec.add_development_dependency 'rubocop'
28
+ spec.add_development_dependency 'rubocop-rspec'
29
+ spec.add_development_dependency 'simplecov'
30
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ RANGE_OF_NUMBERS = (1..6).freeze
4
+ AMOUNT_OF_NUMBERS = 4
5
+ DIFF_ARRAY = %w[hell medium easy].freeze
6
+ RESULTS_FILE = 'lib/files/results_file.csv'
7
+ RULES_FILE = 'lib/files/rules.txt'
8
+
9
+ module Codebreaker
10
+ require 'csv'
11
+ require 'codebreaker/version'
12
+ require 'moduls/validations'
13
+ require 'codebreaker/game'
14
+ require 'codebreaker/user'
15
+ require 'codebreaker/codemaker'
16
+ require 'codebreaker/guess'
17
+ require 'errors/validation_error'
18
+ require 'errors/length_error'
19
+ require 'errors/presence_error'
20
+ require 'errors/out_of_range_error'
21
+ require 'errors/class_type_error'
22
+ require 'errors/inclusion_error'
23
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Codemaker
5
+ NON_OCCURRING_VALUE_FIRST = 'X'
6
+ NON_OCCURRING_VALUE_SECOND = 'Y'
7
+ NON_OCCURRING_VALUE_THIRD = 'Z'
8
+
9
+ attr_reader :secret_code, :response
10
+
11
+ def initialize
12
+ @secret_code = ''
13
+ @response = { in_plase: 0, out_of_place: 0 }
14
+ end
15
+
16
+ def generate_secret_code(amout = AMOUNT_OF_NUMBERS, range = RANGE_OF_NUMBERS)
17
+ array_of_numbers = []
18
+ amout.times { array_of_numbers << rand(range) }
19
+ @secret_code = array_of_numbers.join.to_i
20
+ end
21
+
22
+ def check(guess_array, secret_code_array)
23
+ guess_array.each_with_index do |number, index|
24
+ next unless number == secret_code_array[index]
25
+
26
+ @response[:in_plase] += 1
27
+ guess_array[index] = NON_OCCURRING_VALUE_FIRST
28
+ secret_code_array[index] = NON_OCCURRING_VALUE_SECOND
29
+ end
30
+ guess_array.each do |number|
31
+ if secret_code_array.include?(number)
32
+ @response[:out_of_place] += 1
33
+ secret_code_array[secret_code_array.index(number)] = NON_OCCURRING_VALUE_THIRD
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Game
5
+ include Validations
6
+
7
+ DIFFICULTY_HASH = { easy: { attempts: 15, hints: 2 },
8
+ medium: { attempts: 10, hints: 1 },
9
+ hell: { attempts: 5, hints: 1 } }.freeze
10
+
11
+ validate :difficulty, :inclusion, %i[easy medium hell]
12
+
13
+ attr_reader :user, :difficulty, :secret_code
14
+
15
+ def initialize(user, difficulty, secret_code)
16
+ @user = user
17
+ @difficulty = difficulty
18
+ @secret_code = secret_code
19
+ @attempts_used = 0
20
+ @hints_used = []
21
+ validate!
22
+ end
23
+
24
+ def check(guess)
25
+ @attempts_used += 1
26
+
27
+ guess_array = guess.to_s.split('').map(&:to_i)
28
+ secret_code_array = @secret_code.to_s.split('').map(&:to_i)
29
+
30
+ codemaker = Codebreaker::Codemaker.new
31
+ codemaker.check(guess_array, secret_code_array)
32
+ codemaker.response
33
+ end
34
+
35
+ def any_hints_left?
36
+ DIFFICULTY_HASH[@difficulty][:hints] > @hints_used.count
37
+ end
38
+
39
+ def take_hint
40
+ secret_code_array = @secret_code.to_s.split('').map(&:to_i)
41
+ @hints_used.each do |hint_used|
42
+ secret_code_array.delete_at(secret_code_array.index(hint_used))
43
+ end
44
+ random_element = secret_code_array.sample
45
+ @hints_used << random_element
46
+ random_element
47
+ end
48
+
49
+ def win?(guess)
50
+ guess == @secret_code
51
+ end
52
+
53
+ def lose?
54
+ @attempts_used >= DIFFICULTY_HASH[@difficulty][:attempts]
55
+ end
56
+
57
+ def save_result
58
+ data = "#{@user.name},#{@difficulty},"
59
+ data += "#{DIFFICULTY_HASH[@difficulty][:attempts]},#{@attempts_used},"
60
+ data += "#{DIFFICULTY_HASH[@difficulty][:hints]},#{@hints_used.count}"
61
+ File.open(RESULTS_FILE, 'a') do |file|
62
+ file.puts data
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class Guess
5
+ include Validations
6
+
7
+ attr_reader :numbers
8
+
9
+ validate :numbers, :presence
10
+ validate :numbers, :type, Integer
11
+ validate :numbers, :length, AMOUNT_OF_NUMBERS
12
+ validate :numbers, :range, RANGE_OF_NUMBERS
13
+
14
+ def initialize(numbers)
15
+ @numbers = numbers
16
+ validate!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ class User
5
+ NAME_LENGTH = (3..20).freeze
6
+ include Validations
7
+
8
+ attr_reader :name
9
+
10
+ validate :name, :presence
11
+ validate :name, :type, String
12
+ validate :name, :length, NAME_LENGTH
13
+
14
+ def initialize(name)
15
+ @name = name
16
+ validate!
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Codebreaker
4
+ VERSION = '0.1.0'
5
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ClassTypeError < ValidationError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class InclusionError < ValidationError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class LengthError < ValidationError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class OutOfRangeError < ValidationError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PresenceError < ValidationError
4
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ValidationError < StandardError
4
+ end
File without changes
@@ -0,0 +1,14 @@
1
+ Game Rules
2
+ Codebreaker is a logic game in which a code-breaker tries to break a secret code created by a code-maker. The codemaker, which will be played by the application we’re going to write, creates a secret code of four numbers between 1 and 6.
3
+ The codebreaker gets some number of chances to break the code (depends on chosen difficulty). In each turn, the codebreaker makes a guess of 4 numbers. The codemaker then marks the guess with up to 4 signs - + or - or empty spaces.
4
+ A + indicates an exact match: one of the numbers in the guess is the same as one of the numbers in the secret code and in the same position. For example:
5
+ Secret number - 1234
6
+ Input number - 6264
7
+ Number of pluses - 2 (second and fourth position)
8
+ A - indicates a number match: one of the numbers in the guess is the same as one of the numbers in the secret code but in a different position. For example:
9
+ Secret number - 1234
10
+ Input number - 6462
11
+ Number of minuses - 2 (second and fourth position)
12
+ An empty space indicates that there is not a current digit in a secret number.
13
+ If codebreaker inputs the exact number as a secret number - codebreaker wins the game. If all attempts are spent - codebreaker loses.
14
+ Codebreaker also has some number of hints(depends on chosen difficulty). If a user takes a hint - he receives back a separate digit of the secret code.
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Validations
4
+ def self.included(base)
5
+ base.extend ClassMethods
6
+ base.include InstanceMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ attr_reader :validates
11
+
12
+ def validate(variable, type, *conditional_value)
13
+ @validates ||= []
14
+ @validates << { variable: variable, type: type, conditional_value: conditional_value }
15
+ end
16
+ end
17
+
18
+ module InstanceMethods
19
+ def validate!
20
+ @validates = self.class.instance_variable_get(:@validates)
21
+ @validates.each do |validate|
22
+ value = instance_variable_get("@#{validate[:variable]}")
23
+ name = validate[:variable].capitalize
24
+ send("validate_#{validate[:type]}", value, name, *validate[:conditional_value])
25
+ end
26
+ end
27
+
28
+ def validate_presence(value, name)
29
+ raise PresenceError, "#{name} can't be blank" if value.to_s.empty?
30
+ end
31
+
32
+ def validate_range(value, name, range)
33
+ return unless value.to_s.split('').map(&:to_i).any? { |number| !range.include?(number) }
34
+
35
+ raise OutOfRangeError, "#{name} must be in #{range}"
36
+ end
37
+
38
+ def validate_type(value, name, type_class)
39
+ raise ClassTypeError, "#{name} must be an instance of #{type_class} class" unless value.instance_of? type_class
40
+ end
41
+
42
+ def validate_length(value, name, length)
43
+ raise LengthError, "#{name} must be #{length} characters" unless valid_length?(value.to_s.split('').count, length)
44
+ end
45
+
46
+ def validate_inclusion(value, name, array)
47
+ raise InclusionError, "#{name} must be in #{array.join(', ')}" unless array.include?(value)
48
+ end
49
+
50
+ def valid_length?(count, length)
51
+ if length.instance_of?(Integer)
52
+ count == length
53
+ elsif length.instance_of?(Range)
54
+ length.member?(count)
55
+ end
56
+ end
57
+ end
58
+ end
metadata ADDED
@@ -0,0 +1,160 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: codebreaker_sn
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Sergio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-07-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: fasterer
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: rake
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: rspec
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: rubocop
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: rubocop-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: simplecov
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
+ description:
98
+ email:
99
+ - nakul.sv@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".circleci/config.yml"
105
+ - ".fasterer.yml"
106
+ - ".gitignore"
107
+ - ".rspec"
108
+ - ".rubocop.yml"
109
+ - ".ruby-version"
110
+ - ".travis.yml"
111
+ - CODE_OF_CONDUCT.md
112
+ - Gemfile
113
+ - Gemfile.lock
114
+ - LICENSE.txt
115
+ - README.md
116
+ - Rakefile
117
+ - bin/console
118
+ - bin/setup
119
+ - codebreaker.gemspec
120
+ - lib/codebreaker.rb
121
+ - lib/codebreaker/codemaker.rb
122
+ - lib/codebreaker/game.rb
123
+ - lib/codebreaker/guess.rb
124
+ - lib/codebreaker/user.rb
125
+ - lib/codebreaker/version.rb
126
+ - lib/errors/class_type_error.rb
127
+ - lib/errors/inclusion_error.rb
128
+ - lib/errors/length_error.rb
129
+ - lib/errors/out_of_range_error.rb
130
+ - lib/errors/presence_error.rb
131
+ - lib/errors/validation_error.rb
132
+ - lib/files/results_file.csv
133
+ - lib/files/rules.txt
134
+ - lib/moduls/validations.rb
135
+ homepage: https://github.com/sergeynakul/codebreaker
136
+ licenses:
137
+ - MIT
138
+ metadata:
139
+ homepage_uri: https://github.com/sergeynakul/codebreaker
140
+ source_code_uri: https://github.com/sergeynakul/codebreaker
141
+ post_install_message:
142
+ rdoc_options: []
143
+ require_paths:
144
+ - lib
145
+ required_ruby_version: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ">="
148
+ - !ruby/object:Gem::Version
149
+ version: 2.3.0
150
+ required_rubygems_version: !ruby/object:Gem::Requirement
151
+ requirements:
152
+ - - ">="
153
+ - !ruby/object:Gem::Version
154
+ version: '0'
155
+ requirements: []
156
+ rubygems_version: 3.1.2
157
+ signing_key:
158
+ specification_version: 4
159
+ summary: Codebreaker game
160
+ test_files: []