unit_manager 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: 72a24ca7e42854fd6d1b3a7e92f8fd983bf74887b0a67b13149ba95f0213f7f5
4
+ data.tar.gz: 3db3386967d769987fefe8c813c6617c83724830638698722d147e79cf6e970f
5
+ SHA512:
6
+ metadata.gz: f36cc4400be9659e4b1518c1343ae96eb9504deadfe3a8ea95ce95ce990b8a3be40277071a03f87691679998e968c42038199e4f5e55a529ff0e6475a41f8ca8
7
+ data.tar.gz: 42835c1beaf75935656414a82a2d31e18002a09e74b2007d57ff1715f3b58055b804e270a4f5ce6c654cca3dad064e49e83402b3ccd0041e327ea72ee34c4598
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in unit_manager.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,73 @@
1
+ # UnitManager
2
+
3
+ ## 概要
4
+
5
+ 単位を管理する gem です。この gem を活用することで、「円」や「cm」などのモノの単位を扱いやすくします。
6
+ 一つの yml ファイルに単位名と単位を表示する時の計算式を記述しておくことで、プログラム内で単位を呼び出す時にその計算式に従って単位名と共に値が出力されます。
7
+ 単位や計算式を修正する場合も yml ファイルを編集するのみのため、プログラム上の修正は不要になります。
8
+
9
+ ## 使用方法
10
+
11
+ ### 1. gemfile に追記
12
+
13
+ ```
14
+ gem 'unit_manager'
15
+ ```
16
+
17
+ ### 2. コマンドを利用して gem UnitManager をプロジェクトで利用可能にする
18
+
19
+ **必ずプロジェクトディレクトリ直下で使用してください(gemfile が存在する場所)**
20
+ 単位を管理する yml ファイルが作成されます。
21
+
22
+ ```
23
+ unit init
24
+ ```
25
+
26
+ ### 3. /unit_config/unit.yml に扱う単位を記述する
27
+
28
+ equation で指定した式により計算された値が単位名と共に戻り値として受け取れます。
29
+
30
+ - key: 単位のキー名 `(キー名を利用してプログラムから単位にアクセスする)`
31
+ - name: 単位名 `(プログラムから単位が呼び出された時に値と共に出力される単位名 例: 円)`
32
+ - quation: 単位算出式 `(ここで定義された計算式を利用して値が算出されて表示される)`
33
+
34
+ #### equation について
35
+
36
+ - 式中の<value>と記述した部分にプログラム内で指定した値が代入されて計算されます。
37
+ - 演算子は+, -, \*, /が使用できます。
38
+
39
+ ```
40
+ # 例
41
+ units:
42
+ - key: money
43
+ name: 円
44
+ equation: <value> * 1.1
45
+ - key: length
46
+ name: m
47
+ equation: <value>
48
+ ```
49
+
50
+ ### 4. gem を require する
51
+
52
+ ```
53
+ require('unit_manager')
54
+ ```
55
+
56
+ ### 5. unit_manager をプログラム内で利用可能にする
57
+
58
+ UnitManager::InitializeUnit.new を利用することで、yml ファイルに定義した単位が読み込まれます。
59
+ UnitManager::InitializeUnit クラスのアクセサメソッドである units に単位情報が入っています。
60
+
61
+ ```
62
+ units = UnitManager::InitializeUnit.new.units
63
+ ```
64
+
65
+ ### 6. 単位を取得する
66
+
67
+ unit.yml に記述されているキー名が、units のキー名になっています。このキー名によって取得する単位を切り替えます。
68
+ units['指定のキー名']の中に unit クラスが存在し、unit メソッドによって単位が呼び出されます。このメソッドの引数に値を渡す事によって equation で指定した計算式に従って単位も含めて値を出力します。
69
+
70
+ ```
71
+ units['money'].unit(1000)
72
+ => 1100円
73
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
File without changes
@@ -0,0 +1,73 @@
1
+ # UnitManager
2
+
3
+ ## 概要
4
+
5
+ 単位を管理する gem です。この gem を活用することで、「円」や「cm」などのモノの単位を扱いやすくします。
6
+ 一つの yml ファイルに単位名と単位を表示する時の計算式を記述しておくことで、プログラム内で単位を呼び出す時にその計算式に従って単位名と共に値が出力されます。
7
+ 単位や計算式を修正する場合も yml ファイルを編集するのみのため、プログラム上の修正は不要になります。
8
+
9
+ ## 使用方法
10
+
11
+ ### 1. gemfile に追記
12
+
13
+ ```
14
+ gem 'unit_manager'
15
+ ```
16
+
17
+ ### 2. コマンドを利用して gem UnitManager をプロジェクトで利用可能にする
18
+
19
+ **必ずプロジェクトディレクトリ直下で使用してください(gemfile が存在する場所)**
20
+ 単位を管理する yml ファイルが作成されます。
21
+
22
+ ```
23
+ unit init
24
+ ```
25
+
26
+ ### 3. /unit_config/unit.yml に扱う単位を記述する
27
+
28
+ equation で指定した式により計算された値が単位名と共に戻り値として受け取れます。
29
+
30
+ - key: 単位のキー名 `(キー名を利用してプログラムから単位にアクセスする)`
31
+ - name: 単位名 `(プログラムから単位が呼び出された時に値と共に出力される単位名 例: 円)`
32
+ - quation: 単位算出式 `(ここで定義された計算式を利用して値が算出されて表示される)`
33
+
34
+ #### equation について
35
+
36
+ - 式中の<value>と記述した部分にプログラム内で指定した値が代入されて計算されます。
37
+ - 演算子は+, -, \*, /が使用できます。
38
+
39
+ ```
40
+ # 例
41
+ units:
42
+ - key: money
43
+ name: 円
44
+ equation: <value> * 1.1
45
+ - key: length
46
+ name: m
47
+ equation: <value>
48
+ ```
49
+
50
+ ### 4. gem を require する
51
+
52
+ ```
53
+ require('unit_manager')
54
+ ```
55
+
56
+ ### 5. unit_manager をプログラム内で利用可能にする
57
+
58
+ UnitManager::InitializeUnit.new を利用することで、yml ファイルに定義した単位が読み込まれます。
59
+ UnitManager::InitializeUnit クラスのアクセサメソッドである units に単位情報が入っています。
60
+
61
+ ```
62
+ units = UnitManager::InitializeUnit.new.units
63
+ ```
64
+
65
+ ### 6. 単位を取得する
66
+
67
+ unit.yml に記述されているキー名が、units のキー名になっています。このキー名によって取得する単位を切り替えます。
68
+ units['指定のキー名']の中に unit クラスが存在し、unit メソッドによって単位が呼び出されます。このメソッドの引数に値を渡す事によって equation で指定した計算式に従って単位も含めて値を出力します。
69
+
70
+ ```
71
+ units['money'].unit(1000)
72
+ => 1100円
73
+ ```
data/exe/unit.rb ADDED
@@ -0,0 +1,32 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ GEM_INSTALL_PATH = __dir__.slice!(0, __dir__.size-4)
4
+
5
+ def confirmation
6
+ unit_config_path_file = File.open("#{GEM_INSTALL_PATH}/config/unit_config_path.txt", 'r')
7
+ unit_config_path = unit_config_path_file.read
8
+ unit_config_path_file.close
9
+ puts unit_config_path
10
+ end
11
+
12
+ def init
13
+ Dir.mkdir('unit_config') unless Dir.exist?('unit_config')
14
+ File.new('unit_config/unit.yml', 'w') unless File.exist?("unit_config/unit.yml")
15
+
16
+ File.open("#{GEM_INSTALL_PATH}/config/unit_config_path.txt", mode = 'w') do | f |
17
+ f.write("#{Dir.pwd}/unit_config/unit.yml")
18
+ end
19
+
20
+ puts '=== Initialize Exit ==='
21
+ puts '- unit_config/unit.yml'
22
+ end
23
+
24
+
25
+ case ARGV[0]
26
+ when 'init'
27
+ init
28
+ when 'confirmation'
29
+ confirmation
30
+ else
31
+ puts 'command does not exist'
32
+ end
@@ -0,0 +1,33 @@
1
+ module UnitManager
2
+ class InitializeUnit
3
+ require 'yaml'
4
+ require_relative 'unit'
5
+
6
+ attr_reader :units
7
+
8
+ def initialize
9
+ units_hash = open(unit_config_path, 'r') { |f| YAML.load(f) }
10
+
11
+ units = units_hash['units'].each_with_object({}) do |unit, units|
12
+ split_equation = unit['equation'].split
13
+ raise InvalidEquation, "too long equation invalid: #{unit['equation']}" if UnitManager::Unit::MAX_EQUATION_SIZE < split_equation.size
14
+ raise InvalidEquation, "<value> does not exist equation invalid: #{unit['equation']}" unless split_equation.include?('<value>')
15
+ raise InvalidEquation, "equationv contains invalid symbol: #{unit['equation']}" if split_equation.any? {|equation_symbol| UnitManager::Unit::INVALID_EQUATION_SYMBOL.include?(equation_symbol)}
16
+
17
+ units["#{unit['key']}"] = UnitManager::Unit.new(key: unit['key'], name: unit['name'], equation: unit['equation'])
18
+ end
19
+
20
+ @units = units
21
+ end
22
+
23
+ def unit_config_path
24
+ gem_install_path = __dir__.slice!(0, __dir__.size-17)
25
+
26
+ unit_config_path_file = File.open("#{gem_install_path}/config/unit_config_path.txt", 'r')
27
+ unit_config_path = unit_config_path_file.read
28
+ unit_config_path_file.close
29
+
30
+ unit_config_path
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module UnitManager
2
+ class Unit
3
+ require 'active_support'
4
+ require_relative '../util/calculation_helper'
5
+ include CalculationHelper
6
+
7
+ MAX_EQUATION_SIZE = 999
8
+ INVALID_EQUATION_SYMBOL = ["(", ")", "!", "&", "%"]
9
+
10
+ def initialize(key:, name:, equation:)
11
+ @key = key
12
+ @name = name
13
+ @equation = equation
14
+ end
15
+
16
+ def unit(value)
17
+ "#{derive_value(value)}#{@name}"
18
+ end
19
+
20
+ private
21
+
22
+ def derive_value(value)
23
+ split_equation = @equation.split
24
+
25
+ split_equation.map! { |array_value| array_value == "<value>" ? value.to_s : array_value }
26
+
27
+ calculation_equation(split_equation: split_equation)
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module UnitManager
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "unit_manager/version"
4
+ require_relative "unit_manager/unit"
5
+ require_relative "unit_manager/initialize_unit"
6
+
7
+ module UnitManager
8
+ class Error < StandardError; end
9
+ class InvalidEquation < Error; end
10
+ end
@@ -0,0 +1,62 @@
1
+ module CalculationHelper
2
+ def calculation_equation(split_equation:)
3
+
4
+ while split_equation.index('*').present? || split_equation.index('/').present? do
5
+ multiplication_index = split_equation.index('*').presence || UnitManager::Unit::MAX_EQUATION_SIZE
6
+ division_index = split_equation.index('/').presence || UnitManager::Unit::MAX_EQUATION_SIZE
7
+
8
+ if multiplication_index <= division_index
9
+ multiplication(split_equation: split_equation, index: multiplication_index)
10
+ next
11
+ end
12
+
13
+ if division_index < multiplication_index
14
+ division(split_equation: split_equation, index: division_index)
15
+ next
16
+ end
17
+ end
18
+
19
+ while split_equation.index('+').present? || split_equation.index('-').present? do
20
+ addition_index = split_equation.index('+').presence || UnitManager::Unit::MAX_EQUATION_SIZE
21
+ subtraction_index = split_equation.index('-').presence || UnitManager::Unit::MAX_EQUATION_SIZE
22
+
23
+ if addition_index <= subtraction_index
24
+ addition(split_equation: split_equation, index: addition_index)
25
+ next
26
+ end
27
+
28
+ if subtraction_index < addition_index
29
+ subtraction(split_equation: split_equation, index: subtraction_index)
30
+ next
31
+ end
32
+ end
33
+
34
+ result = split_equation[0]
35
+
36
+ result.to_f.round
37
+ end
38
+
39
+ def multiplication(split_equation:, index:)
40
+ result = split_equation[index-1].to_f * split_equation[index+1].to_f
41
+ split_equation.slice!(index, 2)
42
+ split_equation[index-1] = result.to_s
43
+ end
44
+
45
+ def division(split_equation:, index:)
46
+ result = split_equation[index-1].to_f / split_equation[index+1].to_f
47
+ split_equation.slice!(index, 2)
48
+ split_equation[index-1] = result.to_s
49
+ end
50
+
51
+ def addition(split_equation:, index:)
52
+ result = split_equation[index-1].to_f + split_equation[index+1].to_f
53
+ split_equation.slice!(index, 2)
54
+ split_equation[index-1] = result.to_s
55
+ end
56
+
57
+ def subtraction(split_equation:, index:)
58
+ result = split_equation[index-1].to_f - split_equation[index+1].to_f
59
+ split_equation.slice!(index, 2)
60
+ split_equation[index-1] = result.to_s
61
+ end
62
+ end
@@ -0,0 +1,4 @@
1
+ module UnitManager
2
+ VERSION: String
3
+ # See the writing guide of rbs: https://github.com/ruby/rbs#guides
4
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: unit_manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - NaoyukiKojima
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2023-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '13.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '13.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '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: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry-rails
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
+ description: Manage units easily.
70
+ email:
71
+ - programkoji213@gmail.com
72
+ executables:
73
+ - unit.rb
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".rspec"
78
+ - Gemfile
79
+ - README.md
80
+ - Rakefile
81
+ - config/unit_config_path.txt
82
+ - docment/jp_readme.md
83
+ - exe/unit.rb
84
+ - lib/unit_manager.rb
85
+ - lib/unit_manager/initialize_unit.rb
86
+ - lib/unit_manager/unit.rb
87
+ - lib/unit_manager/version.rb
88
+ - lib/util/calculation_helper.rb
89
+ - sig/unit_manager.rbs
90
+ homepage: https://github.com/KojimaNaoyuki/unit_manager
91
+ licenses: []
92
+ metadata:
93
+ homepage_uri: https://github.com/KojimaNaoyuki/unit_manager
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 2.6.0
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.4.10
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: A gem that manages units.
113
+ test_files: []