Module: DocumentMapper::Document::ClassMethods
- Defined in:
- lib/document_mapper/document.rb
Instance Method Summary collapse
- #all ⇒ Object
- #attributes ⇒ Object
- #directory=(new_directory) ⇒ Object
- #first ⇒ Object
- #from_file(file_path) ⇒ Object
- #last ⇒ Object
- #limit(number) ⇒ Object
- #offset(number) ⇒ Object
- #order_by(field) ⇒ Object
- #reload ⇒ Object
- #reset ⇒ Object
- #select(options = {}) ⇒ Object
- #where(hash) ⇒ Object
Instance Method Details
#all ⇒ Object
102 103 104 |
# File 'lib/document_mapper/document.rb', line 102 def all @documents end |
#attributes ⇒ Object
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
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.(new_directory) @directory.each do |file| next if file[0, 1] == '.' from_file [@directory.path, file].join('/') end end |
#first ⇒ Object
106 107 108 |
# File 'lib/document_mapper/document.rb', line 106 def first @documents.first end |
#from_file(file_path) ⇒ Object
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.(file_path) } document.read_yaml @documents << document end end |
#last ⇒ Object
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 |
#reload ⇒ Object
30 31 32 33 |
# File 'lib/document_mapper/document.rb', line 30 def reload reset self.directory = @directory.path end |
#reset ⇒ Object
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( = {}) documents = @documents.dup [: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 [:order_by].present? order_attribute = [:order_by].keys.first asc_or_desc = [: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 |