- don't use self.method name, just put it in class << self
- don't use get_property, use the property name directly get_ and set_ is considered noise in ruby
- always add () in function definition with parameters
- you should always specify your rescued excpetion class
- there are a lot of cryptic lines with implicit logic like flatten[1]
- be consistent with methods names "_by(id)"
-
-
Save emad-elsaid/c5b1061dba3313a8dbb3 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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