Skip to content

Commit

Permalink
line_noでfindできるようにした
Browse files Browse the repository at this point in the history
  • Loading branch information
hidenba committed Feb 24, 2012
1 parent 188f241 commit 60c28f9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 24 deletions.
7 changes: 5 additions & 2 deletions lib/millionaire/csv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def column(name, option={})
when :value; validates name, inclusion: {in: v}
when :constraint; validates name, v
when :index; index(name)
when :uniq;
when :uniq
validates name, csv_uniqness: column.uniq
index(column.uniq)
end
Expand All @@ -55,7 +55,6 @@ def indexing

def indexes; self.indexes; end


def load(io)
self.csv_data = []
csv = ::CSV.new(io, headers: :first_row, return_headers: false)
Expand All @@ -76,6 +75,10 @@ def where(query)
end
end

def find(line_no)
csv_data[line_no.pred]
end

def all; self.csv_data; end
def columns; self.columns; end
def column_names; self.columns.map(&:name); end
Expand Down
57 changes: 35 additions & 22 deletions spec/millionaire/csv_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
require 'millionaire/csv'

describe Millionaire::Csv do
class CsvRecord
include Millionaire::Csv
column :str, index: true
end

describe '.column' do
class CsvLoad
class Column
include Millionaire::Csv
column :index , index: true
column :presence, null: false, index: true
Expand All @@ -15,13 +20,11 @@ class CsvLoad
column :uniq1, uniq: [:index]
end

subject { CsvLoad.new }
it 'カラムが定義されている' do
CsvLoad.column_names.each {|name| should respond_to name }
end
subject { Column.new }
it { Column.column_names.each {|name| should respond_to name } }

context 'カラムの制約を設定できる' do
subject { CsvLoad.validators.index_by {|v| v.attributes.first } }
context 'column validation' do
subject { Column.validators.index_by {|v| v.attributes.first } }
its([:presence]) { should be_kind_of ActiveModel::Validations::PresenceValidator }
its([:length]) { should be_kind_of ActiveModel::Validations::LengthValidator }
its([:inclution]) { should be_kind_of ActiveModel::Validations::InclusionValidator }
Expand Down Expand Up @@ -56,29 +59,39 @@ class Index
end

describe '.all' do
class AllLoad
include Millionaire::Csv
column :str, index: true
end

let(:io) { StringIO.new %w(str foo bar).join("\n") }

before do
AllLoad.load io
CsvRecord.load io
end

subject { AllLoad.all }
subject { CsvRecord.all }
it { should have(2).recoed }

context 'CSVを読み込めている' do
subject { AllLoad.all.first }
context 'load csv' do
subject { CsvRecord.all.first }
its(:line_no) { should == 1 }
its(:str) { should == 'foo' }
end
end

describe '.find' do
before do
CsvRecord.load StringIO.new %w(str alice bob chris).join("\n")
end

subject { CsvRecord.find line_no }
let(:line_no) { 2 }
its(:str) { should == 'bob' }

context 'recoed not found' do
let(:line_no) { 100 }
it { should be_nil }
end
end

describe '.where' do
class AllLoad
class Where
include Millionaire::Csv
column :str_a
column :str_b
Expand All @@ -87,16 +100,16 @@ class AllLoad
end

before do
AllLoad.load StringIO.new %w(str_a,str_b,str_c 1,2,3 2,1,3 3,1,2).join("\n")
Where.load StringIO.new %w(str_a,str_b,str_c 1,2,3 2,1,3 3,1,2).join("\n")
end

context 'インデックスなし' do
subject { AllLoad.where(str_a: '1', str_b: '2').first }
context 'no index' do
subject { Where.where(str_a: '1', str_b: '2').first }
its(:line_no) { should == 1 }
end

context 'インデックスあり' do
subject { AllLoad.where(str_b: '1', str_c: '2').first }
context 'has index' do
subject { Where.where(str_b: '1', str_c: '2').first }
its(:line_no) { should == 3 }
end
end
Expand Down

0 comments on commit 60c28f9

Please sign in to comment.