warehouse_items 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +7 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +36 -0
- data/LICENSE.txt +21 -0
- data/README.md +39 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/warehouse.rb +11 -0
- data/lib/warehouse/item.rb +21 -0
- data/lib/warehouse/list.rb +83 -0
- data/lib/warehouse/random.rb +36 -0
- data/lib/warehouse/version.rb +3 -0
- data/warehouse_items.gemspec +32 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 25d4f55b08eaebc9d72536240bd555fc79fad450
|
4
|
+
data.tar.gz: 56e00e0ff8b2167707af223935d7dde356748f7b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 186058952fdea0c1fa8873fa5f0f46b8800042cbc1428f1d2f50036b19aca3f71c17c355004f06f6274bebe46218cb8c0304042e3283e349260103473ff7e16d
|
7
|
+
data.tar.gz: fc19a7571d00d2827a2086940cd04fd35532468c97a05a6129f416439d6aea948831668878b77d3c7a005cbde6b80a9b16a4e6aa46728fbbb675efc82ad9ce95
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
warehouse_items (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (5.2.1)
|
11
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
12
|
+
i18n (>= 0.7, < 2)
|
13
|
+
minitest (~> 5.1)
|
14
|
+
tzinfo (~> 1.1)
|
15
|
+
concurrent-ruby (1.1.3)
|
16
|
+
i18n (1.1.1)
|
17
|
+
concurrent-ruby (~> 1.0)
|
18
|
+
minitest (5.11.3)
|
19
|
+
rake (10.5.0)
|
20
|
+
shoulda-context (1.2.2)
|
21
|
+
thread_safe (0.3.6)
|
22
|
+
tzinfo (1.2.5)
|
23
|
+
thread_safe (~> 0.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
minitest (~> 5.0)
|
31
|
+
rake (~> 10.0)
|
32
|
+
shoulda-context
|
33
|
+
warehouse_items!
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 eddie
|
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
|
+
# WarehouseItems
|
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/warehouse_items`. 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 'warehouse_items'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install warehouse_items
|
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 test` 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]/warehouse_items.
|
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
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "warehouse_items"
|
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
data/lib/warehouse.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
class Warehouse::Item < Struct.new(:code, :title, :qty, :price)
|
2
|
+
attr_accessor :items
|
3
|
+
|
4
|
+
def type
|
5
|
+
'single'
|
6
|
+
end
|
7
|
+
|
8
|
+
def random?
|
9
|
+
false
|
10
|
+
end
|
11
|
+
|
12
|
+
def single?
|
13
|
+
true
|
14
|
+
end
|
15
|
+
|
16
|
+
def persisted?; end
|
17
|
+
|
18
|
+
def deep_dup
|
19
|
+
Warehouse::Item.new(code, title, qty, price)
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class Warehouse::List
|
2
|
+
attr_accessor :items
|
3
|
+
|
4
|
+
delegate :empty?, to: :items
|
5
|
+
delegate :length, to: :items
|
6
|
+
delegate :present?, to: :items
|
7
|
+
delegate :each, to: :items
|
8
|
+
delegate :each_with_index, to: :items
|
9
|
+
delegate :map, to: :items
|
10
|
+
delegate :[], to: :items
|
11
|
+
delegate :to_json, to: :items
|
12
|
+
|
13
|
+
def initialize(items=nil)
|
14
|
+
self.items = (items || [])
|
15
|
+
end
|
16
|
+
|
17
|
+
class << self
|
18
|
+
def none
|
19
|
+
new
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# 找到此清單底下料號 (同樣 object_id)
|
24
|
+
def find(code)
|
25
|
+
items.detect { |item| item.code == code }
|
26
|
+
end
|
27
|
+
|
28
|
+
# 解開隨機料號 (New List)
|
29
|
+
def items!
|
30
|
+
list = Warehouse::List.new
|
31
|
+
items.each do |item|
|
32
|
+
extra_items = item.is_a?(Warehouse::Random) ? item.fetch! : [ item ]
|
33
|
+
|
34
|
+
extra_items.each do |item|
|
35
|
+
list.items << item
|
36
|
+
end
|
37
|
+
end
|
38
|
+
list
|
39
|
+
end
|
40
|
+
|
41
|
+
# 把料號加入到清單內 (Clone List)
|
42
|
+
def +(item)
|
43
|
+
case item
|
44
|
+
when Warehouse::Item
|
45
|
+
add_item(item)
|
46
|
+
when Warehouse::Random
|
47
|
+
# 隨機抽一個
|
48
|
+
add_item(item.fetch!)
|
49
|
+
when Warehouse::List
|
50
|
+
list = deep_dup
|
51
|
+
item.each do |target|
|
52
|
+
list += target
|
53
|
+
end
|
54
|
+
list
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
# 把料號 * 數量 (Clone List)
|
59
|
+
def *(qty)
|
60
|
+
Warehouse::List.new.tap do |list|
|
61
|
+
items!.each do |item|
|
62
|
+
list.items << item.deep_dup.tap { |i| i.qty = i.qty * qty }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def deep_dup
|
68
|
+
Warehouse::List.new(items.map { |item| item.deep_dup })
|
69
|
+
end
|
70
|
+
|
71
|
+
protected
|
72
|
+
|
73
|
+
def add_item(item)
|
74
|
+
list = deep_dup
|
75
|
+
target = list.find(item.code)
|
76
|
+
if target.present?
|
77
|
+
target.qty = (target.qty + item.qty)
|
78
|
+
else
|
79
|
+
list.items << item.deep_dup
|
80
|
+
end
|
81
|
+
list
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
class Warehouse::Random < Struct.new(:items, :qty)
|
2
|
+
|
3
|
+
attr_accessor :code, :title, :price
|
4
|
+
|
5
|
+
def type
|
6
|
+
'random'
|
7
|
+
end
|
8
|
+
|
9
|
+
def random?
|
10
|
+
true
|
11
|
+
end
|
12
|
+
|
13
|
+
def single?
|
14
|
+
false
|
15
|
+
end
|
16
|
+
|
17
|
+
def fetch
|
18
|
+
@fetch ||= fetch!
|
19
|
+
end
|
20
|
+
|
21
|
+
def fetch!
|
22
|
+
random_items = {}
|
23
|
+
(1..qty).each do
|
24
|
+
item = items.sample
|
25
|
+
random_items[item.code] = Warehouse::Item.new(item.code, item.title, 0, item.price)
|
26
|
+
random_items[item.code].qty += 1
|
27
|
+
end
|
28
|
+
Warehouse::List.new(random_items.values)
|
29
|
+
end
|
30
|
+
|
31
|
+
def persisted?; end
|
32
|
+
|
33
|
+
def deep_dup
|
34
|
+
Warehouse::Random.new(items.map { |item| item.deep_dup }, qty)
|
35
|
+
end
|
36
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "warehouse/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "warehouse_items"
|
8
|
+
spec.version = Warehouse::VERSION
|
9
|
+
spec.authors = ["Eddie Li"]
|
10
|
+
spec.email = ["eddie@super-landing.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{warehouse_items wrapper & value objects}
|
13
|
+
spec.description = %q{warehouse_items wrapper & value objects}
|
14
|
+
spec.homepage = "https://github.com/superlanding/warehouse_items"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = "exe"
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ["lib"]
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
27
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
28
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
29
|
+
spec.add_development_dependency "shoulda-context"
|
30
|
+
|
31
|
+
spec.add_dependency "activesupport"
|
32
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: warehouse_items
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Eddie Li
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-07 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: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: shoulda-context
|
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: activesupport
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: warehouse_items wrapper & value objects
|
84
|
+
email:
|
85
|
+
- eddie@super-landing.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".travis.yml"
|
92
|
+
- Gemfile
|
93
|
+
- Gemfile.lock
|
94
|
+
- LICENSE.txt
|
95
|
+
- README.md
|
96
|
+
- Rakefile
|
97
|
+
- bin/console
|
98
|
+
- bin/setup
|
99
|
+
- lib/warehouse.rb
|
100
|
+
- lib/warehouse/item.rb
|
101
|
+
- lib/warehouse/list.rb
|
102
|
+
- lib/warehouse/random.rb
|
103
|
+
- lib/warehouse/version.rb
|
104
|
+
- warehouse_items.gemspec
|
105
|
+
homepage: https://github.com/superlanding/warehouse_items
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.4.8
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: warehouse_items wrapper & value objects
|
129
|
+
test_files: []
|