mattetti-couchrest 0.13.3 → 0.14

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,68 @@
1
+ module CouchRest
2
+ module Mixins
3
+ module ExtendedAttachments
4
+
5
+ # creates a file attachment to the current doc
6
+ def create_attachment(args={})
7
+ raise ArgumentError unless args[:file] && args[:name]
8
+ return if has_attachment?(args[:name])
9
+ self['_attachments'] ||= {}
10
+ set_attachment_attr(args)
11
+ rescue ArgumentError => e
12
+ raise ArgumentError, 'You must specify :file and :name'
13
+ end
14
+
15
+ # reads the data from an attachment
16
+ def read_attachment(attachment_name)
17
+ Base64.decode64(database.fetch_attachment(self.id, attachment_name))
18
+ end
19
+
20
+ # modifies a file attachment on the current doc
21
+ def update_attachment(args={})
22
+ raise ArgumentError unless args[:file] && args[:name]
23
+ return unless has_attachment?(args[:name])
24
+ delete_attachment(args[:name])
25
+ set_attachment_attr(args)
26
+ rescue ArgumentError => e
27
+ raise ArgumentError, 'You must specify :file and :name'
28
+ end
29
+
30
+ # deletes a file attachment from the current doc
31
+ def delete_attachment(attachment_name)
32
+ return unless self['_attachments']
33
+ self['_attachments'].delete attachment_name
34
+ end
35
+
36
+ # returns true if attachment_name exists
37
+ def has_attachment?(attachment_name)
38
+ !!(self['_attachments'] && self['_attachments'][attachment_name] && !self['_attachments'][attachment_name].empty?)
39
+ end
40
+
41
+ # returns URL to fetch the attachment from
42
+ def attachment_url(attachment_name)
43
+ return unless has_attachment?(attachment_name)
44
+ "#{database.root}/#{self.id}/#{attachment_name}"
45
+ end
46
+
47
+ private
48
+
49
+ def encode_attachment(data)
50
+ ::Base64.encode64(data).gsub(/\r|\n/,'')
51
+ end
52
+
53
+ def get_mime_type(file)
54
+ ::MIME::Types.type_for(file.path).empty? ?
55
+ 'text\/plain' : MIME::Types.type_for(file.path).first.content_type.gsub(/\//,'\/')
56
+ end
57
+
58
+ def set_attachment_attr(args)
59
+ content_type = args[:content_type] ? args[:content_type] : get_mime_type(args[:file])
60
+ self['_attachments'][args[:name]] = {
61
+ 'content-type' => content_type,
62
+ 'data' => encode_attachment(args[:file].read)
63
+ }
64
+ end
65
+
66
+ end # module ExtendedAttachments
67
+ end
68
+ end
@@ -2,4 +2,5 @@ require File.join(File.dirname(__FILE__), 'properties')
2
2
  require File.join(File.dirname(__FILE__), 'document_queries')
3
3
  require File.join(File.dirname(__FILE__), 'views')
4
4
  require File.join(File.dirname(__FILE__), 'design_doc')
5
- require File.join(File.dirname(__FILE__), 'validation')
5
+ require File.join(File.dirname(__FILE__), 'validation')
6
+ require File.join(File.dirname(__FILE__), 'extended_attachments')
@@ -17,6 +17,7 @@ module CouchRest
17
17
  include CouchRest::Mixins::DocumentQueries
18
18
  include CouchRest::Mixins::Views
19
19
  include CouchRest::Mixins::DesignDoc
20
+ include CouchRest::Mixins::ExtendedAttachments
20
21
 
21
22
  def self.inherited(subklass)
22
23
  subklass.send(:include, CouchRest::Mixins::Properties)
@@ -29,8 +30,8 @@ module CouchRest
29
30
  define_callbacks :destroy
30
31
 
31
32
  def initialize(keys={})
32
- super
33
33
  apply_defaults # defined in CouchRest::Mixins::Properties
34
+ super
34
35
  cast_keys # defined in CouchRest::Mixins::Properties
35
36
  unless self['_id'] && self['_rev']
36
37
  self['couchrest-type'] = self.class.to_s
@@ -195,10 +196,10 @@ module CouchRest
195
196
  # Deletes the document from the database. Runs the :destroy callbacks.
196
197
  # Removes the <tt>_id</tt> and <tt>_rev</tt> fields, preparing the
197
198
  # document to be saved to a new <tt>_id</tt>.
198
- def destroy
199
+ def destroy(bulk=false)
199
200
  caught = catch(:halt) do
200
201
  _run_destroy_callbacks do
201
- result = database.delete_doc self
202
+ result = database.delete_doc(self, bulk)
202
203
  if result['ok']
203
204
  self['_rev'] = nil
204
205
  self['_id'] = nil
data/lib/couchrest.rb CHANGED
@@ -27,7 +27,7 @@ require 'couchrest/monkeypatches'
27
27
 
28
28
  # = CouchDB, close to the metal
29
29
  module CouchRest
30
- VERSION = '0.13.3'
30
+ VERSION = '0.14'
31
31
 
32
32
  autoload :Server, 'couchrest/core/server'
33
33
  autoload :Database, 'couchrest/core/database'
@@ -6,6 +6,7 @@ describe "ExtendedDocument" do
6
6
  use_database TEST_SERVER.default_database
7
7
  property :preset, :default => {:right => 10, :top_align => false}
8
8
  property :set_by_proc, :default => Proc.new{Time.now}, :cast_as => 'Time'
9
+ property :tags, :default => []
9
10
  property :name
10
11
  timestamps!
11
12
  end
@@ -54,6 +55,16 @@ describe "ExtendedDocument" do
54
55
  @obj.set_by_proc.should == @obj.set_by_proc
55
56
  @obj.set_by_proc.should < Time.now
56
57
  end
58
+
59
+ it "should let you overwrite the default values" do
60
+ obj = WithDefaultValues.new(:preset => 'test')
61
+ obj.preset = 'test'
62
+ end
63
+
64
+ it "should work with a default empty array" do
65
+ obj = WithDefaultValues.new(:tags => ['spec'])
66
+ obj.tags.should == ['spec']
67
+ end
57
68
  end
58
69
 
59
70
  describe "timestamping" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mattetti-couchrest
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.3
4
+ version: "0.14"
5
5
  platform: ruby
6
6
  authors:
7
7
  - J. Chris Anderson
@@ -95,6 +95,7 @@ files:
95
95
  - lib/couchrest/mixins/callbacks.rb
96
96
  - lib/couchrest/mixins/design_doc.rb
97
97
  - lib/couchrest/mixins/document_queries.rb
98
+ - lib/couchrest/mixins/extended_attachments.rb
98
99
  - lib/couchrest/mixins/extended_document_mixins.rb
99
100
  - lib/couchrest/mixins/properties.rb
100
101
  - lib/couchrest/mixins/validation.rb