couch_photo 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -78,3 +78,47 @@ The previous variations were all simple image resizings. What if we wanted to do
78
78
  end
79
79
 
80
80
  The `original_image` variable in the blocks is simply the MiniMagick::Image instance of your original image.
81
+
82
+ ### Accessing Variations
83
+
84
+ So, now that you've got some variations, how do you access them? Simple!
85
+
86
+ Let's go back to our original image example:
87
+
88
+ class Image < CouchRest::Model::Base
89
+ use_database IMAGE_DB
90
+ include CouchPhoto
91
+
92
+ override_id! # the id of the document will be the basename of the original image file
93
+
94
+ variations do
95
+ small "20x20"
96
+ medium "100x100"
97
+ large "500x500"
98
+ end
99
+ end
100
+
101
+ i = Image.new
102
+ i.original "avatar.jpg"
103
+ i.save
104
+
105
+ We can access our variations in one of three ways:
106
+ - the `variations` method, which returns an array of all of our variations
107
+ - the `variation.<variation_name>` method, which returns a specific variation
108
+ - the `variation("<variation_name>")`, which also returns a specific variation
109
+
110
+ For example:
111
+
112
+ i.variations
113
+ i.variation.small
114
+ i.variation "large"
115
+
116
+ Once you've got a variation, you can call several methods on it:
117
+
118
+ i.variation.small.name # ==> "small"
119
+ i.variation.small.path # ==> "http://your_couch_server/your_image_database/avatar.jpg/variations/small.jpg"
120
+ i.variation.small.filename # ==> "variations/small.jpg"
121
+ i.variation.small.basename # ==> "small.jpg"
122
+ i.variation.small.filetype # ==> "jpg"
123
+ i.variation.small.mimetype # ==> "image/jpg"
124
+ i.variation.small.data # ==> BINARY BLOB
@@ -3,6 +3,23 @@ module CouchPhoto
3
3
  base.extend ClassMethods
4
4
  end
5
5
 
6
+ def update_or_create_attachment(attachment)
7
+ if self.has_attachment? attachment[:name]
8
+ self.update_attachment attachment
9
+ else
10
+ self.create_attachment attachment
11
+ end
12
+ end
13
+
14
+ def variations
15
+ @variations ||= Variations.new self
16
+ @variations.variations.values
17
+ end
18
+
19
+ def variation(variation_name=nil)
20
+ variation_name ? @variations.send(variation_name) : @variations
21
+ end
22
+
6
23
  def original(filepath, blob=nil)
7
24
  self["_id"] = File.basename filepath if self.class.override_id?
8
25
  blob ||= File.read filepath
@@ -14,24 +31,12 @@ module CouchPhoto
14
31
  update_or_create_attachment :name => "variations/#{variation_name}.#{image_format}", :file => Attachment.new(variation_blob, attachment_name)
15
32
  end
16
33
  end
17
-
18
- def update_or_create_attachment(attachment)
19
- if self.has_attachment? attachment[:name]
20
- self.update_attachment attachment
21
- else
22
- self.create_attachment attachment
23
- end
24
- end
25
34
 
26
35
  module ClassMethods
27
36
  def variations(&block)
28
37
  raise "You must pass a block to the `variations' method." unless block
29
38
  variation_definitions.instance_eval(&block)
30
39
  end
31
-
32
- def variation_definitions
33
- @variation_definitions ||= Variations.new
34
- end
35
40
 
36
41
  def override_id!
37
42
  @override_id = true
@@ -40,5 +45,9 @@ module CouchPhoto
40
45
  def override_id?
41
46
  @override_id
42
47
  end
48
+
49
+ def variation_definitions
50
+ @variation_definitions ||= VariationDefinitions.new
51
+ end
43
52
  end
44
53
  end
@@ -1,14 +1,14 @@
1
1
  module CouchPhoto
2
- class Variations
2
+ class VariationDefinitions
3
3
  def initialize
4
4
  @variations = {}
5
5
  end
6
6
 
7
7
  def method_missing(method_name, *args, &block)
8
8
  if block
9
- @variations[method_name.to_sym] = Variation.new method_name, *args, &block
9
+ @variations[method_name.to_sym] = VariationDefinition.new method_name, *args, &block
10
10
  else
11
- @variations[method_name.to_sym] = Variation.new method_name, *args
11
+ @variations[method_name.to_sym] = VariationDefinition.new method_name, *args
12
12
  end
13
13
  end
14
14
 
@@ -21,7 +21,7 @@ module CouchPhoto
21
21
  end
22
22
  end
23
23
 
24
- class Variation
24
+ class VariationDefinition
25
25
  def initialize(name, format=nil, &block)
26
26
  raise "A variation can not have both a format and a block. Choose one." if format and block
27
27
  raise "A variation must have either a format (e.g., '20x20>') or a block." if !format and !block
@@ -36,4 +36,41 @@ module CouchPhoto
36
36
  image.to_blob
37
37
  end
38
38
  end
39
+
40
+ class Variations
41
+ attr_reader :variations
42
+
43
+ def initialize(document)
44
+ @variations = {}
45
+ attachments = document["_attachments"] || {}
46
+ attachments.keys.select {|name| name.match /variations\//}.each do |variation_name|
47
+ variation_short_name = variation_name.gsub(/variations\/(.+)\.[^\.]+/) {$1}
48
+ @variations[variation_short_name] = Variation.new document, variation_name
49
+ end
50
+ end
51
+
52
+ def method_missing(method_name, *args, &block)
53
+ raise "Unknown variation '#{method_name}'" unless @variations[method_name.to_s]
54
+ @variations[method_name.to_s]
55
+ end
56
+ end
57
+
58
+ class Variation
59
+ attr_reader :name, :path, :filename, :basename, :filetype, :mimetype
60
+
61
+ def initialize(document, attachment_name)
62
+ @path = [document.database.to_s, document.id, attachment_name].join "/"
63
+ @attachment_name = attachment_name
64
+ @name = attachment_name.gsub(/variations\/(.+)\.[^\.]+/) {$1}
65
+ @filename = attachment_name
66
+ @basename = File.basename attachment_name
67
+ @document = document
68
+ @filetype = attachment_name.gsub(/variations\/.+\.([^\.]+)/) {$1}
69
+ @mimetype = document["_attachments"][attachment_name]["content_type"]
70
+ end
71
+
72
+ def data
73
+ @document.read_attachment @attachment_name
74
+ end
75
+ end
39
76
  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: 27
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 2
10
- version: 0.0.2
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Matt Parker
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-11-27 00:00:00 -05:00
18
+ date: 2010-11-29 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency