warehouse_items 0.1.1 → 1.0.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
- SHA1:
3
- metadata.gz: 98e337833a9d62456fe917317e31ba5b7920a8d8
4
- data.tar.gz: fd67a7f10709c72beb1b0724dc8da69333044504
2
+ SHA256:
3
+ metadata.gz: ffa41dc7cfb37c0cafee84925e709e012391a3bda4728bf529bee62091c1e1f5
4
+ data.tar.gz: 81884116cb317e6510f8b72e45a9e883672480171dcd428f7f23074a51992153
5
5
  SHA512:
6
- metadata.gz: 2df7dbdf07b08217e35ed6db18bdee06fba51111777a371234592ff7246e5ffdaee5bd81dfcaf2ff004eb7dd2806cdf640cbe40aa1fa37e39129fb93840ea56e
7
- data.tar.gz: 8f8954b931cd2003ba30837e81a3dc04fb74f1cceab52d663d0d9ed4fb8e15d281c4cea311511d174d93ac37d721e90bac83e2f41bb6ece13ea27abcdc3bbbd1
6
+ metadata.gz: 3b805174c9972f0f23cd470a08762fc9a1505483e24cb07bb755f008d8edde1bd6cea28f8fcdf3e70e31a888a6cf04b9c64efc229f35a5648136acfc4e9ea46f
7
+ data.tar.gz: 9cbb09b6c45a804e5bf6bb2ea9567affa9b935755d6a0f0f121cc8c4626b383457a23c45b779ec1bc26ac14f4e98b43c1e1ca0c164b1337dba85069653c36328
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.*.sw?
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.6.7
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.3.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::Code.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::Code.new('迷黑', '迷黑', nil, 250)
11
+ @item2 = Warehouse::Item::Code.new('迷黃', '迷黃', nil, 250)
12
+ @item3 = Warehouse::Item::Code.new('迷綠', '迷綠', nil, 250)
13
+ @random_item = Warehouse::Item::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::Code.new('迷黑', '迷黑', 1, 250)
21
+ @item2 = Warehouse::Item::Code.new('迷黃', '迷黃', 1, 250)
22
+ @item3 = Warehouse::Item::Code.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,42 @@
1
+ module Warehouse
2
+ module Item
3
+ Barcode = Struct.new(:barcode, :title, :qty) do
4
+
5
+ def barcode?
6
+ true
7
+ end
8
+
9
+ def code?
10
+ false
11
+ end
12
+
13
+ def type
14
+ 'single'
15
+ end
16
+
17
+ def random?
18
+ false
19
+ end
20
+
21
+ def single?
22
+ true
23
+ end
24
+
25
+ def to_h
26
+ {
27
+ 'barcode' => barcode,
28
+ 'title' => title,
29
+ 'qty' => qty
30
+ }
31
+ end
32
+
33
+ def deep_dup
34
+ self.class.new(barcode, title, qty)
35
+ end
36
+
37
+ # 給 form 用的
38
+ def persisted?; false end
39
+ def product; end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,56 @@
1
+ module Warehouse
2
+ module Item
3
+ Code = Struct.new(:code, :title, :qty, :price, :barcode, :code_2, :title_en) do
4
+
5
+ def self.build(fragment)
6
+ qty = fragment["qty"].nil? ? 0 : fragment["qty"].to_i
7
+ Code.new(fragment["code"], fragment["title"], qty, fragment["price"].to_i,
8
+ fragment["barcode"], fragment["code_2"], fragment["title_en"])
9
+ end
10
+
11
+ def is_free?
12
+ price == 0
13
+ end
14
+
15
+ def barcode?
16
+ false
17
+ end
18
+
19
+ def code?
20
+ true
21
+ end
22
+
23
+ def type
24
+ 'single'
25
+ end
26
+
27
+ def random?
28
+ false
29
+ end
30
+
31
+ def single?
32
+ true
33
+ end
34
+
35
+ def to_h
36
+ {
37
+ 'code' => code,
38
+ 'title' => title,
39
+ 'qty' => qty,
40
+ 'price' => price,
41
+ 'barcode' => barcode,
42
+ 'code_2' => code_2,
43
+ 'title_en' => title_en
44
+ }
45
+ end
46
+
47
+ def deep_dup
48
+ self.class.new(code, title, qty, price, barcode, code_2, title_en)
49
+ end
50
+
51
+ # 給 form 用的
52
+ def persisted?; false end
53
+ def product; end
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,40 @@
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
+ item.barcode, item.code_2, item.title_en)
28
+ random_items[item.code].qty += 1
29
+ end
30
+ Warehouse::List.new(random_items.values)
31
+ end
32
+
33
+ def persisted?; end
34
+
35
+ def deep_dup
36
+ Warehouse::Item::Random.new(items.map { |item| item.deep_dup }, qty)
37
+ end
38
+ end
39
+ end
40
+ 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 = "1.0.0"
3
3
  end
data/lib/warehouse.rb CHANGED
@@ -2,10 +2,11 @@ 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/code"
6
+ require "warehouse/item/barcode"
7
+ require "warehouse/item/random"
7
8
  require "warehouse/list"
8
9
 
9
10
  module Warehouse
10
-
11
+
11
12
  end
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: 1.0.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: 2022-08-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -88,6 +88,7 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
  files:
90
90
  - ".gitignore"
91
+ - ".ruby-version"
91
92
  - ".travis.yml"
92
93
  - Gemfile
93
94
  - Gemfile.lock
@@ -97,9 +98,10 @@ files:
97
98
  - bin/console
98
99
  - bin/setup
99
100
  - lib/warehouse.rb
100
- - lib/warehouse/item.rb
101
+ - lib/warehouse/item/barcode.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
@@ -121,8 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
121
123
  - !ruby/object:Gem::Version
122
124
  version: '0'
123
125
  requirements: []
124
- rubyforge_project:
125
- rubygems_version: 2.4.8
126
+ rubygems_version: 3.0.3.1
126
127
  signing_key:
127
128
  specification_version: 4
128
129
  summary: warehouse_items wrapper & value objects
@@ -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