Module: Hashed

Defined in:
lib/hashed.rb,
lib/hashed/version.rb

Constant Summary collapse

VERSION =
"0.0.5"

Instance Method Summary collapse

Instance Method Details

#hashed(opts = {}) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/hashed.rb', line 5

def hashed(opts = {})
  result  = {}
  opts    = { by: opts } if opts.is_a? Symbol
  options = { by: (primary_key.respond_to?(:to_sym) ? primary_key.to_sym : primary_key.to_s), only: false }
  options.merge! opts

  dataset = options[:only] ? select([options[:by], options[:only]]) : self
  dataset.all.each do |row|
    result[row.send(options[:by])] = (options[:only] ? row.send(options[:only]) : row)
  end

  result
end