Skip to content

Commit

Permalink
とりあえず値段まで表示されるように対応
Browse files Browse the repository at this point in the history
  • Loading branch information
ex-efourtion-ichikawa committed Dec 16, 2015
1 parent f61ed24 commit e94b5d6
Show file tree
Hide file tree
Showing 8 changed files with 204 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Expand Up @@ -5,6 +5,8 @@

間違ってもこのアプリを報酬で使わないようにしてください。

作成中です・・・

## 概要
このプログラムはRailsプロジェクトのステップ数やコードの書き方によって適当に値段をつけていきます。
値段は``のみの対応となります。
Expand All @@ -24,3 +26,5 @@ $ ruby how_much.rb rails_root_path [output_folder]
$ ruby how_much.rb -h/--help
```

## 製作日数
2日予定
25 changes: 25 additions & 0 deletions config/app.yml
Expand Up @@ -12,4 +12,29 @@ app:
- vendor
- .idea

# デバッグフラグ
debug: false

# 解析するプログラム単位での設定
program:
# 基本設定
base:
# 一行毎に発生する金額(円)
line:
# 通常行の値段
line_price: 500
# 制御処理行の値段
proc_line_price: 1000
# コメント行の値段
comment_line_price: 100

# メソッドに発生する値段(円)
method:
price: 2000
# メソッド行数が長い場合に発生するペナルティ(コメントは含まず)
max_line: 30
# メソッド行数が長い場合に引かれる金額(通常の値段は別途入ります。)
max_line_over_price: -500

# Ruby設定(baseと同じ)
ruby:
32 changes: 30 additions & 2 deletions controllers/dispatch.rb
Expand Up @@ -13,13 +13,41 @@ def run

# 全てのファイルを読み込みます。
read_all_file do |rails_path, file_name|
# 対象ファイルに対して値段をつけます。
puts "#{rails_path}:#{file_name}"
# パーサーモデルを取得します。
model = get_parser_model(rails_path, file_name)
if model
Logger.debug "[Start] #{file_name}"

# インスタンスを生成して実行します。
instance = model.new("#{@rails_path}#{rails_path}/#{file_name}")
instance.price_calculation
price += instance.price

# TODO 出力ページの作成

Logger.debug "[End] #{file_name}"
else
Logger.debug "[Skip] #{file_name}"
end
end

Logger.info "Your Rails app will be #{price} yen"
end

private

# 使用するパーサーモデルを取得
def get_parser_model(rails_path, file_name)
# 指定のモデルを取得します。
if File::extname(file_name) == ".rb"
# Rubyのパーサーを返します。
return RubyProgram
end

# 使用するモデルなし。
nil
end

# 再帰的に全てのフォルダを読み込みます。
def read_all_file(current_path = "", &block)
# ディレクトリを読み込みます。
Expand Down
12 changes: 6 additions & 6 deletions how_much.rb
@@ -1,8 +1,5 @@
#!/usr/bin/ruby

# 起動確認用に表示
puts "Welcome to joke app!"

# 必要なファイルを読み込みます。
# $:.unshift File.dirname(__FILE__)
require_relative 'lib/load_file'
Expand All @@ -11,6 +8,9 @@
LoadFile.path 'models'
LoadFile.path 'lib'

# 起動確認用に表示
Logger.info "Welcome to joke app!"

# ヘルプの表示
if !ARGV[0] || ARGV[0] == "-h" || ARGV[0] == "--help"
# ヘルプの表示
Expand All @@ -26,19 +26,19 @@

# 対象のフォルダが存在するかチェック
unless Validate.dir? rails_path
puts "rails project path is not found. => #{rails_path}"
Logger.info "rails project path is not found. => #{rails_path}"
exit 1
end

# 出力フォルダが存在するかチェック
unless Validate.dir? output_path
puts "output path is not found. => #{output_path}"
Logger.info "output path is not found. => #{output_path}"
exit 1
end

# Railsプロジェクトであるかチェック
unless Validate.rails_project? rails_path
puts "`#{rails_path}` path is not rails project."
Logger.info "`#{rails_path}` path is not rails project."
exit 1
end

Expand Down
6 changes: 5 additions & 1 deletion lib/app_config.rb
Expand Up @@ -8,8 +8,12 @@ def self.get(*keys)
yml = AppConfig.yml["app"]
keys.each { |key|
yml = yml[key.to_s]
return nil if yml.nil?
}
# 中身を書き換えられないようにコピーを渡す。

# 中身を書き換えられないようにコピーを渡す。(コピーできないオブジェクトは除外)
return yml if yml.nil? || yml.class == TrueClass ||
yml.class == FalseClass || yml.class == Numeric || yml.class == Symbol
return yml.dup
end

Expand Down
12 changes: 12 additions & 0 deletions lib/logger.rb
@@ -0,0 +1,12 @@
# 簡易ログクラス
class Logger
# 通常メッセージ
def self.info(msg)
puts msg
end

# デバッグ時のメッセージ
def self.debug(msg)
puts msg if AppConfig.get(:base, :debug)
end
end
108 changes: 108 additions & 0 deletions models/program.rb
@@ -0,0 +1,108 @@
# このクラスを基底にしてプログラムの値段を算出します。
class Program
# 値段
attr_accessor :price

# 使用する設定ファイルの定義
def self.config_file(symbol)
define_method(:config_file) {
@config ||= AppConfig.get(:program, symbol)
}
end
config_file :base

# ファイルパスを起点に処理を行います。
def initialize(file_path)
# 基底の設定ファイルを読み込み
@base_config = AppConfig.get(:program, :base)
# ファイルパスを保持
@file_path = file_path
end

# 値段を算出します。
def price_calculation
# 値段の初期化
self.price = 0

# ファイルを読み込んで値段を算出する。
open(@file_path) { |file|
# 行単位で処理を行う。
file.each_line do |line|
# 前後の空白を削除
l = line.strip
# 空行は無視
next if l.length == 0

if comment_line?(l)
# コメントの値段
self.price = comment_line_price
else
if proc_line?(l)
# 制御構文の値段
self.price = proc_line_price
else
# 通常行の値段
self.price = line_price
end
end
end
}
end

private

# 追加で行いたい処理
def price_calculation_ext(line)
# 必要に応じて継承先で実装する。
end

#
# 制御処理
#

# 制御構文であるか?
def proc_line?(line)
(/^.*(if\s|for\s|while\s).*$/ =~ line) != nil
end

# コメント行であるか?
def comment_line?(line)
line.index("#") == 0
end

#
# 値段
#

# 制御構文の値段
def proc_line_price
@proc_line_price ||= config(:line, :proc_line_price)
end

# 通常のラインの値段
def line_price
@line_price ||= config(:line, :line_price)
end

# コメントの値段
def comment_line_price
@comment_line_price ||= config(:line, :comment_line_price)
end

# 使用する設定内容を取得ます。
def config(*keys)
# 使用する設定を取得
config = config_file
base_config = @base_config

# 設定がない場合はbaseを引き継ぐ
config = base_config if config.nil?
keys.each { |key|
config = config[key.to_s]
base_config = base_config[key.to_s]
config = base_config if config.nil?
}

config
end
end
14 changes: 14 additions & 0 deletions models/ruby_program.rb
@@ -0,0 +1,14 @@
# Rubyプログラムを解析します。
class RubyProgram < Program

# rubyの設定を読み込む
config_file :ruby

private

# 制御構文であるか?
def proc_line?(line)
(/^.*(if\s|for\s|while\s|\.each|\.times).*$/ =~ line) != nil
end

end

0 comments on commit e94b5d6

Please sign in to comment.