Skip to content

Commit

Permalink
generate individual dbstore for each locale
Browse files Browse the repository at this point in the history
and move finders to StreetNames::Finder
  • Loading branch information
despo committed Jul 31, 2012
1 parent e90b7cb commit 0a10540
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 44 deletions.
Binary file removed data/street_names.dat
Binary file not shown.
Binary file added data/street_names_en.dat
Binary file not shown.
Binary file added data/street_names_gr.dat
Binary file not shown.
1 change: 1 addition & 0 deletions lib/street_names.rb
@@ -1,5 +1,6 @@
require 'street_names/parser'
require 'street_names/database'
require 'street_names/finder'

module StreetNames

Expand Down
7 changes: 3 additions & 4 deletions lib/street_names/database.rb
Expand Up @@ -15,15 +15,14 @@ def save! data
datastore.transaction { datastore[:"streets_#{@locale}"] = data[:streets] }
end

def path
"./data/street_names.dat"
end

private
def datastore
@datastore ||= PStore.new(path)
end

def path
"./data/street_names_#{@locale}.dat"
end

end
end
26 changes: 26 additions & 0 deletions lib/street_names/finder.rb
@@ -0,0 +1,26 @@
module StreetNames
class Finder
attr_reader :streets

def initialize locale=LOCALE
@database = StreetNames::Database.new locale
convert!
end

[ :postcode, :name, :area ].each do |key|
define_method(:"find_by_#{key}") do |value|
@streets.select { |street| eval("street.#{key}") == value }
end
end

private

def convert!
@streets = @database.load_streets.map do |street|
Street.new [ street[:name], street[:postcode], street[:area] ]
end
end


end
end
8 changes: 1 addition & 7 deletions lib/street_names/parser.rb
Expand Up @@ -14,14 +14,8 @@ def load_cities
end.inject(:+)
end

[ :postcode, :name, :area ].each do |key|
define_method(:"find_by_#{key}") do |value|
@streets.select { |street| eval("street.#{key}") == value }
end
end

def save!
db = StreetNames::Database.new
db = StreetNames::Database.new @locale
db.save! :streets => @streets.map(&:to_hash)
end

Expand Down
23 changes: 6 additions & 17 deletions spec/street_names/database_spec.rb
Expand Up @@ -2,29 +2,18 @@

describe StreetNames::Database do

let(:streets) { [ { :name => "Road A" }, { :name => "Road B" }, { :name => "Not road C"} ] }
let(:database) { StreetNames::Database.new }

before(:all) do
`rm ./data/street_names.dat`
end

it 'is nil' do
database.load_streets.should == nil
end
let(:database) { StreetNames::Database.new "en" }

it 'can store a hash of streets' do
database.save! :streets => streets
datastore = mock
database.should_receive(:datastore).and_return(datastore)
datastore.should_receive(:transaction)

database.load_streets.should == streets
database.save! :streets => :streets
end

it 'can retrieve a hash of streets' do

database.load_streets.should == streets
database.load_streets.first.should be_a Hash
end

after(:all) do
`git checkout data/street_names.dat`
end
end
27 changes: 27 additions & 0 deletions spec/street_names/finder_spec.rb
@@ -0,0 +1,27 @@
require 'spec_helper'
describe StreetNames::Finder do

let (:street_finder) { StreetNames::Finder.new "en" }

it 'load streets from spreadsheet' do
street = { :name => "Agias Marinas", :postcode => 8041, :area => "Pafos"}

street_finder.streets.map(&:to_json).should include street.to_json
end

context '#finders' do
let(:street) { { :name => "Dimosthenous Georgiou", :postcode => 8020, :area => "Pafos"} }

it 'find streets by postcode' do
street_finder.find_by_postcode(8020).map(&:to_json).should include street.to_json
end

it 'find street by name' do
street_finder.find_by_name("Dimosthenous Georgiou").map(&:to_json).should include street.to_json
end

it 'find area by street name' do
street_finder.find_by_area("Pafos").map(&:to_json).should include street.to_json
end
end
end
16 changes: 0 additions & 16 deletions spec/street_names/parser_spec.rb
Expand Up @@ -11,22 +11,6 @@

street_parser.streets.map(&:to_json).should include street.to_json
end

context '#finders' do
let(:street) { { :name => "Dimosthenous Georgiou", :postcode => 8020, :area => "Pafos"} }

it 'find streets by postcode' do
street_parser.find_by_postcode(8020).map(&:to_json).should include street.to_json
end

it 'find street by name' do
street_parser.find_by_name("Dimosthenous Georgiou").map(&:to_json).should include street.to_json
end

it 'find area by street name' do
street_parser.find_by_area("Pafos").map(&:to_json).should include street.to_json
end
end
end

end

0 comments on commit 0a10540

Please sign in to comment.