line_head_washer 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.coveralls.yml ADDED
@@ -0,0 +1 @@
1
+ service_name: travis-ci
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/.rubocop.yml ADDED
@@ -0,0 +1,6 @@
1
+ inherit_from: rubocop-todo.yml
2
+
3
+ LineLength:
4
+ Enabled: false
5
+ Semicolon:
6
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ before_install:
3
+ - gem update --system 2.1.11
4
+ - gem --version
5
+ rvm:
6
+ - 1.9.3
7
+ - 2.0.0
8
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+ gem "rspec", "~> 2.14.1"
5
+ gem "thor", "~> 0.18.1"
6
+ gem "simplecov", "~> 0.8.2"
7
+ gem "activesupport", "~> 4.0.1"
8
+ gem "activemodel", "~> 4.0.2"
9
+ group :test do
10
+ gem 'coveralls', require: false
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 tbpgr
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # LineHeadWasher
2
+
3
+ [![Build Status](https://travis-ci.org/tbpgr/line_head_washer.png?branch=master)](https://travis-ci.org/tbpgr/line_head_washer)
4
+ [![Coverage Status](https://coveralls.io/repos/tbpgr/line_head_washer/badge.png)](https://coveralls.io/r/tbpgr/line_head_washer)
5
+
6
+ LineHeadWasher remove start of target pattern charactors.
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ gem 'line_head_washer'
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install line_head_washer
21
+
22
+ ## Usage
23
+ ### generate Washfile
24
+ ~~~bash
25
+ lhwasher init
26
+ ~~~
27
+
28
+ ~~~ruby
29
+ # encoding: utf-8
30
+
31
+ # input file or directory by regexp
32
+ # input is required
33
+ # input allow only String
34
+ # input's default value => "./*.txt"
35
+ input "./*.txt"
36
+
37
+ # output directory
38
+ # output is required
39
+ # output allow only String
40
+ # output's default value => "./"
41
+ output "./"
42
+
43
+ # remove_regexp
44
+ # remove_regexp is required
45
+ # remove_regexp allow only String
46
+ # remove_regexp's default value => "[ | ]"
47
+ remove_regexp "[ | ]"
48
+ ~~~
49
+
50
+ ### edit Washfile
51
+ ~~~ruby
52
+ # encoding: utf-8
53
+
54
+ input "./inputs/*.txt"
55
+ output "./outputs"
56
+ remove_regexp "[ | ]*"
57
+ ~~~
58
+
59
+ ### prepare test input files
60
+ ~~~bash
61
+ $ mkdir inputs
62
+ $ cat <<EOS>test1.txt
63
+ hoge
64
+ hige
65
+ hege
66
+ EOS
67
+ $ cat <<EOS>test2.txt
68
+ hoge
69
+ hige
70
+ hege
71
+ EOS
72
+ ~~~
73
+
74
+ ### execute wash files
75
+ ~~~bash
76
+ lhwasher e
77
+ ~~~
78
+
79
+ ### check results
80
+ ~~~bash
81
+ $ cat outputs/test1.txt
82
+
83
+ $ cat outputs/test2.txt
84
+
85
+ ~~~
86
+
87
+ ## History
88
+ version 0.0.1 : first release.
89
+
90
+ ## Contributing
91
+
92
+ 1. Fork it
93
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
94
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
95
+ 4. Push to the branch (`git push origin my-new-feature`)
96
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+ task :default => [:spec]
5
+
6
+ RSpec::Core::RakeTask.new(:spec) do |spec|
7
+ spec.pattern = 'spec/**/*_spec.rb'
8
+ end
data/bin/lhwasher ADDED
@@ -0,0 +1,31 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ require 'line_head_washer_core'
5
+ require 'line_head_washer/version'
6
+ require 'thor'
7
+
8
+ module LineHeadWasher
9
+ # = LineHeadWasher CLI
10
+ class CLI < Thor
11
+ class_option :help, type: :boolean, aliases: '-h', desc: 'help message.'
12
+ class_option :version, type: :boolean, desc: 'version'
13
+
14
+ desc 'execute', 'TODO: write your desc'
15
+ def execute
16
+ LineHeadWasher::Core.new.execute
17
+ end
18
+
19
+ desc 'init', 'generate Lineheadwasherfile'
20
+ def init
21
+ LineHeadWasher::Core.new.init
22
+ end
23
+
24
+ desc 'version', 'version'
25
+ def version
26
+ p LineHeadWasher::VERSION
27
+ end
28
+ end
29
+ end
30
+
31
+ LineHeadWasher::CLI.start(ARGV)
@@ -0,0 +1,3 @@
1
+ module LineHeadWasher
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,80 @@
1
+ # encoding: utf-8
2
+ require 'line_head_washer_dsl'
3
+
4
+ module LineHeadWasher
5
+ # LineHeadWasher Core
6
+ class Core
7
+ WASHER_FILE = 'Washerfile'
8
+ WASHER_TEMPLATE = <<-EOS
9
+ # encoding: utf-8
10
+
11
+ # input file or directory by regexp
12
+ # input is required
13
+ # input allow only String
14
+ # input's default value => "./*.txt"
15
+ input "./*.txt"
16
+
17
+ # output directory
18
+ # output is required
19
+ # output allow only String
20
+ # output's default value => "./"
21
+ output "./"
22
+
23
+ # remove_regexp
24
+ # remove_regexp is required
25
+ # remove_regexp allow only String
26
+ # remove_regexp's default value => "[ | ]"
27
+ remove_regexp "[ | ]"
28
+ EOS
29
+
30
+ # generate Washerfile to current directory.
31
+ def init
32
+ File.open(WASHER_FILE, 'w') { |f|f.puts WASHER_TEMPLATE }
33
+ end
34
+
35
+ # remove line head specific charactors
36
+ def execute
37
+ dsl = get_dsl
38
+ input, remove_regexp, output = get_in_out_removes(dsl)
39
+ create_output_dir(output)
40
+ Dir.glob(input).each do |file|
41
+ input_src = get_input(file)
42
+ replaced = remove_head(input_src, remove_regexp)
43
+ output_file(file, replaced, output)
44
+ end
45
+ end
46
+
47
+ private
48
+ def get_dsl
49
+ src = read_dsl
50
+ dsl = LineHeadWasher::Dsl.new
51
+ dsl.instance_eval src
52
+ dsl
53
+ end
54
+
55
+ def get_in_out_removes(dsl)
56
+ [dsl.line_head_washer.input, dsl.line_head_washer.remove_regexp, dsl.line_head_washer.output]
57
+ end
58
+
59
+ def create_output_dir(output)
60
+ Dir.mkdir(output) unless Dir.exists? output
61
+ end
62
+
63
+ def get_input(file)
64
+ File.read(file)
65
+ end
66
+
67
+ def output_file(file, replaced, output)
68
+ filename = File.basename(file)
69
+ File.open("#{output}/#{filename}", 'w:UTF-8') { |f|f.print replaced }
70
+ end
71
+
72
+ def remove_head(input_src, remove_regexp)
73
+ input_src.gsub /^#{remove_regexp}/, ''
74
+ end
75
+
76
+ def read_dsl
77
+ File.open(WASHER_FILE) { |f|f.read }
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,29 @@
1
+ # encoding: utf-8
2
+ require 'line_head_washer_dsl_model'
3
+
4
+ module LineHeadWasher
5
+ class Dsl
6
+ attr_accessor :line_head_washer
7
+
8
+ # String Define
9
+ [:input, :output, :remove_regexp].each do |f|
10
+ define_method f do |value|
11
+ eval "@line_head_washer.#{f.to_s} = '#{value}'", binding
12
+ end
13
+ end
14
+
15
+ # Array/Hash/Boolean Define
16
+ [].each do |f|
17
+ define_method f do |value|
18
+ eval "@line_head_washer.#{f.to_s} = #{value}", binding
19
+ end
20
+ end
21
+
22
+ def initialize
23
+ @line_head_washer = LineHeadWasher::DslModel.new
24
+ @line_head_washer.input = './*.txt'
25
+ @line_head_washer.output = './*.txt'
26
+ @line_head_washer.remove_regexp = '[ | ]'
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+ require 'active_model'
3
+
4
+ module LineHeadWasher
5
+ class DslModel
6
+ include ActiveModel::Model
7
+
8
+ # input file or directory by regexp
9
+ attr_accessor :input
10
+ validates :input, presence: true
11
+
12
+ # output file or directory by regexp
13
+ attr_accessor :output
14
+ validates :output, presence: true
15
+
16
+ # remove_regexp
17
+ attr_accessor :remove_regexp
18
+ validates :remove_regexp, presence: true
19
+
20
+ end
21
+ end
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'line_head_washer/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "line_head_washer"
8
+ spec.version = LineHeadWasher::VERSION
9
+ spec.authors = ["tbpgr"]
10
+ spec.email = ["tbpgr@tbpgr.jp"]
11
+ spec.description = %q{LineHeadWasher remove start of target pattern charactors.}
12
+ spec.summary = %q{LineHeadWasher remove start of target pattern charactors.}
13
+ spec.homepage = "https://github.com/tbpgr/line_head_washer"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "activesupport", "~> 4.0.1"
22
+ spec.add_runtime_dependency "activemodel", "~> 4.0.2"
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_development_dependency "rspec", "~> 2.14.1"
27
+ spec.add_development_dependency "simplecov", "~> 0.8.2"
28
+ spec.add_development_dependency 'coveralls'
29
+ end
data/rubocop-todo.yml ADDED
@@ -0,0 +1,36 @@
1
+ # This configuration was generated by `rubocop --auto-gen-config`.
2
+ # The point is for the user to remove these configuration records
3
+ # one by one as the offences are removed from the code base.
4
+
5
+ CyclomaticComplexity:
6
+ Enabled: false
7
+
8
+ Documentation:
9
+ Enabled: false
10
+
11
+ EmptyLinesAroundBody:
12
+ Enabled: false
13
+
14
+ Encoding:
15
+ Enabled: false
16
+
17
+ Eval:
18
+ Enabled: false
19
+
20
+ LineLength:
21
+ Enabled: false
22
+
23
+ MethodLength:
24
+ Enabled: false
25
+
26
+ NestedTernaryOperator:
27
+ Enabled: false
28
+
29
+ ParameterLists:
30
+ Enabled: false
31
+
32
+ ReduceArguments:
33
+ Enabled: false
34
+
35
+ UselessAssignment:
36
+ Enabled: false
@@ -0,0 +1,86 @@
1
+ # encoding: utf-8
2
+ require 'spec_helper'
3
+ require 'line_head_washer_core'
4
+
5
+ describe LineHeadWasher::Core do
6
+
7
+ context :execute do
8
+ TMP_WASHER = 'tmp_washer'
9
+ WASHER_CASE1 = <<-EOS
10
+ # encoding: utf-8
11
+ input "./input/*.txt"
12
+ output "output"
13
+ remove_regexp "[ |\t| ]*"
14
+ EOS
15
+ CONTENTS1 = <<-EOS
16
+ hoge
17
+ hige
18
+   huge
19
+ hege
20
+ EOS
21
+ CONTENTS2 = <<-EOS
22
+ hoge
23
+ hige
24
+ huge  
25
+ hege
26
+ EOS
27
+ EXPECTED1 = <<-EOS
28
+ hoge
29
+ hige
30
+ huge
31
+ hege
32
+ EOS
33
+ EXPECTED2 = <<-EOS
34
+ hoge
35
+ hige
36
+ huge  
37
+ hege
38
+ EOS
39
+ cases = [
40
+ {
41
+ case_no: 1,
42
+ case_title: 'valid case',
43
+ washerfile: WASHER_CASE1,
44
+ input_contents: [CONTENTS1, CONTENTS2],
45
+ input_file_names: ['./input/test1.txt', './input/test2.txt'],
46
+ output_file_names: ['./output/test1.txt', './output/test2.txt'],
47
+ expected_contents: [EXPECTED1, EXPECTED2],
48
+ },
49
+ ]
50
+
51
+ cases.each do |c|
52
+ it "|case_no=#{c[:case_no]}|case_title=#{c[:case_title]}" do
53
+ begin
54
+ case_before c
55
+
56
+ # -- given --
57
+ line_head_washer_core = LineHeadWasher::Core.new
58
+
59
+ # -- when --
60
+ line_head_washer_core.execute
61
+
62
+ # -- then --
63
+ c[:output_file_names].each_with_index { |v, i|expect(File.read(v)).to eq(c[:expected_contents][i]) }
64
+ ensure
65
+ case_after c
66
+ end
67
+ end
68
+
69
+ def case_before(c)
70
+ Dir.mkdir(TMP_WASHER) unless File.exists?(TMP_WASHER)
71
+ Dir.chdir(TMP_WASHER)
72
+ Dir.mkdir('input')
73
+ File.open(LineHeadWasher::Core::WASHER_FILE, 'w:UTF-8') { |f|f.print c[:washerfile] }
74
+ c[:input_file_names].each_with_index do |v, i|
75
+ File.open(v, 'w:UTF-8') { |f|f.print c[:input_contents][i] }
76
+ end
77
+ end
78
+
79
+ def case_after(c)
80
+ Dir.chdir('../')
81
+ return unless File.exists? TMP_WASHER
82
+ FileUtils.rm_rf("./#{TMP_WASHER}")
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,15 @@
1
+ require 'simplecov'
2
+ require 'coveralls'
3
+ Coveralls.wear!
4
+ SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
5
+ SimpleCov::Formatter::HTMLFormatter,
6
+ Coveralls::SimpleCov::Formatter
7
+ ]
8
+ SimpleCov.start do
9
+ add_filter '/spec/'
10
+ end
11
+ RSpec.configure do |config|
12
+ config.treat_symbols_as_metadata_keys_with_true_values = true
13
+ config.run_all_when_everything_filtered = true
14
+ config.filter_run :focus
15
+ end
metadata ADDED
@@ -0,0 +1,145 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: line_head_washer
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - tbpgr
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: &23007840 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: 4.0.1
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *23007840
25
+ - !ruby/object:Gem::Dependency
26
+ name: activemodel
27
+ requirement: &23007540 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ~>
31
+ - !ruby/object:Gem::Version
32
+ version: 4.0.2
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *23007540
36
+ - !ruby/object:Gem::Dependency
37
+ name: bundler
38
+ requirement: &23007264 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.3'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *23007264
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: &23007036 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ! '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ type: :development
56
+ prerelease: false
57
+ version_requirements: *23007036
58
+ - !ruby/object:Gem::Dependency
59
+ name: rspec
60
+ requirement: &23006712 !ruby/object:Gem::Requirement
61
+ none: false
62
+ requirements:
63
+ - - ~>
64
+ - !ruby/object:Gem::Version
65
+ version: 2.14.1
66
+ type: :development
67
+ prerelease: false
68
+ version_requirements: *23006712
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: &23006412 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ version: 0.8.2
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: *23006412
80
+ - !ruby/object:Gem::Dependency
81
+ name: coveralls
82
+ requirement: &23006184 !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ type: :development
89
+ prerelease: false
90
+ version_requirements: *23006184
91
+ description: LineHeadWasher remove start of target pattern charactors.
92
+ email:
93
+ - tbpgr@tbpgr.jp
94
+ executables:
95
+ - lhwasher
96
+ extensions: []
97
+ extra_rdoc_files: []
98
+ files:
99
+ - .coveralls.yml
100
+ - .gitignore
101
+ - .rspec
102
+ - .rubocop.yml
103
+ - .travis.yml
104
+ - Gemfile
105
+ - LICENSE.txt
106
+ - README.md
107
+ - Rakefile
108
+ - bin/lhwasher
109
+ - lib/line_head_washer/version.rb
110
+ - lib/line_head_washer_core.rb
111
+ - lib/line_head_washer_dsl.rb
112
+ - lib/line_head_washer_dsl_model.rb
113
+ - line_head_washer.gemspec
114
+ - rubocop-todo.yml
115
+ - spec/line_head_washer_core_spec.rb
116
+ - spec/spec_helper.rb
117
+ homepage: https://github.com/tbpgr/line_head_washer
118
+ licenses:
119
+ - MIT
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ none: false
126
+ requirements:
127
+ - - ! '>='
128
+ - !ruby/object:Gem::Version
129
+ version: '0'
130
+ required_rubygems_version: !ruby/object:Gem::Requirement
131
+ none: false
132
+ requirements:
133
+ - - ! '>='
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ requirements: []
137
+ rubyforge_project:
138
+ rubygems_version: 1.8.11
139
+ signing_key:
140
+ specification_version: 3
141
+ summary: LineHeadWasher remove start of target pattern charactors.
142
+ test_files:
143
+ - spec/line_head_washer_core_spec.rb
144
+ - spec/spec_helper.rb
145
+ has_rdoc: