Module: DocumentMapper::Document::ClassMethods

Defined in:
lib/document_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#allObject



102
103
104
# File 'lib/document_mapper/document.rb', line 102

def all
  @documents
end

#attributesObject



114
115
116
# File 'lib/document_mapper/document.rb', line 114

def attributes
  @documents.map(&:attributes).map(&:keys).flatten.uniq.sort
end

#directory=(new_directory) ⇒ Object

Raises:



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/document_mapper/document.rb', line 47

def directory=(new_directory)
  raise FileNotFoundError unless File.directory?(new_directory)

  reset
  @directory = Dir.new File.expand_path(new_directory)
  @directory.each do |file|
    next if file[0, 1] == '.'

    from_file [@directory.path, file].join('/')
  end
end

#firstObject



106
107
108
# File 'lib/document_mapper/document.rb', line 106

def first
  @documents.first
end

#from_file(file_path) ⇒ Object

Raises:



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/document_mapper/document.rb', line 35

def from_file(file_path)
  raise FileNotFoundError unless File.exist? file_path

  new.tap do |document|
    document.attributes = {
      file_path: File.expand_path(file_path)
    }
    document.read_yaml
    @documents << document
  end
end

#lastObject



110
111
112
# File 'lib/document_mapper/document.rb', line 110

def last
  @documents.last
end

#limit(number) ⇒ Object



98
99
100
# File 'lib/document_mapper/document.rb', line 98

def limit(number)
  Query.new(self).limit(number)
end

#offset(number) ⇒ Object



94
95
96
# File 'lib/document_mapper/document.rb', line 94

def offset(number)
  Query.new(self).offset(number)
end

#order_by(field) ⇒ Object



90
91
92
# File 'lib/document_mapper/document.rb', line 90

def order_by(field)
  Query.new(self).order_by(field)
end

#reloadObject



30
31
32
33
# File 'lib/document_mapper/document.rb', line 30

def reload
  reset
  self.directory = @directory.path
end

#resetObject



26
27
28
# File 'lib/document_mapper/document.rb', line 26

def reset
  @documents = []
end

#select(options = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/document_mapper/document.rb', line 59

def select(options = {})
  documents = @documents.dup
  options[:where].each do |selector, selector_value|
    documents = documents.select do |document|
      next unless document.attributes.key? selector.attribute

      document_value = document.send(selector.attribute)
      operator = OPERATOR_MAPPING[selector.operator]
      document_value.send operator, selector_value
    end
  end

  if options[:order_by].present?
    order_attribute = options[:order_by].keys.first
    asc_or_desc = options[:order_by].values.first
    documents = documents.select do |document|
      document.attributes.include? order_attribute
    end
    documents = documents.sort_by do |document|
      document.send order_attribute
    end
    documents.reverse! if asc_or_desc == :desc
  end

  documents
end

#where(hash) ⇒ Object



86
87
88
# File 'lib/document_mapper/document.rb', line 86

def where(hash)
  Query.new(self).where(hash)
end