stockman-logic 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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b1950d32a54e76c7af280dfd15010366d292148d
4
+ data.tar.gz: a7f9dbf94a3fe7b3ca79f75381dd305c4293aaf7
5
+ SHA512:
6
+ metadata.gz: 1990b8970e576a275d849424a3a2c30e9f1bec55d031fe5d34373f02136313d2aed82d133a13dc70377bf7dca70a967076684214105e8d6d0596e4558a10216d
7
+ data.tar.gz: a04285334262c0aea9b369a523b1aed976dfcf52fce0fc73d3b033d037123ed2d58d60441089ac8fdd8b094160f2e04fdfecdc08b4295c338f5f4927cf121530
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in stockman-logic.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Vasiliy Reys
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,40 @@
1
+ # Stockman::Logic
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/stockman/logic`. 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 'stockman-logic'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install stockman-logic
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]/stockman-logic.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://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 "stockman/logic"
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
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
@@ -0,0 +1,45 @@
1
+ module Stockman
2
+ module Logic
3
+ class Kit
4
+ class Content
5
+ attr_reader :kit_id, :variant_id, :amount_in_kit
6
+
7
+ def initialize(kit_id, variant_id, amount_in_kit)
8
+ @kit_id = kit_id.to_i
9
+ @variant_id = variant_id.to_i
10
+ @amount_in_kit = amount_in_kit.to_i
11
+ end
12
+
13
+ def variant
14
+ @variant ||= Variant.new(variant_id)
15
+ end
16
+
17
+ def warehouse_ids
18
+ @warehouse_ids ||= variant.warehouse_ids
19
+ end
20
+
21
+ def stock_levels
22
+ @stock_levels ||= variant.stock_levels
23
+ end
24
+
25
+ def physical_amount(warehouse_id)
26
+ stock_level_amount(:physical, warehouse_id)
27
+ end
28
+
29
+ def available_amount(warehouse_id)
30
+ stock_level_amount(:available, warehouse_id)
31
+ end
32
+
33
+ private
34
+
35
+ def stock_level_amount(type, warehouse_id)
36
+ stock_level = stock_levels.find_by_warehouse(warehouse_id)
37
+
38
+ return 0 if amount_in_kit.zero? || stock_level.nil?
39
+
40
+ stock_level.send(:"#{type}_amount") / amount_in_kit
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,47 @@
1
+ module Stockman
2
+ module Logic
3
+ class Kit
4
+ class ContentsCollection
5
+ include Enumerable
6
+
7
+ attr_reader :kit_id
8
+
9
+ def initialize(kit_id)
10
+ @kit_id = kit_id.to_i
11
+ end
12
+
13
+ def [](index)
14
+ contents[index]
15
+ end
16
+
17
+ def each
18
+ contents.each{ |content| yield content }
19
+ end
20
+
21
+ def minimum_physical_amount(warehouse_id)
22
+ minimum_amount(:physical, warehouse_id)
23
+ end
24
+
25
+ def minimum_available_amount(warehouse_id)
26
+ minimum_amount(:available, warehouse_id)
27
+ end
28
+
29
+ def contents
30
+ @contents ||= load_contents
31
+ end
32
+
33
+ private
34
+
35
+ def load_contents
36
+ Storage.obtain_hash("kits:#{kit_id}:variants").map do |variant_id, amount_in_kit|
37
+ Content.new(kit_id, variant_id, amount_in_kit)
38
+ end
39
+ end
40
+
41
+ def minimum_amount(type, warehouse_id)
42
+ contents.map{|content| content.send(:"#{type}_amount", warehouse_id)}.min
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ module Stockman
2
+ module Logic
3
+ class Kit
4
+ class StockLevel
5
+ attr_reader :kit_contents, :warehouse_id
6
+
7
+ def initialize(kit_contents, warehouse_id)
8
+ @kit_contents = kit_contents
9
+ @warehouse_id = warehouse_id.to_i
10
+ end
11
+
12
+ def physical_amount
13
+ kit_contents.minimum_physical_amount(warehouse_id)
14
+ end
15
+
16
+ def allocated_amount
17
+ 0
18
+ end
19
+
20
+ def available_amount
21
+ kit_contents.minimum_available_amount(warehouse_id)
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,51 @@
1
+ module Stockman
2
+ module Logic
3
+ class Kit
4
+ class StockLevelsCollection
5
+ include Enumerable
6
+
7
+ attr_reader :kit_id
8
+
9
+ def initialize(kit_id)
10
+ @kit_id = kit_id.to_i
11
+ end
12
+
13
+ def [](warehouse_id)
14
+ find_by_warehouse(warehouse_id)
15
+ end
16
+
17
+ def find_by_warehouse(warehouse_id)
18
+ stock_levels.detect{ |stock_level| stock_level.warehouse_id == warehouse_id }
19
+ end
20
+
21
+ def each
22
+ stock_levels.each{ |stock_level| yield stock_level }
23
+ end
24
+
25
+ def empty?
26
+ stock_levels.empty?
27
+ end
28
+
29
+ def contents
30
+ @contents ||= ContentsCollection.new(kit_id)
31
+ end
32
+
33
+ def stock_levels
34
+ @stock_levels ||= load_stock_levels
35
+ end
36
+
37
+ def warehouse_ids
38
+ @warehouse_ids ||= contents.map(&:warehouse_ids).flatten.uniq
39
+ end
40
+
41
+ private
42
+
43
+ def load_stock_levels
44
+ warehouse_ids.inject([]) do |memo, warehouse_id|
45
+ memo << StockLevel.new(contents, warehouse_id)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,60 @@
1
+ require_relative "kit/content"
2
+ require_relative "kit/contents_collection"
3
+ require_relative "kit/stock_level"
4
+ require_relative "kit/stock_levels_collection"
5
+
6
+ module Stockman
7
+ module Logic
8
+ class Kit
9
+ attr_reader :id
10
+
11
+ def initialize(id)
12
+ @id = id.to_i
13
+ end
14
+
15
+ def total_physical_amount
16
+ stock_levels.sum(&:physical_amount)
17
+ end
18
+
19
+ def total_allocated_amount
20
+ 0
21
+ end
22
+
23
+ def total_available_amount
24
+ stock_levels.sum(&:available_amount)
25
+ end
26
+
27
+ def stock_levels
28
+ @stock_levels ||= StockLevelsCollection.new(id)
29
+ end
30
+
31
+ def warehouse_ids
32
+ stock_levels.map(&:warehouse_id)
33
+ end
34
+
35
+ def summary
36
+ @summary ||= Summary.from_object(self)
37
+ end
38
+
39
+ def physical_amount(warehouse_id)
40
+ obtain_stock_level_amount(:physical, warehouse_id)
41
+ end
42
+
43
+ def allocated_amount(warehouse_id)
44
+ 0
45
+ end
46
+
47
+ def available_amount(warehouse_id)
48
+ obtain_stock_level_amount(:available, warehouse_id)
49
+ end
50
+
51
+ private
52
+
53
+ def obtain_stock_level_amount(type, warehouse_id)
54
+ return 0 unless warehouse_ids.include?(warehouse_id.to_i)
55
+
56
+ stock_levels.find_by_warehouse(warehouse_id).send(:"#{type}_amount")
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,35 @@
1
+ module Stockman
2
+ module Logic
3
+ class Product
4
+ class SellablesCollection
5
+ include Enumerable
6
+
7
+ attr_reader :product_id
8
+
9
+ def initialize(product_id)
10
+ @product_id = product_id
11
+ end
12
+
13
+ def sellables
14
+ @sellables ||= load_sellables
15
+ end
16
+
17
+ def stock_levels
18
+ @stock_levels ||= sellables.map(&:stock_levels).flatten.map(&:stock_levels).flatten
19
+ end
20
+
21
+ def each
22
+ sellables.each{ |sellable| yield sellable }
23
+ end
24
+
25
+ private
26
+
27
+ def load_sellables
28
+ Storage.obtain_set("products:#{product_id}:sellables").map do |sellable_data|
29
+ SellableFactory.build_sellable(sellable_data)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,32 @@
1
+ module Stockman
2
+ module Logic
3
+ class Product
4
+ class StockLevel
5
+ attr_reader :sellables, :warehouse_id
6
+
7
+ def initialize(sellables, warehouse_id)
8
+ @sellables = sellables
9
+ @warehouse_id = warehouse_id.to_i
10
+ end
11
+
12
+ def physical_amount
13
+ sellables_stock_levels.sum(&:physical_amount)
14
+ end
15
+
16
+ def allocated_amount
17
+ sellables_stock_levels.sum(&:allocated_amount)
18
+ end
19
+
20
+ def available_amount
21
+ sellables_stock_levels.sum(&:available_amount)
22
+ end
23
+
24
+ def sellables_stock_levels
25
+ @sellables_stock_levels ||= sellables.stock_levels.select do |stock_level|
26
+ stock_level.warehouse_id == warehouse_id
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,55 @@
1
+ module Stockman
2
+ module Logic
3
+ class Product
4
+ class StockLevelsCollection
5
+ include Enumerable
6
+
7
+ attr_reader :product_id
8
+
9
+ def initialize(product_id)
10
+ @product_id = product_id.to_i
11
+ end
12
+
13
+ def sellables
14
+ @sellables ||= SellablesCollection.new(product_id)
15
+ end
16
+
17
+ def stock_levels
18
+ @stock_levels ||= load_stock_levels
19
+ end
20
+
21
+ def warehouse_ids
22
+ @warehouse_ids ||= load_warehouse_ids
23
+ end
24
+
25
+ def each
26
+ stock_levels.each{ |stock_level| yield stock_level }
27
+ end
28
+
29
+ def empty?
30
+ stock_levels.empty?
31
+ end
32
+
33
+ def [](warehouse_id)
34
+ find_by_warehouse(warehouse_id)
35
+ end
36
+
37
+ def find_by_warehouse(warehouse_id)
38
+ stock_levels.detect{ |stock_level| stock_level.warehouse_id == warehouse_id }
39
+ end
40
+
41
+ private
42
+
43
+ def load_warehouse_ids
44
+ sellables.map(&:warehouse_ids).flatten.uniq
45
+ end
46
+
47
+ def load_stock_levels
48
+ warehouse_ids.inject([]) do |memo, warehouse_id|
49
+ memo << StockLevel.new(sellables, warehouse_id)
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ require_relative "product/sellables_collection"
2
+ require_relative "product/stock_level"
3
+ require_relative "product/stock_levels_collection"
4
+
5
+ module Stockman
6
+ module Logic
7
+ class Product
8
+ attr_reader :id
9
+
10
+ def initialize(id)
11
+ @id = id
12
+ end
13
+
14
+ def total_physical_amount
15
+ stock_levels.sum(&:physical_amount)
16
+ end
17
+
18
+ def total_allocated_amount
19
+ stock_levels.sum(&:allocated_amount)
20
+ end
21
+
22
+ def total_available_amount
23
+ stock_levels.sum(&:available_amount)
24
+ end
25
+
26
+ def stock_levels
27
+ @stock_levels ||= StockLevelsCollection.new(id)
28
+ end
29
+
30
+ def summary
31
+ @summary ||= Summary.from_object(self)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,15 @@
1
+ module Stockman
2
+ module Logic
3
+ class SellableFactory
4
+ VALID_SELLABLE_TYPES = ['variants', 'kits']
5
+
6
+ def self.build_sellable(sellable_data)
7
+ type, id = *sellable_data.split(":")
8
+
9
+ raise "#{type} is not valid sellable type" unless VALID_SELLABLE_TYPES.include?(type)
10
+
11
+ "Stockman::Logic::#{type.singularize.classify}".constantize.new(id.to_i)
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,19 @@
1
+ module Stockman
2
+ module Logic
3
+ class Storage
4
+ def self.obtain_set(key)
5
+ Logic.redis.smembers(key)
6
+ end
7
+
8
+ def self.obtain_hash(key)
9
+ Logic.redis.hgetall(key)
10
+ end
11
+
12
+ def self.obtain_amount(key, field)
13
+ value = Logic.redis.hget(key, field)
14
+
15
+ Utils.deserialize_amount(value)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,78 @@
1
+ module Stockman
2
+ module Logic
3
+ class Summary
4
+ attr_reader :data
5
+
6
+ def self.from_hash(hash)
7
+ data = transform_values(hash, &Utils.method(:deserialize_amount))
8
+
9
+ new(data)
10
+ end
11
+
12
+ def self.from_object(object)
13
+ data = {
14
+ physical_amount: object.total_physical_amount,
15
+ allocated_amount: object.total_allocated_amount,
16
+ available_amount: object.total_available_amount,
17
+
18
+ warehouses: object.stock_levels.inject({}) do |memo, stock_level|
19
+ memo.merge(
20
+ stock_level.warehouse_id => {
21
+ physical_amount: stock_level.physical_amount,
22
+ allocated_amount: stock_level.allocated_amount,
23
+ available_amount: stock_level.available_amount
24
+ }
25
+ )
26
+ end
27
+ }
28
+
29
+ new(data)
30
+ end
31
+
32
+ def initialize(data)
33
+ @data = data
34
+ end
35
+
36
+ def total_physical_amount
37
+ data[:physical_amount]
38
+ end
39
+
40
+ def total_allocated_amount
41
+ data[:allocated_amount]
42
+ end
43
+
44
+ def total_available_amount
45
+ data[:available_amount]
46
+ end
47
+
48
+ def warehouses
49
+ data[:warehouses]
50
+ end
51
+
52
+ def to_hash
53
+ self.class.transform_values(data, &Utils.method(:serialize_amount))
54
+ end
55
+
56
+ def to_json(_ = nil)
57
+ JSON.generate(to_hash)
58
+ end
59
+
60
+ private
61
+
62
+ def self.transform_values(hash, &block)
63
+ method = lambda do |memo, data|
64
+ memo.merge data[0] => yield(data[1])
65
+ end
66
+
67
+ totals = hash.symbolize_keys.slice(:physical_amount, :allocated_amount, :available_amount)
68
+ .inject({}, &method)
69
+
70
+ warehouses = hash.symbolize_keys[:warehouses].inject({}) do |memo, data|
71
+ memo.merge data[0].to_i => data[1].inject({}, &method).symbolize_keys
72
+ end
73
+
74
+ totals.merge(warehouses: warehouses)
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,21 @@
1
+ module Stockman
2
+ module Logic
3
+ class Utils
4
+ def self.serialize_amount(value)
5
+ if value == Float::INFINITY
6
+ 'inf'
7
+ else
8
+ value.to_i
9
+ end
10
+ end
11
+
12
+ def self.deserialize_amount(value)
13
+ if value == 'inf'
14
+ Float::INFINITY
15
+ else
16
+ value.to_i
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,32 @@
1
+ module Stockman
2
+ module Logic
3
+ class Variant
4
+ class StockLevel
5
+ attr_reader :variant_id, :warehouse_id
6
+
7
+ def initialize(variant_id, warehouse_id)
8
+ @variant_id = variant_id.to_i
9
+ @warehouse_id = warehouse_id.to_i
10
+ end
11
+
12
+ def physical_amount
13
+ obtain_amount(:physical)
14
+ end
15
+
16
+ def allocated_amount
17
+ obtain_amount(:allocated)
18
+ end
19
+
20
+ def available_amount
21
+ physical_amount - allocated_amount
22
+ end
23
+
24
+ private
25
+
26
+ def obtain_amount(type)
27
+ Storage.obtain_amount("variants:#{variant_id}:stock_levels:#{warehouse_id}", :"#{type}_amount")
28
+ end
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,37 @@
1
+ module Stockman
2
+ module Logic
3
+ class Variant
4
+ class StockLevelsCollection
5
+ include Enumerable
6
+
7
+ attr_reader :variant_id
8
+
9
+ def initialize(variant_id)
10
+ @variant_id = variant_id.to_i
11
+ end
12
+
13
+ def stock_levels
14
+ @stock_levels ||= Storage.obtain_set("variants:#{variant_id}:warehouses").map do |warehouse_id|
15
+ StockLevel.new(variant_id, warehouse_id)
16
+ end
17
+ end
18
+
19
+ def each
20
+ stock_levels.each{ |stock_level| yield stock_level }
21
+ end
22
+
23
+ def empty?
24
+ stock_levels.empty?
25
+ end
26
+
27
+ def [](warehouse_id)
28
+ find_by_warehouse(warehouse_id)
29
+ end
30
+
31
+ def find_by_warehouse(warehouse_id)
32
+ stock_levels.detect{ |stock_level| stock_level.warehouse_id == warehouse_id }
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,58 @@
1
+ require_relative 'variant/stock_level'
2
+ require_relative 'variant/stock_levels_collection'
3
+
4
+ module Stockman
5
+ module Logic
6
+ class Variant
7
+ attr_reader :id
8
+
9
+ def initialize(id)
10
+ @id = id.to_i
11
+ end
12
+
13
+ def total_physical_amount
14
+ stock_levels.sum(&:physical_amount)
15
+ end
16
+
17
+ def total_allocated_amount
18
+ stock_levels.sum(&:allocated_amount)
19
+ end
20
+
21
+ def total_available_amount
22
+ total_physical_amount - total_allocated_amount
23
+ end
24
+
25
+ def stock_levels
26
+ @stock_levels ||= StockLevelsCollection.new(id)
27
+ end
28
+
29
+ def warehouse_ids
30
+ stock_levels.map(&:warehouse_id)
31
+ end
32
+
33
+ def physical_amount(warehouse_id)
34
+ obtain_stock_level_amount(:physical, warehouse_id)
35
+ end
36
+
37
+ def allocated_amount(warehouse_id)
38
+ obtain_stock_level_amount(:allocated, warehouse_id)
39
+ end
40
+
41
+ def available_amount(warehouse_id)
42
+ obtain_stock_level_amount(:available, warehouse_id)
43
+ end
44
+
45
+ def summary
46
+ @summary ||= Summary.from_object(self)
47
+ end
48
+
49
+ private
50
+
51
+ def obtain_stock_level_amount(type, warehouse_id)
52
+ return 0 unless warehouse_ids.include?(warehouse_id.to_i)
53
+
54
+ stock_levels.find_by_warehouse(warehouse_id).send(:"#{type}_amount")
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,5 @@
1
+ module Stockman
2
+ module Logic
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,20 @@
1
+ require "active_support/core_ext/enumerable"
2
+ require "active_support/core_ext/string/inflections"
3
+ require 'active_support/core_ext/hash'
4
+
5
+ require_relative "logic/version"
6
+ require_relative "logic/utils"
7
+ require_relative "logic/storage"
8
+ require_relative "logic/summary"
9
+ require_relative "logic/variant"
10
+ require_relative "logic/kit"
11
+ require_relative "logic/sellable_factory"
12
+ require_relative "logic/product"
13
+
14
+ module Stockman
15
+ module Logic
16
+ class << self
17
+ attr_accessor :redis
18
+ end
19
+ end
20
+ end
data/lib/stockman.rb ADDED
@@ -0,0 +1 @@
1
+ require_relative 'stockman/logic'
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'stockman/logic/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "stockman-logic"
8
+ spec.version = Stockman::Logic::VERSION
9
+ spec.authors = ["Vasiliy Reys"]
10
+ spec.email = ["vasiliy.reys@gmail.com"]
11
+
12
+ spec.summary = %q{Stockman stock levels calculation logic}
13
+ spec.homepage = "http://veeqo.com"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.12"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ spec.add_development_dependency "rspec", "~> 3.2"
24
+ spec.add_development_dependency "redis", "~> 3.3.3"
25
+ spec.add_development_dependency "byebug", "~> 9.1.0"
26
+
27
+ spec.add_dependency "activesupport", '>= 3.2', '< 5.0'
28
+ end
data/test.rb ADDED
@@ -0,0 +1,148 @@
1
+ #!/usr/bin/env ruby
2
+ require 'active_support/core_ext/hash'
3
+ # require 'active_support/hash_with_indifferent_access'
4
+ require 'pp'
5
+ require 'json'
6
+ require 'ostruct'
7
+
8
+ module Stockman
9
+ module Logic
10
+ class Utils
11
+ def self.deserialize_amount(value)
12
+ if value == 'inf'
13
+ Float::INFINITY
14
+ else
15
+ value.to_i
16
+ end
17
+ end
18
+
19
+ def self.serialize_amount(value)
20
+ if value == Float::INFINITY
21
+ 'inf'
22
+ else
23
+ value.to_i
24
+ end
25
+ end
26
+ end
27
+
28
+ class Summary
29
+ attr_reader :data
30
+
31
+ def self.from_hash(hash)
32
+ data = transform_values(hash, &Utils.method(:deserialize_amount))
33
+
34
+ new(data)
35
+ end
36
+
37
+ def self.from_object(object)
38
+ data = {
39
+ physical_amount: object.total_physical_amount,
40
+ allocated_amount: object.total_allocated_amount,
41
+ available_amount: object.total_available_amount,
42
+
43
+ warehouses: object.stock_levels.inject({}) do |memo, stock_level|
44
+ memo.merge(
45
+ stock_level.warehouse_id => {
46
+ physical_amount: stock_level.physical_amount,
47
+ allocated_amount: stock_level.allocated_amount,
48
+ available_amount: stock_level.available_amount
49
+ }
50
+ )
51
+ end
52
+ }
53
+
54
+ new(data)
55
+ end
56
+
57
+ def initialize(data)
58
+ @data = data
59
+ end
60
+
61
+ def total_physical_amount
62
+ data[:physical_amount]
63
+ end
64
+
65
+ def total_allocated_amount
66
+ data[:allocated_amount]
67
+ end
68
+
69
+ def total_available_amount
70
+ data[:available_amount]
71
+ end
72
+
73
+ def warehouses
74
+ data[:warehouses]
75
+ end
76
+
77
+ def to_hash
78
+ self.class.transform_values(data, &Utils.method(:serialize_amount))
79
+ end
80
+
81
+ def to_json
82
+ JSON.generate(to_hash)
83
+ end
84
+
85
+ private
86
+
87
+ def self.transform_values(hash, &block)
88
+ method = lambda do |memo, data|
89
+ memo.merge data[0] => yield(data[1])
90
+ end
91
+
92
+ totals = hash.symbolize_keys.slice(:physical_amount, :allocated_amount, :available_amount)
93
+ .inject({}, &method)
94
+
95
+ warehouses = hash.symbolize_keys[:warehouses].inject({}) do |memo, data|
96
+ memo.merge data[0].to_i => data[1].inject({}, &method).symbolize_keys
97
+ end
98
+
99
+ totals.merge(warehouses: warehouses)
100
+ end
101
+ end
102
+ end
103
+ end
104
+
105
+ data = {
106
+ physical_amount: Float::INFINITY,
107
+ allocated_amount: 40,
108
+ available_amount: Float::INFINITY,
109
+
110
+ warehouses: {
111
+ 17 => {
112
+ physical_amount: Float::INFINITY,
113
+ allocated_amount: 10,
114
+ available_amount: Float::INFINITY
115
+ },
116
+ 18 => {
117
+ physical_amount: 500,
118
+ allocated_amount: 30,
119
+ available_amount: 470
120
+ }
121
+ }
122
+ }
123
+
124
+ object = OpenStruct.new(
125
+ total_physical_amount: Float::INFINITY,
126
+ total_allocated_amount: 40,
127
+ total_available_amount: Float::INFINITY,
128
+ stock_levels: [
129
+ OpenStruct.new(
130
+ warehouse_id: 17,
131
+ physical_amount: Float::INFINITY,
132
+ allocated_amount: 10,
133
+ available_amount: Float::INFINITY
134
+ ),
135
+ OpenStruct.new(
136
+ warehouse_id: 18,
137
+ physical_amount: 500,
138
+ allocated_amount: 30,
139
+ available_amount: 470
140
+ )
141
+ ]
142
+ )
143
+
144
+ json = %Q{{"physical_amount":"inf","allocated_amount":40,"available_amount":"inf","warehouses":{"17":{"physical_amount":"inf","allocated_amount":10,"available_amount":"inf"},"18":{"physical_amount":500,"allocated_amount":30,"available_amount":470}}}}
145
+
146
+ puts Stockman::Logic::Summary.new(data).to_hash
147
+ puts Stockman::Logic::Summary.from_hash(JSON.parse(json)).to_hash
148
+ puts Stockman::Logic::Summary.from_object(object).to_hash
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: stockman-logic
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Vasiliy Reys
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-08-29 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.12'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.12'
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.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redis
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 9.1.0
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 9.1.0
83
+ - !ruby/object:Gem::Dependency
84
+ name: activesupport
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ - - "<"
91
+ - !ruby/object:Gem::Version
92
+ version: '5.0'
93
+ type: :runtime
94
+ prerelease: false
95
+ version_requirements: !ruby/object:Gem::Requirement
96
+ requirements:
97
+ - - ">="
98
+ - !ruby/object:Gem::Version
99
+ version: '3.2'
100
+ - - "<"
101
+ - !ruby/object:Gem::Version
102
+ version: '5.0'
103
+ description:
104
+ email:
105
+ - vasiliy.reys@gmail.com
106
+ executables: []
107
+ extensions: []
108
+ extra_rdoc_files: []
109
+ files:
110
+ - ".gitignore"
111
+ - ".rspec"
112
+ - Gemfile
113
+ - LICENSE.txt
114
+ - README.md
115
+ - Rakefile
116
+ - bin/console
117
+ - bin/setup
118
+ - lib/stockman.rb
119
+ - lib/stockman/logic.rb
120
+ - lib/stockman/logic/kit.rb
121
+ - lib/stockman/logic/kit/content.rb
122
+ - lib/stockman/logic/kit/contents_collection.rb
123
+ - lib/stockman/logic/kit/stock_level.rb
124
+ - lib/stockman/logic/kit/stock_levels_collection.rb
125
+ - lib/stockman/logic/product.rb
126
+ - lib/stockman/logic/product/sellables_collection.rb
127
+ - lib/stockman/logic/product/stock_level.rb
128
+ - lib/stockman/logic/product/stock_levels_collection.rb
129
+ - lib/stockman/logic/sellable_factory.rb
130
+ - lib/stockman/logic/storage.rb
131
+ - lib/stockman/logic/summary.rb
132
+ - lib/stockman/logic/utils.rb
133
+ - lib/stockman/logic/variant.rb
134
+ - lib/stockman/logic/variant/stock_level.rb
135
+ - lib/stockman/logic/variant/stock_levels_collection.rb
136
+ - lib/stockman/logic/version.rb
137
+ - stockman-storage_api.gemspec
138
+ - test.rb
139
+ homepage: http://veeqo.com
140
+ licenses:
141
+ - MIT
142
+ metadata: {}
143
+ post_install_message:
144
+ rdoc_options: []
145
+ require_paths:
146
+ - lib
147
+ required_ruby_version: !ruby/object:Gem::Requirement
148
+ requirements:
149
+ - - ">="
150
+ - !ruby/object:Gem::Version
151
+ version: '0'
152
+ required_rubygems_version: !ruby/object:Gem::Requirement
153
+ requirements:
154
+ - - ">="
155
+ - !ruby/object:Gem::Version
156
+ version: '0'
157
+ requirements: []
158
+ rubyforge_project:
159
+ rubygems_version: 2.5.1
160
+ signing_key:
161
+ specification_version: 4
162
+ summary: Stockman stock levels calculation logic
163
+ test_files: []