mirror of https://github.com/fspc/BikeShed-1.git
Ilya Konanykhin
8 years ago
4 changed files with 124 additions and 125 deletions
@ -0,0 +1,78 @@ |
|||||
|
class BikeCsvImporter |
||||
|
module BikeAttrs |
||||
|
def bike_attr_fields |
||||
|
%i{ shop_id bike_purpose_id value bike_brand_id bike_model_id model bike_style_id bike_condition_id seat_tube_height bike_wheel_size_id serial_number } |
||||
|
end |
||||
|
|
||||
|
def bike_attrs(bike_hash) |
||||
|
bike_attr_fields.each_with_object({}) do |field, memo| |
||||
|
memo[field] = send :"bike_attr_#{ field }", bike_hash |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
def bike_attr_shop_id(bike_hash) |
||||
|
bike_hash['velocipede number'].to_i |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_purpose_id(bike_hash) |
||||
|
map = { |
||||
|
'SALE' => /shop|as(-|\s+)is|safety\s*check/, |
||||
|
'BUILDBIKE' => /build|bikes.*world/, |
||||
|
'STORAGE' => nil, |
||||
|
'PARTS' => /part|frame/, |
||||
|
'SCRAP' => /scrap|strip/, |
||||
|
} |
||||
|
|
||||
|
default = 'UNDETERMINED' |
||||
|
test_value = clean_value(bike_hash['program']).try :downcase |
||||
|
value = map.find { |_, regexp| regexp.try :match, test_value }.try :first |
||||
|
|
||||
|
cached_bike_purpose(value || default).id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_gone(bike_hash) |
||||
|
%w{ yes yeah y }.include? clean_value(bike_hash['gone']).try :downcase |
||||
|
end |
||||
|
|
||||
|
def bike_attr_value(bike_hash) |
||||
|
clean_value(bike_hash['price']).try(:gsub, /[$]/, '').try :to_i |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_brand_id(bike_hash) |
||||
|
brand = clean_value(bike_hash['make']) |
||||
|
return unless brand |
||||
|
cached_bike_brand(brand).try :id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_model_id(bike_hash) |
||||
|
model = clean_value(bike_hash['model']) |
||||
|
return unless model |
||||
|
cached_bike_model(model).try :id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_model(bike_hash) |
||||
|
model = clean_value bike_hash['model'] |
||||
|
model unless model =~ /unknown/i |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_style_id(_) |
||||
|
@bike_style_other_cache ||= BikeStyle.find_by_style('OTHER').id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_condition_id(_) |
||||
|
@bike_condition_undertermined_cache ||= BikeCondition.find_by_condition('UNDETERMINED').id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_seat_tube_height(_) |
||||
|
0 |
||||
|
end |
||||
|
|
||||
|
def bike_attr_bike_wheel_size_id(_) |
||||
|
@bike_condition_wheel_size_undertermined_cache ||= BikeWheelSize.find_by_description('UNDETERMINED').id |
||||
|
end |
||||
|
|
||||
|
def bike_attr_serial_number(_) |
||||
|
'UNDETERMINED' |
||||
|
end |
||||
|
end |
||||
|
end |
@ -0,0 +1,26 @@ |
|||||
|
class BikeCsvImporter |
||||
|
module Cache |
||||
|
def cached_bike_purpose(purpose) |
||||
|
@bike_purpose_cache ||= {} |
||||
|
@bike_purpose_cache[purpose] ||= BikePurpose.find_by_purpose purpose |
||||
|
end |
||||
|
|
||||
|
def cached_bike_brand(brand) |
||||
|
@bike_brand_cache ||= {} |
||||
|
if @bike_brand_cache.has_key? brand |
||||
|
@bike_brand_cache[brand] |
||||
|
else |
||||
|
@bike_brand_cache[brand] = BikeBrand.where('lower(brand) = ?', brand.downcase).first |
||||
|
end |
||||
|
end |
||||
|
|
||||
|
def cached_bike_model(model) |
||||
|
@bike_model_cache ||= {} |
||||
|
if @bike_model_cache.has_key? model |
||||
|
@bike_model_cache[model] |
||||
|
else |
||||
|
@bike_model_cache[model] = BikeModel.where('lower(model) = ?', model.downcase).first |
||||
|
end |
||||
|
end |
||||
|
end |
||||
|
end |
@ -0,0 +1,15 @@ |
|||||
|
class BikeCsvImporter |
||||
|
module Cleaner |
||||
|
def clean_value(value) |
||||
|
value_or_nil strip_value(value) |
||||
|
end |
||||
|
|
||||
|
def strip_value(value) |
||||
|
value.try(:strip).try(:gsub, /\n|\r/, '') |
||||
|
end |
||||
|
|
||||
|
def value_or_nil(value) |
||||
|
return value unless ['?', 'n/a', 'missing', 'unknown', ''].include? value.try(:downcase) |
||||
|
end |
||||
|
end |
||||
|
end |
Loading…
Reference in new issue