Module: DocumentMapper::Document::ClassMethods

Defined in:
lib/document_mapper/document.rb

Instance Method Summary collapse

Instance Method Details

#allObject



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

def all
  @documents
end

#attributesObject



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

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

#directory=(new_directory) ⇒ Object

Raises:



45
46
47
48
49
50
51
52
53
# File 'lib/document_mapper/document.rb', line 45

def directory=(new_directory)
  raise FileNotFoundError unless File.directory?(new_directory)
  self.reset
  @directory = Dir.new File.expand_path(new_directory)
  @directory.each do |file|
    next if file[0,1] == '.'
    self.from_file [@directory.path, file].join('/')
  end
end

#firstObject



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

def first
  @documents.first
end

#from_file(file_path) ⇒ Object



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

def from_file(file_path)
  if !File.exist? file_path
    raise FileNotFoundError
  end
  self.new.tap do |document|
    document.attributes = {
      :file_path => File.expand_path(file_path)
    }
    document.read_yaml
    @documents << document
  end
end

#lastObject



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

def last
  @documents.last
end

#limit(number) ⇒ Object



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

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

#offset(number) ⇒ Object



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

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

#order_by(field) ⇒ Object



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

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

#reloadObject



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

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

#resetObject



23
24
25
# File 'lib/document_mapper/document.rb', line 23

def reset
  @documents = []
end

#select(options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/document_mapper/document.rb', line 55

def select(options = {})
  documents = @documents.dup
  options[:where].each do |selector, selector_value|
    documents = documents.select do |document|
      next unless document.attributes.has_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



81
82
83
# File 'lib/document_mapper/document.rb', line 81

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