Module: Ymodel::Queryable

Included in:
Base
Defined in:
lib/ymodel/queryable.rb

Overview

This mixin holds the query methods.

Instance Method Summary collapse

Instance Method Details

#allObject



37
38
39
40
41
42
43
44
45
# File 'lib/ymodel/queryable.rb', line 37

def all
  if @pears
    load_records!
  else
    @all ||= load_records!
  end
rescue Errno::ENOENT
  raise SourceFileNotFound
end

#find(index) ⇒ Object



6
7
8
# File 'lib/ymodel/queryable.rb', line 6

def find(index)
  all.find { |record| record.index == index }
end

#find!(index) ⇒ Object



10
11
12
# File 'lib/ymodel/queryable.rb', line 10

def find!(index)
  find(index) || raise_record_not_found_exception!(index)
end

#find_by(attributes) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/ymodel/queryable.rb', line 14

def find_by(attributes)
  sanitized = sanitize_attributes(attributes)
  all.each do |record|
    return record if sanitized.all? { |k, v| record.send(k) == v }
  end

  nil
end

#find_by!(attributes) ⇒ Object



23
24
25
# File 'lib/ymodel/queryable.rb', line 23

def find_by!(attributes)
  find_by(attributes) || raise_record_not_found_exception!(attributes)
end

#find_by_key(key) ⇒ Object



27
28
29
30
31
# File 'lib/ymodel/queryable.rb', line 27

def find_by_key(key)
  all.find do |record|
    record.key == key.to_s
  end
end

#find_by_key!(key) ⇒ Object



33
34
35
# File 'lib/ymodel/queryable.rb', line 33

def find_by_key!(key)
  find_by_key(key) || raise_record_not_found_exception!(key)
end

#raise_record_not_found_exception!(attributes = nil) ⇒ Object

Raises:



77
78
79
80
81
82
83
84
85
# File 'lib/ymodel/queryable.rb', line 77

def raise_record_not_found_exception!(attributes = nil)
  if attributes.is_a?(Hash)
    message = "Couldn't find #{name} with "
    attributes.each { |k, v| message += "#{k}: #{v}" }
  else
    message = "Couldn't find #{name} #{attributes}"
  end
  raise RecordNotFound, message
end

#sanitize_attributes(attributes) ⇒ Object



71
72
73
74
75
# File 'lib/ymodel/queryable.rb', line 71

def sanitize_attributes(attributes)
  # definitive schema
  attributes.symbolize_keys!
    .select { |attr| schema.include?(attr) }
end

#where(attributes) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/ymodel/queryable.rb', line 47

def where(attributes)
  sanitized = sanitize_attributes(attributes)

  if sanitized.length != attributes.length
    unpermitted = (attributes.keys.map(&:to_sym) - sanitized.keys)
    message = "These attributes are not allowed: #{unpermitted}"

    raise UnpermittedParamsError, message
  end

  all.select do |record|
    sanitized.all? { |key, value| record.send(key) == value }
  end
end

#where!(attributes) ⇒ Object

Beware of using this method. If all attributes get removed during sanitation it returns the equivalent of #all.



64
65
66
67
68
69
# File 'lib/ymodel/queryable.rb', line 64

def where!(attributes)
  sanitized = sanitize_attributes(attributes)
  all.select do |record|
    sanitized.all? { |key, value| record.send(key) == value }
  end
end