class HashDB::Base

Public Class Methods

new(data=nil) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 4
def initialize(data=nil)
  if data.present?
    @data = Array(data)
  else
    @data = []
  end
end

Public Instance Methods

all(options={}) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 26
def all(options={})
  if options.has_key?(:conditions)
    where(options[:conditions])
  else
    wrap(@data ||= [])
  end
end
count() click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 34
def count
  all.length
end
data() click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 12
def data
  @data
end
data=(data) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 16
def data=(data)
  @data = data || []
end
delete_all() click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 83
def delete_all
  @data = []
end
find(id, * args) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 95
def find(id, * args)
  case id
    when nil
      nil
    when :all
      all
    when :first
      all(*args).first
    when Array
      id.map { |i| find(i) }
    else
      where({id: id})
  end
end
first() click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 87
def first
  @data.first
end
last() click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 91
def last
  @data.last
end
limit( count=nil ) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 75
def limit( count=nil )
  if count.present? and count>0
    wrap(@data.slice(0,count))
  else
    all
  end
end
scoped_collection( scope = :all ) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 60
def scoped_collection ( scope = :all )
  scope = scope.to_a if scope.is_a? Hash
  Array(scope).inject(self) do |o, a|
    o.try(*a)
  end
end
select( fields=nil ) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 67
def select( fields=nil )
  if fields.present?
    wrap(@data.map{|v| v.slice(*(Array(fields).map(&:to_sym)))})
  else
    all
  end
end
where(options) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 38
def where(options)
  return @data if options.blank?

  data = (@data || []).select do |record|
    match_options?(record, options)
  end

  wrap(data)
end

Private Instance Methods

match_options?(record, options) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 48
def match_options?(record, options)
  options.all? do |col, match|
    if [Array, Range].include?(match.class)
      match.include?(record[col])
    else
      record[col] == match
    end
  end
end
wrap(data) click to toggle source
# File lib/action_cable_notifications/hash_db.rb, line 20
def wrap(data)
  HashDB::Base.new(data)
end