Skip to content

Instantly share code, notes, and snippets.

@lgrains
Created November 7, 2013 21:39
Show Gist options
  • Save lgrains/7362281 to your computer and use it in GitHub Desktop.
Save lgrains/7362281 to your computer and use it in GitHub Desktop.
Taking a vote. Which looks cleaner and more rubyesque?
require 'ostruct'
class Instrument < OpenStruct
def self.instruments_array
InstrumentsCSVReader.get_from_file
end
def self.all
@all ||= instruments_array.map { |n| new(n) }
end
def self.find(description)
all.find { |n| n.name == description }
end
class InstrumentsCSVReader
require 'csv'
def self.get_from_file
instruments_array=[]
CSV.foreach("tmp/instruments.csv", headers: true) do |row|
instruments_array << { colleague_code: row["Key"], name: row["Description"] }
end
instruments_array
end
end
end
require 'ostruct'
class Instrument < OpenStruct
def self.all
@all ||= InstrumentsCSVReader.new.map { |n| new(n) }
end
def self.find(description)
all.find { |n| n.name == description }
end
class InstrumentsCSVReader < Array
require 'csv'
def initialize
super()
get_from_file
end
def get_from_file
CSV.foreach("tmp/instruments.csv", headers: true) do |row|
self << { colleague_code: row["Key"], name: row["Description"] }
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment