haversack 0.1.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: 6cd0e17b216a5749f67cfa35838d739f4d45971dbb87b6a145cba0a41f4e9b2e
4
+ data.tar.gz: 727df279fe818360a8533f698700d218d3801f4939052d719fe252aa2a7dd9ba
5
+ SHA512:
6
+ metadata.gz: de5429d256326f523cf432ff334944318900104477b0dba9b93bcd5079e16e1bc615e8ab10d29d6cd498c85c05482c60597948a8abe2cb3cbca3d092f3849b7e
7
+ data.tar.gz: 443d15a96241f0b4bb73e70653f46b1a45f3207d924bfff57e2a6014603598dc380c7acfce4dc66557ba73c5b450622ea085e6aacf55bd550120729800917c3a
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,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.0
7
+ before_install: gem install bundler -v 1.16.6
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in haversack.gemspec
6
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Alex Larsen
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 all
13
+ 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 THE
21
+ SOFTWARE.
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 alex0112
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,39 @@
1
+ # Haversack
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/haversack`. 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 'haversack'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install haversack
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]/haversack.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "haversack"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ 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
data/haversack.gemspec ADDED
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "haversack/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "haversack"
7
+ spec.version = Haversack::VERSION
8
+ spec.authors = ["alex0112"]
9
+ spec.email = ["alarsen0112@gmail.com"]
10
+
11
+ spec.summary = %q{Knapsack Abstraction}
12
+ spec.description = %q{Haversack is an enumberable abstraction of a Knapsack}
13
+ spec.homepage = "https://github.com/alex0112/haversack"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ if spec.respond_to?(:metadata)
19
+ spec.metadata["allowed_push_host"] = 'https://rubygems.org'
20
+
21
+ spec.metadata["homepage_uri"] = spec.homepage
22
+ spec.metadata["source_code_uri"] = "https://github.com/alex0112/haversack"
23
+ else
24
+ raise "RubyGems 2.0 or newer is required to protect against " \
25
+ "public gem pushes."
26
+ end
27
+
28
+ # Specify which files should be added to the gem when it is released.
29
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
30
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
31
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
32
+ end
33
+ spec.bindir = "exe"
34
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
35
+ spec.require_paths = ["lib/haversack.rb"]
36
+
37
+ spec.add_development_dependency "bundler", "~> 1.16"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "factory_bot", "~> 4.0"
41
+
42
+ end
@@ -0,0 +1,5 @@
1
+ module Haversack
2
+ class KnapsackCapacityExceededError < StandardError; end
3
+ class KnapsackWeightExceededError < StandardError; end
4
+ class KnapsackContentError < StandardError; end
5
+ end
@@ -0,0 +1,13 @@
1
+ module Haversack
2
+ class Item
3
+ attr_accessor :weight
4
+ attr_accessor :size
5
+ attr_accessor :data
6
+
7
+ def initialize(weight:, size: 1, data: nil)
8
+ @weight = weight
9
+ @size = size
10
+ @data = data
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,79 @@
1
+ class Sack
2
+ include Enumerable
3
+
4
+ attr_reader :capacity, :weight, :available_capacity, :current_weight, :contents
5
+
6
+ def initialize(capacity:, weight:, contents: nil)
7
+ @capacity = capacity
8
+ @weight = weight
9
+ self.contents = contents || [] ## Use the setter to enforce weight/capacity restrictions
10
+ @current_weight = self.current_weight
11
+ @available_capacity = self.available_capacity
12
+ end
13
+
14
+ def method_missing(method_id)
15
+ Enumerable.respond_to? method_id ? @contents.send(method_id) : raise(NoMethodError)
16
+ end
17
+
18
+ def contents=(new_contents)
19
+ raise Haversack::KnapsackContentError if has_non_items? new_contents
20
+ raise Haversack::KnapsackCapacityExceededError if exceeds_capacity? new_contents
21
+ raise Haversack::KnapsackWeightExceededError if exceeds_weight? new_contents
22
+
23
+ @contents = new_contents
24
+ end
25
+
26
+ def has_non_items?(contents) ## TODO: Consider refactor: Static method? Doesn't rely on instance data.
27
+ contents.any? { |e| !e.is_a? Haversack::Item }
28
+ end
29
+
30
+ def current_weight
31
+ @contents.empty? ? 0 : @contents.map { |item| item.weight }.sum
32
+ end
33
+
34
+ ## TODO: the *fit* methods duplicate behavior. Consolidate them in a future itteration.
35
+
36
+ def fits_item_weight?(item)
37
+ item.weight + @current_weight <= @weight
38
+ end
39
+
40
+ def available_capacity
41
+ @contents.empty? ? @capacity : (@capacity - @contents.length)
42
+ end
43
+
44
+ def fits_item_capacity?(item)
45
+ item.size < @available_capacity
46
+ end
47
+
48
+ def will_fit?(item)
49
+ (item.is_a? Haversack::Item) && (fits_item_capacity?(item) && fits_item_weight?(item))
50
+ end
51
+
52
+ def push(item) ## TODO: This should return an error that describes which constraint failed
53
+ will_fit? item ? @contents.push(item) : raise(Haversack::KnapsackContentError)
54
+ @contents
55
+ end
56
+
57
+ def fits_contents?(contents)
58
+ fits_weight?(contents) && fits_capacity?(contents)
59
+ end
60
+
61
+ def fits_weight?(contents)
62
+ new_weight = contents.map { |item| item.weight }.sum
63
+ new_weight <= @weight
64
+ end
65
+
66
+ def exceeds_weight?(contents)
67
+ !fits_weight? contents
68
+ end
69
+
70
+ def fits_capacity?(contents)
71
+ new_capacity = contents.map { |item| item.size }.sum
72
+ new_capacity <= @capacity
73
+ end
74
+
75
+ def exceeds_capacity?(contents)
76
+ !fits_capacity? contents
77
+ end
78
+
79
+ end
@@ -0,0 +1,3 @@
1
+ module Haversack
2
+ VERSION = "0.1.0"
3
+ end
data/lib/haversack.rb ADDED
@@ -0,0 +1,6 @@
1
+ module Haversack
2
+ require "haversack/version"
3
+ require "haversack/sack"
4
+ require "haversack/item"
5
+ require "haversack/err"
6
+ end
metadata ADDED
@@ -0,0 +1,119 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: haversack
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - alex0112
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-12-22 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: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.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: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: factory_bot
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '4.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '4.0'
69
+ description: Haversack is an enumberable abstraction of a Knapsack
70
+ email:
71
+ - alarsen0112@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - bin/console
85
+ - bin/setup
86
+ - haversack.gemspec
87
+ - lib/haversack.rb
88
+ - lib/haversack/err.rb
89
+ - lib/haversack/item.rb
90
+ - lib/haversack/sack.rb
91
+ - lib/haversack/version.rb
92
+ homepage: https://github.com/alex0112/haversack
93
+ licenses:
94
+ - MIT
95
+ metadata:
96
+ allowed_push_host: https://rubygems.org
97
+ homepage_uri: https://github.com/alex0112/haversack
98
+ source_code_uri: https://github.com/alex0112/haversack
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib/haversack.rb
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubyforge_project:
115
+ rubygems_version: 2.7.4
116
+ signing_key:
117
+ specification_version: 4
118
+ summary: Knapsack Abstraction
119
+ test_files: []