Created
March 13, 2009 22:45
-
-
Save Bregor/78808 to your computer and use it in GitHub Desktop.
This file contains 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
def read_many(query) | |
puts query.inspect | |
DataMapper::Collection.new(query) do |set| | |
read(query, set, true) | |
end | |
end | |
def read(query, set, arr = true) | |
file = './config/dhcpd.conf' | |
repository_name = query.repository.name | |
properties = query.fields | |
puts "repository_name: \t#{repository_name}, \nproperties: \t#{properties}" | |
results = [] | |
File.read(file).scan(/^host\s+([^\s\{]+).+?hardware\sethernet\s+([^;]+).+?fixed-address\s+([\d\.]+)/m).each do |host| | |
results << [host[0], host[1], host[2]] | |
results.each do |result| | |
values = result_values(result, properties, repository_name) | |
# This is the core logic that handles the difference between all/first | |
arr ? set.load(values) : (break set.load(values, query)) | |
end | |
end | |
def result_values(result, properties, repository_name) | |
properties.map { |p| result.send(p.field(repository_name)) } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment