couch_photo 0.0.3 → 0.0.4

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.
data/README.markdown CHANGED
@@ -36,14 +36,14 @@ First, define an Image document and set the versions on it:
36
36
  Next, create an instance of the image document and set the "original" image on it.
37
37
 
38
38
  i = Image.new
39
- i.original "avatar.jpg"
39
+ i.original = "avatar.jpg"
40
40
  i.save
41
41
 
42
42
  By calling the original method with a string, CouchPhoto will attempt to open a file with the same name and use that as the original image. If you don't have
43
43
  an actual file (e.g., maybe this was a form upload), then simply pass a blob as the second parameter. For example,
44
44
 
45
45
  blob = File.read "avatar.jpg"
46
- i.original "avatar.jpg", blob
46
+ i.original = ["avatar.jpg", blob]
47
47
 
48
48
  Now, if you look in your image document in CouchDB, you'd see the following attachments:
49
49
 
@@ -79,6 +79,19 @@ The previous variations were all simple image resizings. What if we wanted to do
79
79
 
80
80
  The `original_image` variable in the blocks is simply the MiniMagick::Image instance of your original image.
81
81
 
82
+ ### Accessing the Original Image
83
+
84
+ How do you access the the original image? Let me count the ways:
85
+
86
+ @image.original.original_filename # ==> "avatar.jpg"
87
+ @image.original.filename # ==> "original.jpg"
88
+ @image.original.path # ==> "/your_image_database/avatar.jpg/original.jpg"
89
+ @image.original.url # ==> "http://your_couch_server/your_image_database/avatar.jpg/original.jpg"
90
+ @image.original.basename # ==> "original.jpg"
91
+ @image.original.filetype # ==> "jpg"
92
+ @image.original.mimetype # ==> "image/jpg"
93
+ @image.original.data # ==> BINARY BLOB
94
+
82
95
  ### Accessing Variations
83
96
 
84
97
  So, now that you've got some variations, how do you access them? Simple!
@@ -99,7 +112,7 @@ Let's go back to our original image example:
99
112
  end
100
113
 
101
114
  i = Image.new
102
- i.original "avatar.jpg"
115
+ i.original = "avatar.jpg"
103
116
  i.save
104
117
 
105
118
  We can access our variations in one of three ways:
@@ -1,6 +1,7 @@
1
1
  module CouchPhoto
2
2
  def self.included(base)
3
3
  base.extend ClassMethods
4
+ base.property :original_filename
4
5
  end
5
6
 
6
7
  def update_or_create_attachment(attachment)
@@ -16,12 +17,23 @@ module CouchPhoto
16
17
  @variations.variations.values
17
18
  end
18
19
 
20
+ def original
21
+ raise "You do not have an original attachment" unless original_attachment_name
22
+ @original ||= Variation.new self, original_attachment_name
23
+ end
24
+
25
+ def original_attachment_name
26
+ @original_attachment_name ||= self["_attachments"].keys.select {|a| a.match /original\.[^\.]+/}.first
27
+ end
28
+
19
29
  def variation(variation_name=nil)
20
30
  variation_name ? @variations.send(variation_name) : @variations
21
31
  end
22
32
 
23
- def original(filepath, blob=nil)
33
+ def original=(*args)#filepath, blob=nil
34
+ filepath, blob = args[0], args[1]
24
35
  self["_id"] = File.basename filepath if self.class.override_id?
36
+ self.original_filename = File.basename filepath
25
37
  blob ||= File.read filepath
26
38
  image_format = filepath.match(/^.*\.([^\.]*)$/)[1]
27
39
  attachment_name = "original.#{image_format}"
@@ -56,21 +56,26 @@ module CouchPhoto
56
56
  end
57
57
 
58
58
  class Variation
59
- attr_reader :name, :path, :filename, :basename, :filetype, :mimetype
59
+ attr_reader :name, :url, :path, :filename, :basename, :filetype, :mimetype
60
60
 
61
61
  def initialize(document, attachment_name)
62
- @path = [document.database.to_s, document.id, attachment_name].join "/"
62
+ @path = "/" + [document.database.name, document.id, attachment_name].join("/")
63
+ @url = [document.database.to_s, document.id, attachment_name].join "/"
63
64
  @attachment_name = attachment_name
64
- @name = attachment_name.gsub(/variations\/(.+)\.[^\.]+/) {$1}
65
+ @name = attachment_name.gsub(/(?:variations\/)?(.+)\.[^\.]+/) {$1}
65
66
  @filename = attachment_name
66
67
  @basename = File.basename attachment_name
67
68
  @document = document
68
- @filetype = attachment_name.gsub(/variations\/.+\.([^\.]+)/) {$1}
69
+ @filetype = attachment_name.gsub(/(?:variations\/)?.+\.([^\.]+)/) {$1}
69
70
  @mimetype = document["_attachments"][attachment_name]["content_type"]
70
71
  end
71
72
 
72
73
  def data
73
74
  @document.read_attachment @attachment_name
74
75
  end
76
+
77
+ def original_filename
78
+ @document.original_filename
79
+ end
75
80
  end
76
81
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: couch_photo
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 3
10
- version: 0.0.3
9
+ - 4
10
+ version: 0.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker