Class: HashDB::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/action_cable_notifications/hash_db.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = nil) ⇒ Base

Returns a new instance of Base.



4
5
6
7
8
9
10
# File 'lib/action_cable_notifications/hash_db.rb', line 4

def initialize(data=nil)
  if data.present?
    @data = Array(data)
  else
    @data = []
  end
end

Instance Method Details

#all(options = {}) ⇒ Object



26
27
28
29
30
31
32
# 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

#countObject



34
35
36
# File 'lib/action_cable_notifications/hash_db.rb', line 34

def count
  all.length
end

#dataObject



12
13
14
# File 'lib/action_cable_notifications/hash_db.rb', line 12

def data
  @data
end

#data=(data) ⇒ Object



16
17
18
# File 'lib/action_cable_notifications/hash_db.rb', line 16

def data=(data)
  @data = data || []
end

#delete_allObject



83
84
85
# File 'lib/action_cable_notifications/hash_db.rb', line 83

def delete_all
  @data = []
end

#find(id, *args) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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

#firstObject



87
88
89
# File 'lib/action_cable_notifications/hash_db.rb', line 87

def first
  @data.first
end

#lastObject



91
92
93
# File 'lib/action_cable_notifications/hash_db.rb', line 91

def last
  @data.last
end

#limit(count = nil) ⇒ Object



75
76
77
78
79
80
81
# 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) ⇒ Object



60
61
62
63
64
65
# 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) ⇒ Object



67
68
69
70
71
72
73
# 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) ⇒ Object



38
39
40
41
42
43
44
45
46
# 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