Skip to content

Instantly share code, notes, and snippets.

@emad-elsaid
Forked from theHamdiz/drug_info.rb
Last active March 19, 2016 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emad-elsaid/c5b1061dba3313a8dbb3 to your computer and use it in GitHub Desktop.
Save emad-elsaid/c5b1061dba3313a8dbb3 to your computer and use it in GitHub Desktop.
  1. don't use self.method name, just put it in class << self
  2. don't use get_property, use the property name directly get_ and set_ is considered noise in ruby
  3. always add () in function definition with parameters
  4. you should always specify your rescued excpetion class
  5. there are a lot of cryptic lines with implicit logic like flatten[1]
  6. be consistent with methods names "_by(id)"
class Info
class << self
def category_by(id)
cat_id = read_file('drug_category').keep_if { |c| c[0] == id }.flatten[1]
categories = read_file('category')
cat = categories.select { |c| c[0] == cat_id }
parent_id = cat.flatten[1]
parent_cat = categories.select { |c| c[0] == parent_id }
parent_cat.flatten[2].empty? ? cat.flatten[2].to_s : "#{cat.flatten[2]} (#{parent_cat.flatten[2]})"
rescue StandardError => error
puts error
end
def company_by(id)
read_file('companies').keep_if { |c| c[0] == id }.flatten[1]
end
def active_ingredients(id)
ais = read_file('drug_ai')
active_ingredient = ais.select { |a| a[1] == id }
active_ingredient.each_with_object([]) do |ac, ing|
concentration = ac[2]
name = read_file('ai').select { |n| n[0] == ac[0] }.flatten
unit = read_file('drug_ai_unit').select { |u| u[0] == ac[3] }.flatten
ing << "#{name[1].capitalize} #{concentration} #{unit[1].downcase}"
end
end
def form_by(id)
read_file('form').select { |f| f[0] == id }.flatten[1]
end
def read_file(file)
file_name = file.end_with?('.csv') ? file : file + '.csv'
CSV.read(file_name, col_sep: '|').tap(&:shift)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment