Skip to content

Commit

Permalink
インデックス作成ができるようになった
Browse files Browse the repository at this point in the history
  • Loading branch information
hidenba committed Feb 13, 2012
1 parent a6e1aaa commit 2903761
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
14 changes: 13 additions & 1 deletion lib/millionaire/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ module Millionaire::Base
include ActiveModel::Validations

included do
class_attribute :csv_data, :columns
class_attribute :csv_data, :columns, :indexes
self.columns = []
self.indexes = {}
end

def initialize(attr={})
Expand All @@ -25,20 +26,31 @@ def column(name, option={})
when :length; validates name, length: {maximum: v}
when :value; validates name, inclusion: {in: v}
when :constraint; validates name, v
when :index; self.indexes[name.to_s] = []
end
end
end

def index(*name)
name.each do |n|
self.indexes[n.is_a?(Array) ? n.map(&:to_s).join('_') : n.to_s] = []
end
end

def load(io)
self.csv_data = []
csv = ::CSV.new(io, headers: :first_row, return_headers: false)
csv.each do |row|
self.csv_data << self.new(row.to_hash.slice(*self.column_names))
end
self.indexes.keys.each do |k|
self.indexes[k] = self.csv_data.group_by{|v| v.send(k) }
end
end

def all; self.csv_data; end
def columns; self.columns; end
def column_names; self.columns.map(&:name); end
def indexes; self.indexes; end
end
end
36 changes: 30 additions & 6 deletions spec/millionaire/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
describe '.column' do
class CsvLoad
include Millionaire::Base
column :presence, null: false
column :index , index: true
column :presence, null: false, index: true
column :length, length: 20
column :inclution, value: %w(foo bar)
column :constraint, constraint: {format: {with: /\A[a-zA-Z]+\z/}}
Expand All @@ -26,26 +27,49 @@ class CsvLoad
its([:constraint]) { should be_kind_of ActiveModel::Validations::FormatValidator }
its([:int]) { should be_kind_of ActiveModel::Validations::InclusionValidator }
end

context 'インデックスが設定できる' do
subject { CsvLoad.indexes }
its(:keys) { should =~ ['index', 'presence'] }
end
end

describe '.index' do
class Index
include Millionaire::Base
column :index_a
column :index_b
index :index_a, :index_b
index [:index_a, :index_b]
end

subject { Index.indexes }
its(:keys) { should =~ ['index_a', 'index_b', 'index_a_index_b'] }
end

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

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

before do
CsvLoad.load io
AllLoad.load io
end

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

context '設定したカラムに値が入っている' do
subject { CsvLoad.all.first }
subject { AllLoad.all.first }
its(:str) { should == 'foo' }
end

context 'インデックスが作成されている' do
subject { AllLoad.indexes['str'] }
its(['foo']) { should have(1).record }
end
end
end

0 comments on commit 2903761

Please sign in to comment.