condition 0.0.1

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
+ SHA1:
3
+ metadata.gz: b3db8b51554104a64aa91a45869f59070dbba10e
4
+ data.tar.gz: 317311cccf4ae7232a441d1545eb01caa890024d
5
+ SHA512:
6
+ metadata.gz: 9223a6577b9db8f6846699a0e415bde33bedfc271f428a52fe60deb24e2b20df80910b505bcf5caf4484b1a62760904fb51ed5bb97ebce59dff7eb57bdbe9764
7
+ data.tar.gz: 94652a228b56e34a4ee07bcb81d4b251ee385651ac9f02ae68fb44b28557ab4f0381e98e029029bd8825dfd843d73ffc27a0785a09b43698ccf2f5ae390381bf
data/.gitignore ADDED
@@ -0,0 +1,19 @@
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
18
+ vendor
19
+ .~lock.*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in condition.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 aoyagikouhei
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,29 @@
1
+ # Condition
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'condition'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install condition
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec) do |t|
5
+ t.rspec_opts = ["-c", "-fs"]
6
+ end
7
+ task default: :spec
data/condition.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'condition'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "condition"
8
+ spec.version = Condition::VERSION
9
+ spec.authors = ["aoyagikouhei"]
10
+ spec.email = ["aoyagi.kouhei@gmail.com"]
11
+ spec.description = %q{Condition Test}
12
+ spec.summary = %q{Condition Test}
13
+ spec.homepage = "https://github.com/aoyagikouhei/condition"
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_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+ spec.add_development_dependency "pg"
25
+ spec.add_dependency "roo"
26
+ spec.add_dependency "sequel"
27
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+
3
+ module Condition
4
+ class Post
5
+ include Condition::Reader;
6
+
7
+ def initialize(path, sheet_index=0)
8
+ tables = read_sheet(path, sheet_index)
9
+ @tables = []
10
+ tables.each do |rows|
11
+ @tables << Condition::Table.new(rows)
12
+ end
13
+ end
14
+
15
+ def exec(db)
16
+ @tables.each do |table|
17
+ list = table.all(db)
18
+ list.each do |line|
19
+ table.check_line(line)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+
3
+ module Condition
4
+ class Pre
5
+ include Condition::Reader;
6
+
7
+ def initialize(path, sheet_index=0)
8
+ tables = read_sheet(path, sheet_index)
9
+ @tables = []
10
+ tables.each do |rows|
11
+ @tables << Condition::Table.new(rows)
12
+ end
13
+ end
14
+
15
+ def exec(db)
16
+ @tables.each do |table|
17
+ table.delete(db)
18
+ table.insert(db)
19
+ end
20
+ end
21
+
22
+ end
23
+ end
@@ -0,0 +1,36 @@
1
+ # coding: utf-8
2
+ require 'roo'
3
+
4
+ module Condition
5
+ module Reader
6
+ def read_sheet(path, sheet_index)
7
+ ss = Roo::Spreadsheet.open(path)
8
+ ss.default_sheet = ss.sheets[sheet_index]
9
+ row_index = 1
10
+ res = []
11
+ while true
12
+ break if nil == ss.cell(row_index, 1)
13
+ table = []
14
+ while true
15
+ row = read_row(ss, row_index)
16
+ row_index += 1
17
+ break if 0 == row.size
18
+ table << row
19
+ end
20
+ res << table
21
+ end
22
+ res
23
+ end
24
+
25
+ def read_row(ss, row_index)
26
+ column_index = 1
27
+ result = []
28
+ while true
29
+ value = ss.cell(row_index, column_index)
30
+ return result if nil == value
31
+ result << value
32
+ column_index += 1
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,78 @@
1
+ # coding: utf-8
2
+ require 'time'
3
+
4
+ module Condition
5
+ class Table
6
+ def initialize(rows)
7
+ @table_name = rows[0][0]
8
+ @keys = rows[0].size > 1 ? rows[0][1..rows.size - 1] : []
9
+ body = rows[1..rows.size - 1]
10
+ @values = []
11
+ @params = {}
12
+ body.each do |row|
13
+ index = 0
14
+ name = row[0]
15
+ @params[name.to_sym] = ("$" + name).to_sym
16
+ items = row[1..row.size - 1]
17
+ value = nil
18
+ items.each do |item|
19
+ value = @values[index] ? @values[index] : {}
20
+ value[name.to_sym] = '#NULL' == item ? nil : item
21
+ @values[index] = value
22
+ index += 1
23
+ end
24
+ end
25
+ end
26
+
27
+ def all(db)
28
+ db["SELECT * FROM #{@table_name}"].all
29
+ end
30
+
31
+ def delete(db)
32
+ db["DELETE FROM #{@table_name}"].delete
33
+ end
34
+
35
+ def insert(db)
36
+ ds = db[@table_name.to_sym].prepare(:insert, :insert_with_name, @params)
37
+ @values.each do |it|
38
+ ds.call(it)
39
+ end
40
+ end
41
+
42
+ def value_match?(expected, real)
43
+ if Time === real
44
+ real == Time.parse(expected)
45
+ elsif nil == real
46
+ expected == nil
47
+ else
48
+ real.to_s == expected
49
+ end
50
+ end
51
+
52
+ def check_value(real, value)
53
+ targetFlag = true
54
+ matchFlag = true
55
+ value.each_pair do |k, v|
56
+ match = value_match?(v, real[k])
57
+ whereKeyFlag = nil != @keys.index(k.to_s)
58
+ matchFlag = false if !match
59
+ targetFlag = false if whereKeyFlag && !match
60
+ end
61
+ if targetFlag && matchFlag
62
+ return true
63
+ elsif !targetFlag
64
+ return false
65
+ else
66
+ raise "#{@table_name} not match " + real.to_s
67
+ end
68
+ end
69
+
70
+ def check_line(real)
71
+ @values.each do |value|
72
+ return if check_value(real, value)
73
+ end
74
+ raise "#{@table_name} not found " + real.to_s
75
+ end
76
+
77
+ end
78
+ end
@@ -0,0 +1,5 @@
1
+ # coding: utf-8
2
+
3
+ module Condition
4
+ VERSION = "0.0.1"
5
+ end
data/lib/condition.rb ADDED
@@ -0,0 +1,7 @@
1
+ # coding: utf-8
2
+
3
+ require "condition/version"
4
+ require "condition/reader"
5
+ require "condition/table"
6
+ require "condition/pre"
7
+ require "condition/post"
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+ describe Condition do
3
+ before(:all) do
4
+ DB << "CREATE TABLE IF NOT EXISTS t_user(user_id BIGINT, user_name TEXT)"
5
+ DB << "CREATE TABLE IF NOT EXISTS t_test(id BIGINT, name TEXT, flag BOOLEAN, ts TIMESTAMPTZ, iary BIGINT[], tary TEXT[])"
6
+ end
7
+
8
+ it 'pre and post' do
9
+ pre = Condition::Pre.new(FILES + '/t_user.ods')
10
+ pre.exec(DB)
11
+ post = Condition::Post.new(FILES + '/t_user.ods', 1)
12
+ post.exec(DB)
13
+ end
14
+ end
Binary file
@@ -0,0 +1,12 @@
1
+ # coding: utf-8
2
+
3
+ require 'bundler/setup'
4
+ require 'condition'
5
+ require 'sequel'
6
+
7
+ FILES = File.dirname(__FILE__) + "/files"
8
+ DB = Sequel.connect('postgres://localhost:5432/test?user=aoyagikouhei')
9
+
10
+ RSpec.configure do |config|
11
+ # some (optional) config here
12
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: condition
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - aoyagikouhei
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-09 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.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: pg
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
+ - !ruby/object:Gem::Dependency
70
+ name: roo
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: sequel
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Condition Test
98
+ email:
99
+ - aoyagi.kouhei@gmail.com
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - condition.gemspec
110
+ - lib/condition.rb
111
+ - lib/condition/post.rb
112
+ - lib/condition/pre.rb
113
+ - lib/condition/reader.rb
114
+ - lib/condition/table.rb
115
+ - lib/condition/version.rb
116
+ - spec/condition_spec.rb
117
+ - spec/files/t_user.ods
118
+ - spec/spec_helper.rb
119
+ homepage: https://github.com/aoyagikouhei/condition
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - '>='
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.0.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: Condition Test
143
+ test_files:
144
+ - spec/condition_spec.rb
145
+ - spec/files/t_user.ods
146
+ - spec/spec_helper.rb