warehouse_items 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98e337833a9d62456fe917317e31ba5b7920a8d8
4
- data.tar.gz: fd67a7f10709c72beb1b0724dc8da69333044504
3
+ metadata.gz: f7ae3eb84eaf2129459844170902c41896f8bd6e
4
+ data.tar.gz: b83f135e3ef919864e78b30b99c1e326a396e396
5
5
  SHA512:
6
- metadata.gz: 2df7dbdf07b08217e35ed6db18bdee06fba51111777a371234592ff7246e5ffdaee5bd81dfcaf2ff004eb7dd2806cdf640cbe40aa1fa37e39129fb93840ea56e
7
- data.tar.gz: 8f8954b931cd2003ba30837e81a3dc04fb74f1cceab52d663d0d9ed4fb8e15d281c4cea311511d174d93ac37d721e90bac83e2f41bb6ece13ea27abcdc3bbbd1
6
+ metadata.gz: bf6e08cce5ba858901229893f7b6e091bfbf1af37b289367bc6e25c6262dfe55af9bfcc6ffeef6d2ad1d9cc33e08f16d1ab66d2ca70dfdc25b54e6a9d6ade52a
7
+ data.tar.gz: 8cf0a6c1296f0d4a34982842dcf445ae5560b645407eeecaf3cd42568393b0bafbaa673f2f2a69b4de9d17e30501ddf8307dc2d5add040ef1094ec50ceec9604
data/Gemfile.lock CHANGED
@@ -1,19 +1,19 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- warehouse_items (0.1.0)
4
+ warehouse_items (0.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
- activesupport (5.2.1)
10
+ activesupport (5.2.3)
11
11
  concurrent-ruby (~> 1.0, >= 1.0.2)
12
12
  i18n (>= 0.7, < 2)
13
13
  minitest (~> 5.1)
14
14
  tzinfo (~> 1.1)
15
- concurrent-ruby (1.1.3)
16
- i18n (1.1.1)
15
+ concurrent-ruby (1.1.5)
16
+ i18n (1.5.1)
17
17
  concurrent-ruby (~> 1.0)
18
18
  minitest (5.11.3)
19
19
  rake (10.5.0)
data/README.md CHANGED
@@ -1,39 +1,36 @@
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:
1
+ # Warehouse::Item
10
2
 
11
3
  ```ruby
12
- gem 'warehouse_items'
4
+ @item1 = Warehouse::Item.new('迷黑', '迷黑', 3, 250)
13
5
  ```
14
6
 
15
- And then execute:
16
-
17
- $ bundle
18
-
19
- Or install it yourself as:
20
-
21
- $ gem install warehouse_items
7
+ # Warehouse::Random
22
8
 
23
- ## Usage
24
-
25
- TODO: Write usage instructions here
26
-
27
- ## Development
9
+ ```ruby
10
+ @item1 = Warehouse::Item.new('迷黑', '迷黑', nil, 250)
11
+ @item2 = Warehouse::Item.new('迷黃', '迷黃', nil, 250)
12
+ @item3 = Warehouse::Item.new('迷綠', '迷綠', nil, 250)
13
+ @random_item = Warehouse::Random.new([@item1, @item2, @item3], 4)
14
+ @list = @random_item.fetch! # (Warehouse::List)
15
+ ```
28
16
 
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.
17
+ # Warehouse::List
30
18
 
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).
19
+ ```ruby
20
+ @item1 = Warehouse::Item.new('迷黑', '迷黑', 1, 250)
21
+ @item2 = Warehouse::Item.new('迷黃', '迷黃', 1, 250)
22
+ @item3 = Warehouse::Item.new('迷綠', '迷綠', 1, 250)
23
+ @list = Warehouse::List.new([@item1, @item2, @item3])
32
24
 
33
- ## Contributing
25
+ # 所有數量變兩倍 (會解壓縮 RandomItem)
26
+ @list *= 2
34
27
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/warehouse_items.
28
+ # 迷黑數量 + 1
29
+ @list += @item1
36
30
 
37
- ## License
31
+ # 解壓 random item (回傳都是已經隨機好的品項)
32
+ @list = @random_item.items!
38
33
 
39
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+ # 回傳 item
35
+ @list.find('迷黑')
36
+ ```
@@ -0,0 +1,26 @@
1
+ module Warehouse
2
+ module Item
3
+ class Barcode < Base
4
+ def initialize(*)
5
+ super
6
+ self.code_type = :barcode
7
+ end
8
+
9
+ def barcode
10
+ code_or_barcode
11
+ end
12
+
13
+ def code
14
+ raise NoMethodError, "use #barcode instead."
15
+ end
16
+
17
+ def to_h
18
+ {
19
+ 'barcode' => code_or_barcode,
20
+ 'title' => title,
21
+ 'qty' => qty
22
+ }
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ module Warehouse
2
+ module Item
3
+ Base = Struct.new(:code_or_barcode, :title, :qty, :price) do
4
+ attr_accessor :code_type
5
+ attr_accessor :items
6
+
7
+ def barcode?
8
+ code_type == :barcode
9
+ end
10
+
11
+ def code?
12
+ code_type == :code
13
+ end
14
+
15
+ def type
16
+ 'single'
17
+ end
18
+
19
+ def random?
20
+ false
21
+ end
22
+
23
+ def single?
24
+ true
25
+ end
26
+
27
+ def deep_dup
28
+ self.class.new(code_or_barcode, title, qty, price)
29
+ end
30
+
31
+ # 給 form 用的
32
+ def persisted?; false end
33
+ def product; end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ module Warehouse
2
+ module Item
3
+ class Code < Base
4
+ def initialize(*)
5
+ super
6
+ self.code_type = :code
7
+ end
8
+
9
+ def code
10
+ code_or_barcode
11
+ end
12
+
13
+ def to_h
14
+ {
15
+ 'code' => code_or_barcode,
16
+ 'title' => title,
17
+ 'qty' => qty
18
+ }
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,39 @@
1
+ module Warehouse
2
+ module Item
3
+ Random = Struct.new(:items, :qty) do
4
+ attr_accessor :code, :title, :price
5
+
6
+ def type
7
+ 'random'
8
+ end
9
+
10
+ def random?
11
+ true
12
+ end
13
+
14
+ def single?
15
+ false
16
+ end
17
+
18
+ def fetch
19
+ @fetch ||= fetch!
20
+ end
21
+
22
+ def fetch!
23
+ random_items = {}
24
+ (1..qty).each do
25
+ item = items.sample
26
+ random_items[item.code] ||= Warehouse::Item::Code.new(item.code, item.title, 0, item.price)
27
+ random_items[item.code].qty += 1
28
+ end
29
+ Warehouse::List.new(random_items.values)
30
+ end
31
+
32
+ def persisted?; end
33
+
34
+ def deep_dup
35
+ Warehouse::Item::Random.new(items.map { |item| item.deep_dup }, qty)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -4,32 +4,43 @@ class Warehouse::List
4
4
  delegate :empty?, to: :items
5
5
  delegate :length, to: :items
6
6
  delegate :present?, to: :items
7
+ delegate :any?, to: :items
8
+ delegate :all?, to: :items
7
9
  delegate :each, to: :items
8
10
  delegate :each_with_index, to: :items
9
11
  delegate :map, to: :items
10
12
  delegate :[], to: :items
11
13
  delegate :to_json, to: :items
12
14
 
13
- def initialize(items=nil)
14
- self.items = (items || [])
15
- end
16
-
17
15
  class << self
18
16
  def none
19
17
  new
20
18
  end
21
19
  end
22
20
 
21
+ def initialize(items=nil)
22
+ self.items = (items || [])
23
+ end
24
+
23
25
  # 找到此清單底下料號 (同樣 object_id)
24
- def find(code)
25
- items.detect { |item| item.code == code }
26
+ def find(code, type: nil)
27
+ case type
28
+ when :code
29
+ items.detect { |item| item.code? && item.code == code }
30
+ when :barcode
31
+ items.detect { |item| item.barcode? && item.barcode == code }
32
+ else
33
+ items.detect do |item|
34
+ (item.code? && item.code == code) || (item.barcode? && item.barcode == code)
35
+ end
36
+ end
26
37
  end
27
38
 
28
39
  # 解開隨機料號 (New List)
29
40
  def items!
30
41
  list = Warehouse::List.new
31
42
  items.each do |item|
32
- extra_items = item.is_a?(Warehouse::Random) ? item.fetch! : [ item ]
43
+ extra_items = item.is_a?(Warehouse::Item::Random) ? item.fetch! : [ item ]
33
44
 
34
45
  extra_items.each do |item|
35
46
  list.items << item
@@ -41,9 +52,9 @@ class Warehouse::List
41
52
  # 把料號加入到清單內 (Clone List)
42
53
  def +(item)
43
54
  case item
44
- when Warehouse::Item
55
+ when Warehouse::Item::Code, Warehouse::Item::Barcode
45
56
  add_item(item)
46
- when Warehouse::Random
57
+ when Warehouse::Item::Random
47
58
  # 隨機抽一個
48
59
  add_item(item.fetch!)
49
60
  when Warehouse::List
@@ -68,11 +79,30 @@ class Warehouse::List
68
79
  Warehouse::List.new(items.map { |item| item.deep_dup })
69
80
  end
70
81
 
82
+ def to_h
83
+ items.map do |item|
84
+ [
85
+ (item.code? ? item.code : item.barcode),
86
+ item.qty
87
+ ]
88
+ end.to_h
89
+ end
90
+
91
+ def to_a
92
+ items.map(&:to_h)
93
+ end
94
+
71
95
  protected
72
96
 
73
97
  def add_item(item)
74
98
  list = deep_dup
75
- target = list.find(item.code)
99
+ # 找到 barcode or code item
100
+ target = if item.code?
101
+ list.find(item.code, type: :code)
102
+ else
103
+ list.find(item.barcode, type: :barcode)
104
+ end
105
+
76
106
  if target.present?
77
107
  target.qty = (target.qty + item.qty)
78
108
  else
@@ -1,3 +1,3 @@
1
1
  module Warehouse
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/warehouse.rb CHANGED
@@ -2,8 +2,10 @@ require "warehouse/version"
2
2
  require 'active_support/core_ext/object'
3
3
  require 'active_support/core_ext/class'
4
4
  require 'active_support'
5
- require "warehouse/item"
6
- require "warehouse/random"
5
+ require "warehouse/item/base"
6
+ require "warehouse/item/code"
7
+ require "warehouse/item/barcode"
8
+ require "warehouse/item/random"
7
9
  require "warehouse/list"
8
10
 
9
11
  module Warehouse
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: warehouse_items
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eddie Li
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-12-07 00:00:00.000000000 Z
11
+ date: 2019-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -97,9 +97,11 @@ files:
97
97
  - bin/console
98
98
  - bin/setup
99
99
  - lib/warehouse.rb
100
- - lib/warehouse/item.rb
100
+ - lib/warehouse/item/barcode.rb
101
+ - lib/warehouse/item/base.rb
102
+ - lib/warehouse/item/code.rb
103
+ - lib/warehouse/item/random.rb
101
104
  - lib/warehouse/list.rb
102
- - lib/warehouse/random.rb
103
105
  - lib/warehouse/version.rb
104
106
  - warehouse_items.gemspec
105
107
  homepage: https://github.com/superlanding/warehouse_items
@@ -1,21 +0,0 @@
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
@@ -1,36 +0,0 @@
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