Class: Rid::Document
- Inherits:
-
Object
- Object
- Rid::Document
- Includes:
- Attachments
- Defined in:
- lib/rid/document.rb
Constant Summary
Constants included from Attachments
Attachments::MIME_TYPE_MAPPING
Instance Attribute Summary collapse
-
#hash ⇒ Object
Returns the value of attribute hash.
-
#path ⇒ Object
Returns the value of attribute path.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #checksum(*files) ⇒ Object
- #id ⇒ Object
-
#initialize(options = {}) ⇒ Document
constructor
A new instance of Document.
-
#json=(json) ⇒ Object
Build the documents hash from a JSON string.
-
#read!(&block) ⇒ Object
Read document from a filesystem.
- #rev ⇒ Object
- #rev=(value) ⇒ Object
-
#to_json ⇒ Object
Returns a JSON string representation of the documents hash.
Methods included from Attachments
#map_attachments!, #reduce_attachments!
Constructor Details
#initialize(options = {}) ⇒ Document
Returns a new instance of Document.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/rid/document.rb', line 10 def initialize( = {}) path = [:path] path = Pathname.new(path) unless path.nil? || path.is_a?(Pathname) @path = path # build hash from options hash @hash = [:hash] # build hash from json option self.json = [:json] if !@hash && [:json] @hash ||= {} end |
Instance Attribute Details
#hash ⇒ Object
Returns the value of attribute hash.
8 9 10 |
# File 'lib/rid/document.rb', line 8 def hash @hash end |
#path ⇒ Object
Returns the value of attribute path.
8 9 10 |
# File 'lib/rid/document.rb', line 8 def path @path end |
Instance Method Details
#==(other) ⇒ Object
21 22 23 |
# File 'lib/rid/document.rb', line 21 def ==(other) @hash == other.hash end |
#checksum(*files) ⇒ Object
98 99 100 |
# File 'lib/rid/document.rb', line 98 def checksum(*files) MD5.new files.map { |f| File.read(File.join(@path, f)) }.join end |
#id ⇒ Object
81 82 83 84 |
# File 'lib/rid/document.rb', line 81 def id @id ||= @hash['_id'] @id ||= @path && File.read(@path.join('_id')).strip rescue nil end |
#json=(json) ⇒ Object
Build the documents hash from a JSON string
77 78 79 |
# File 'lib/rid/document.rb', line 77 def json=(json) @hash = JSON.parse(json) end |
#read!(&block) ⇒ Object
Read document from a filesystem.
Takes a filename, many filenames, or an array of filenames and assign the return value of a yielded block to the hash.
Nested hashes like { “hash” => { “key” => “value” } } can be constructed if the filename contains a slash (/), eg “hash/key”.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/rid/document.rb', line 37 def read!(&block) raise 'No path given!' unless @path Dir[@path.join '**/*']. map! { |file| Pathname.new file }. delete_if { |f| !f.file? }. flatten. uniq. each do |filename| key = filename.relative_path_from(@path).to_s # strip js extname from javascript files key.sub!(/\.js$/, '') unless key =~ /^_attachments/ key.sub!(/\.erb$/, '') value = block_given? ? block.call(filename) : read_file(filename) if key !~ /^_attachments/ && key =~ /\.json$/ # parse json files value = JSON.parse(value) key.sub!(/\.json$/, '') else # strip value if its a one-liner value.strip! if value.count("\n") == 1 end @hash.update_at key, value end end |
#rev ⇒ Object
86 87 88 89 |
# File 'lib/rid/document.rb', line 86 def rev @rev ||= @hash['_rev'] @rev ||= @path && File.read(@path.join('_rev')).strip rescue nil end |
#rev=(value) ⇒ Object
91 92 93 94 95 96 |
# File 'lib/rid/document.rb', line 91 def rev=(value) @rev = @hash['_rev'] = value if @path File.open(@path.join('_rev'), "w") { |file| file << value } end end |
#to_json ⇒ Object
Returns a JSON string representation of the documents hash
71 72 73 |
# File 'lib/rid/document.rb', line 71 def to_json @hash.to_json end |