millionaire 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/Rakefile +9 -0
- data/lib/millionaire/column.rb +11 -0
- data/lib/millionaire/csv.rb +89 -0
- data/lib/millionaire/validations/csv_uniqness.rb +4 -0
- data/lib/millionaire/version.rb +3 -0
- data/lib/millionaire.rb +7 -0
- data/millionaire.gemspec +29 -0
- data/spec/millionaire/column_spec.rb +6 -0
- data/spec/millionaire/csv_spec.rb +133 -0
- data/spec/millionaire/version_spec.rb +7 -0
- data/spec/spec_helper.rb +9 -0
- metadata +118 -0
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2012 Hidekuni Kajita
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'millionaire'
|
3
|
+
require 'millionaire/column'
|
4
|
+
require 'millionaire/validations/csv_uniqness'
|
5
|
+
|
6
|
+
module Millionaire::Csv
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
include ActiveModel::Validations
|
9
|
+
|
10
|
+
included do
|
11
|
+
class_attribute :csv_data, :columns, :indexes
|
12
|
+
attr_accessor :line_no
|
13
|
+
|
14
|
+
self.columns = []
|
15
|
+
self.indexes = {}
|
16
|
+
end
|
17
|
+
|
18
|
+
def initialize(attr={})
|
19
|
+
attr.each {|k,v| send("#{k}=", v) }
|
20
|
+
end
|
21
|
+
|
22
|
+
module ClassMethods
|
23
|
+
def load(io)
|
24
|
+
self.csv_data = []
|
25
|
+
csv = ::CSV.new(io, headers: :first_row, return_headers: false)
|
26
|
+
csv.each_with_index do |row,ix|
|
27
|
+
self.csv_data << self.new(row.to_hash.slice(*self.column_names).merge(line_no: ix.succ))
|
28
|
+
end
|
29
|
+
indexing
|
30
|
+
end
|
31
|
+
|
32
|
+
def column(name, option={})
|
33
|
+
attr_accessor name
|
34
|
+
column = Millionaire::Column.new(name, option)
|
35
|
+
self.columns << column
|
36
|
+
option.each do |k,v|
|
37
|
+
case k
|
38
|
+
when :null; validates name, presence: v unless v
|
39
|
+
when :length; validates name, length: {maximum: v}
|
40
|
+
when :value; validates name, inclusion: {in: v}
|
41
|
+
when :constraint; validates name, v
|
42
|
+
when :index; index(name)
|
43
|
+
when :uniq
|
44
|
+
validates name, csv_uniqness: column.uniq
|
45
|
+
index(column.uniq)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
def column_names; self.columns.map(&:name); end
|
50
|
+
|
51
|
+
def index(*name)
|
52
|
+
name.each do |n|
|
53
|
+
self.indexes[Array.wrap n] = []
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def indexing
|
58
|
+
self.indexes.keys.each do |k|
|
59
|
+
index_data= self.csv_data.group_by do |v|
|
60
|
+
k.map{|a| v.send(a) }
|
61
|
+
end
|
62
|
+
self.indexes[k] = index_data
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def where(query)
|
67
|
+
if self.indexes.key? query.keys
|
68
|
+
self.indexes[query.keys][query.values]
|
69
|
+
else
|
70
|
+
group = self.csv_data.group_by do |r|
|
71
|
+
query.map{|k,v| r.send(k)}
|
72
|
+
end
|
73
|
+
|
74
|
+
if query.values.all? {|val| val.is_a? Array }
|
75
|
+
query.values.map {|val| val.map {|v| group[Array.wrap(v)] } }.flatten
|
76
|
+
else
|
77
|
+
group[query.values]
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def find(line_no)
|
83
|
+
csv_data[line_no.pred]
|
84
|
+
end
|
85
|
+
def all; self.csv_data; end
|
86
|
+
def first; self.csv_data.first; end
|
87
|
+
def last; self.csv_data.last; end
|
88
|
+
end
|
89
|
+
end
|
data/lib/millionaire.rb
ADDED
data/millionaire.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "millionaire/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "millionaire"
|
7
|
+
s.version = Millionaire::VERSION
|
8
|
+
s.authors = ["HIDEKUNI Kajita"]
|
9
|
+
s.email = ["hide.nba@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Simple use the CSV}
|
12
|
+
s.description = %q{You can do the CSV file by specifying the column to include a millionaire.}
|
13
|
+
|
14
|
+
s.rubyforge_project = "millionaire"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_development_dependency "rspec"
|
22
|
+
s.add_development_dependency "shoulda-matchers"
|
23
|
+
s.add_development_dependency "tapp"
|
24
|
+
|
25
|
+
s.add_dependency "activemodel"
|
26
|
+
s.add_dependency "activesupport"
|
27
|
+
|
28
|
+
s.required_ruby_version = '>= 1.9.2'
|
29
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
# -*- coding: utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
require 'millionaire/csv'
|
4
|
+
|
5
|
+
describe Millionaire::Csv do
|
6
|
+
class CsvRecord
|
7
|
+
include Millionaire::Csv
|
8
|
+
column :str, index: true
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.column' do
|
12
|
+
class Column
|
13
|
+
include Millionaire::Csv
|
14
|
+
column :index , index: true
|
15
|
+
column :presence, null: false, index: true
|
16
|
+
column :length, length: 20
|
17
|
+
column :inclution, value: %w(foo bar)
|
18
|
+
column :constraint, constraint: {format: {with: /\A[a-zA-Z]+\z/}}
|
19
|
+
column :int, integer: true, value: 100..200
|
20
|
+
column :uniq1, uniq: [:index]
|
21
|
+
end
|
22
|
+
|
23
|
+
subject { Column.new }
|
24
|
+
it { Column.column_names.each {|name| should respond_to name } }
|
25
|
+
|
26
|
+
it { Column.should have(7).columns }
|
27
|
+
|
28
|
+
context 'column validation' do
|
29
|
+
subject { Column.validators.index_by {|v| v.attributes.first } }
|
30
|
+
its([:presence]) { should be_kind_of ActiveModel::Validations::PresenceValidator }
|
31
|
+
its([:length]) { should be_kind_of ActiveModel::Validations::LengthValidator }
|
32
|
+
its([:inclution]) { should be_kind_of ActiveModel::Validations::InclusionValidator }
|
33
|
+
its([:constraint]) { should be_kind_of ActiveModel::Validations::FormatValidator }
|
34
|
+
its([:int]) { should be_kind_of ActiveModel::Validations::InclusionValidator }
|
35
|
+
its([:uniq1]) { should be_kind_of CsvUniqnessValidator }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
describe 'index' do
|
40
|
+
class Index
|
41
|
+
include Millionaire::Csv
|
42
|
+
column :index_a
|
43
|
+
column :index_b
|
44
|
+
index :index_a, :index_b
|
45
|
+
index [:index_a, :index_b]
|
46
|
+
end
|
47
|
+
|
48
|
+
subject { Index.indexes }
|
49
|
+
its(:keys) { should =~ [[:index_a], [:index_b], [:index_a, :index_b]] }
|
50
|
+
|
51
|
+
describe '.indexing' do
|
52
|
+
before do
|
53
|
+
Index.csv_data=[Index.new(index_a: 10, index_b: 20)]
|
54
|
+
Index.indexing
|
55
|
+
end
|
56
|
+
|
57
|
+
its([[:index_a]]) { should have(1).record }
|
58
|
+
its([[:index_b]]) { should have(1).record }
|
59
|
+
its([[:index_a, :index_b]]) { should have(1).record }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
describe 'finder' do
|
64
|
+
before do
|
65
|
+
CsvRecord.load StringIO.new %w(str alice bob chris).join("\n")
|
66
|
+
end
|
67
|
+
|
68
|
+
describe '.find' do
|
69
|
+
subject { CsvRecord.find line_no }
|
70
|
+
let(:line_no) { 2 }
|
71
|
+
|
72
|
+
its(:str) { should == 'bob' }
|
73
|
+
|
74
|
+
context 'recoed not found' do
|
75
|
+
let(:line_no) { 100 }
|
76
|
+
it { should be_nil }
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
describe '.all' do
|
81
|
+
subject { CsvRecord.all }
|
82
|
+
it { should have(3).recoed }
|
83
|
+
|
84
|
+
context 'load csv' do
|
85
|
+
subject { CsvRecord.all.first }
|
86
|
+
its(:line_no) { should == 1 }
|
87
|
+
its(:str) { should == 'alice' }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
describe '.first' do
|
92
|
+
subject { CsvRecord.first }
|
93
|
+
its(:str) { should == 'alice' }
|
94
|
+
end
|
95
|
+
|
96
|
+
describe '.last' do
|
97
|
+
subject { CsvRecord.last }
|
98
|
+
its(:str) { should == 'chris' }
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
describe '.where' do
|
103
|
+
class Where
|
104
|
+
include Millionaire::Csv
|
105
|
+
column :str_a
|
106
|
+
column :str_b
|
107
|
+
column :str_c
|
108
|
+
index [:str_b, :str_c]
|
109
|
+
end
|
110
|
+
|
111
|
+
before do
|
112
|
+
Where.load StringIO.new %w(str_a,str_b,str_c 1,2,3 2,1,3 3,1,2).join("\n")
|
113
|
+
end
|
114
|
+
|
115
|
+
context 'no index' do
|
116
|
+
subject { Where.where(str_a: '1', str_b: '2').first }
|
117
|
+
its(:line_no) { should == 1 }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'has index' do
|
121
|
+
subject { Where.where(str_b: '1', str_c: '2').first }
|
122
|
+
its(:line_no) { should == 3 }
|
123
|
+
end
|
124
|
+
|
125
|
+
describe 'operator' do
|
126
|
+
describe 'in' do
|
127
|
+
subject { Where.where(str_a: %w(1 3)) }
|
128
|
+
it { should have(2).record }
|
129
|
+
it { subject.map(&:line_no).should == [1,3] }
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,118 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: millionaire
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- HIDEKUNI Kajita
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-03-04 00:00:00.000000000Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70092811212760 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70092811212760
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: shoulda-matchers
|
27
|
+
requirement: &70092811212340 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70092811212340
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: tapp
|
38
|
+
requirement: &70092811211920 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70092811211920
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: activemodel
|
49
|
+
requirement: &70092811211500 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70092811211500
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: activesupport
|
60
|
+
requirement: &70092811211080 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :runtime
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70092811211080
|
69
|
+
description: You can do the CSV file by specifying the column to include a millionaire.
|
70
|
+
email:
|
71
|
+
- hide.nba@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE
|
79
|
+
- Rakefile
|
80
|
+
- lib/millionaire.rb
|
81
|
+
- lib/millionaire/column.rb
|
82
|
+
- lib/millionaire/csv.rb
|
83
|
+
- lib/millionaire/validations/csv_uniqness.rb
|
84
|
+
- lib/millionaire/version.rb
|
85
|
+
- millionaire.gemspec
|
86
|
+
- spec/millionaire/column_spec.rb
|
87
|
+
- spec/millionaire/csv_spec.rb
|
88
|
+
- spec/millionaire/version_spec.rb
|
89
|
+
- spec/spec_helper.rb
|
90
|
+
homepage: ''
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: 1.9.2
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project: millionaire
|
110
|
+
rubygems_version: 1.8.6
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Simple use the CSV
|
114
|
+
test_files:
|
115
|
+
- spec/millionaire/column_spec.rb
|
116
|
+
- spec/millionaire/csv_spec.rb
|
117
|
+
- spec/millionaire/version_spec.rb
|
118
|
+
- spec/spec_helper.rb
|