magick_title 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/magick_title.rb CHANGED
@@ -30,10 +30,8 @@ module MagickTitle
30
30
  # MagickTitle.image("Hello!")
31
31
  # MagickTitle.say("Hi", :refresh => true)
32
32
  #
33
- def image(text, opts={})
34
- title = ::MagickTitle::Image.new(text, opts)
35
- title.save unless title.options.cache && File.exists?(title.fullpath)
36
- title
33
+ def image(*args)
34
+ MagickTitle::Image.create(*args)
37
35
  end
38
36
  alias :say :image
39
37
 
@@ -1,5 +1,4 @@
1
1
  require 'digest/sha1'
2
- require 'fileutils' unless defined?(FileUtils)
3
2
 
4
3
  module MagickTitle
5
4
 
@@ -22,32 +21,64 @@ module MagickTitle
22
21
  attr_reader :url
23
22
 
24
23
 
24
+ # Creates an instance of MagickTitle::Image
25
+ #
26
+ # MagickTitle::Image.create("text")
27
+ # MagickTitle::Image.create("large", :font_size => 60)
28
+ #
29
+ def self.create(*args)
30
+ title = new(*args)
31
+ return unless title.save
32
+ title
33
+ end
34
+
35
+
25
36
  # Initializes a new image title with a string
26
37
  def initialize(text="", opts={})
27
38
  update(text, opts)
28
39
  end
29
40
 
30
41
 
31
- # updates the image title to reflect new text
42
+ # updates the image title to reflect new text and returns self
32
43
  def update(text, opts={})
33
44
  @text = text
34
- return false unless valid?
45
+ return unless valid?
46
+
47
+ # save the fullpath so we can delete it later
48
+ @old_path = fullpath
49
+
35
50
  @options = (@options || MagickTitle.options).merge(opts.symbolize_keys)
36
51
  @filename = filename_from_options #unique_filename(@text)
37
52
  @path = options.destination
38
53
  @url = File.join((@path.match(/public(\/.*)/) || ['', './'])[1].to_s, @filename)
54
+ self
39
55
  end
40
56
 
41
57
 
42
-
43
- # saves title and generates image
58
+ # Saves title and generates image
44
59
  def save
45
- return false unless valid?
60
+ # validate
61
+ return unless valid?
62
+
63
+ # check for caching
64
+ return true if options.cache && !dirty?
65
+
66
+ # delete if an old title exists
67
+ delete(@old_path)
68
+
69
+ # delete current image
70
+ delete(fullpath)
71
+
46
72
  FileUtils.mkdir_p(path)
47
73
  run('convert', title_command_string(fullpath))
48
74
  File.exists?(fullpath)
49
75
  end
50
76
 
77
+ # Deletes the specified image
78
+ def delete(file=fullpath)
79
+ FileUtils.rm(file) if file && File.exists?(file)
80
+ end
81
+
51
82
 
52
83
  # Checks if the image title is valid
53
84
  def valid?
@@ -55,8 +86,21 @@ module MagickTitle
55
86
  end
56
87
 
57
88
 
89
+ # Checks if the image title needs to be saved
90
+ def dirty?
91
+ !exists? || (@old_path && @old_path != fullpath)
92
+ end
93
+
94
+ # Checks if the specified file exists
95
+ def exists?(file=fullpath)
96
+ return unless file
97
+ file = [file, options.extension].join(".") unless file.match(/\.[a-z]{3,4}$/)
98
+ File.exists?(file)
99
+ end
100
+
58
101
  # Returns the full path to the file
59
102
  def fullpath
103
+ return unless path && filename
60
104
  File.join(path, filename)
61
105
  end
62
106
 
@@ -147,22 +191,16 @@ module MagickTitle
147
191
  # creates a unique filename for the title's text
148
192
  def unique_filename
149
193
  file = fileize_text(@text)
150
- exists = exists_in_destination? file
194
+ exists = exists? file
151
195
  dupe, count = nil, 0
152
196
  while exists do
153
197
  count += 1
154
198
  dupe = "#{file}_#{count}"
155
- exists = exists_in_destination? dupe
199
+ exists = exists? dupe
156
200
  end
157
201
  dupe || file
158
202
  end
159
203
 
160
-
161
- # Checks if file exists in the destination option
162
- def exists_in_destination?(file)
163
- file = [file, options.extension].join(".") unless file.match(/\.[a-z]{3,4}$/)
164
- File.exists?(File.join(options.destination, file))
165
- end
166
204
 
167
205
  end # Image
168
206
 
@@ -33,6 +33,7 @@ module MagickTitle
33
33
  :font_size => 50,
34
34
  :destination => Proc.new{ File.join MagickTitle.root, "public/system/titles" },
35
35
  :extension => "png",
36
+ :text_transform => nil,
36
37
  :width => 800,
37
38
  :height => nil,
38
39
  :background_color => '#ffffff',
@@ -1,3 +1,3 @@
1
1
  module MagickTitle
2
- VERSION = "0.1.3"
2
+ VERSION = "0.1.4"
3
3
  end
data/test/dummy/Gemfile CHANGED
@@ -1,6 +1,4 @@
1
1
  source "http://rubygems.org"
2
2
 
3
3
  gem "sinatra"
4
- gem "magick_title", "0.1.2"
5
-
6
- #:path => File.expand_path("../../../", __FILE__)
4
+ gem "magick_title", :path => File.expand_path("../../../", __FILE__)
data/test/helper.rb CHANGED
@@ -3,4 +3,15 @@ ENV["environment"] = "test"
3
3
  require 'test/unit'
4
4
  #require 'rack/test'
5
5
  require 'shoulda'
6
- require 'magick_title'
6
+ require 'magick_title'
7
+ require 'fileutils'
8
+
9
+ class Test::Unit::TestCase
10
+
11
+ def setup
12
+ super
13
+ MagickTitle.options[:root] = File.expand_path("../dummy", __FILE__)
14
+ FileUtils.rm_r MagickTitle.options.destination if Dir.exists?(MagickTitle.options.destination)
15
+ end
16
+
17
+ end
@@ -0,0 +1,194 @@
1
+ require 'helper'
2
+
3
+ class TestImage < Test::Unit::TestCase
4
+
5
+ def assert_opening_tag(html, tag, inline=false)
6
+ assert html.match(Regexp.new("#{'^' unless inline}<#{tag}\s?")), "#{tag} opening tag"
7
+ end
8
+
9
+ def assert_closing_tag(html, tag, inline=false)
10
+ assert html.match(Regexp.new("</#{tag}>#{'$' unless inline}")), "#{tag} closing tag"
11
+ end
12
+
13
+ def assert_self_closing_tag(html)
14
+ assert html.match(/\/>$/), "Has self closing tag"
15
+ end
16
+
17
+ should "create an instance of MagickTitle::Image" do
18
+ @title = MagickTitle::Image.create("create using class method")
19
+ end
20
+
21
+ context "an invalid title" do
22
+
23
+ setup do
24
+ @title = MagickTitle::Image.new("")
25
+ end
26
+
27
+ should "not allow empty string" do
28
+ assert !@title.valid?
29
+ assert !@title.save
30
+ assert !@title.fullpath
31
+ end
32
+
33
+ should "not allow update" do
34
+ assert !@title.update("")
35
+ assert !@title.save
36
+ assert !@title.fullpath
37
+ end
38
+
39
+ should "allow update to valid title" do
40
+ assert @title.update("Hello!")
41
+ assert @title.save
42
+ assert File.exists?(@title.fullpath)
43
+ end
44
+
45
+ end
46
+
47
+ context "a valid title" do
48
+
49
+ setup do
50
+ @title = MagickTitle::Image.new("hello!")
51
+ end
52
+
53
+ should "save and create an image" do
54
+ assert @title.save
55
+ assert @title.fullpath.match(/\.png$/)
56
+ assert File.exists?(@title.fullpath)
57
+ end
58
+
59
+ should "delete it's image" do
60
+ assert @title.save
61
+ assert @title.delete
62
+ assert !File.exists?(@title.fullpath)
63
+ end
64
+
65
+ should "downcase the image tag text" do
66
+ html = @title.to_html(:id => "crazy-test-id", :class => "span-12 last", :alt => "Custom Alt Tags, Yo!", :parent => nil)
67
+ assert_opening_tag html, 'img'
68
+ assert html.match(/id="crazy-test-id"/)
69
+ assert html.match(/class="span-12\slast"/)
70
+ assert html.match(/alt="Custom\sAlt\sTags\,\sYo\!"/)
71
+ assert_self_closing_tag html
72
+ end
73
+
74
+ end
75
+
76
+ context "an existing title" do
77
+
78
+ setup do
79
+ @title = MagickTitle::Image.create("hello!")
80
+ end
81
+
82
+ should "cache when asked to" do
83
+ # make sure cache is turned ON
84
+ assert @title.options.cache
85
+ mod = File::mtime(@title.fullpath)
86
+
87
+ # delay for timestamp..
88
+ sleep 1
89
+
90
+ # make sure we really don't need to update
91
+ assert !@title.dirty?
92
+ @title.save
93
+
94
+ mod2 = File::mtime(@title.fullpath)
95
+ assert_equal mod, mod2
96
+ end
97
+
98
+ should "not cache when asked to" do
99
+ # make sure cache is turned OFF
100
+ @title.options[:cache] = false
101
+ assert !@title.options.cache
102
+ mod = File::mtime(@title.fullpath)
103
+
104
+ # delay for timestamp..
105
+ sleep 1
106
+
107
+ # make sure we really don't need to update
108
+ assert !@title.dirty?
109
+ @title.save
110
+
111
+ mod2 = File::mtime(@title.fullpath)
112
+ assert mod != mod2
113
+ end
114
+
115
+ should "create an html img tag without a parent element" do
116
+ html = @title.to_html(false)
117
+ assert html.is_a?(String)
118
+ assert_opening_tag html, 'img'
119
+ assert html.match("src=#{@title.url.inspect}"), "Sets src to url"
120
+ assert html.match("alt=#{@title.text.inspect}"), "Sets alt to text"
121
+ assert_self_closing_tag html
122
+ end
123
+
124
+ should "set the parent html container with a string" do
125
+ tag = "h3"
126
+ html = @title.to_html(tag)
127
+ assert_opening_tag html, tag
128
+ assert_closing_tag html, tag
129
+ end
130
+
131
+ should "defaults the parent html container" do
132
+ tag = "h1"
133
+ html = @title.to_html(:parent => { :id => "custom_id" })
134
+ assert_opening_tag html, tag
135
+ assert_opening_tag html, 'img', true #inline img tag
136
+ assert_closing_tag html, tag
137
+ end
138
+
139
+ should "use a different parent container" do
140
+ tag = "div"
141
+ html = @title.to_html(tag)
142
+ assert html.match(/<div></)
143
+ end
144
+
145
+ should "customize the parent html container" do
146
+ tag = "div"
147
+ html = @title.to_html(:parent => { :tag => tag, :id => "custom_id", :class => "some-class" })
148
+ assert_opening_tag html, tag
149
+ assert html.match(/id="custom_id"/)
150
+ assert html.match(/class="some-class"/)
151
+ assert_closing_tag html, tag
152
+ end
153
+
154
+ should "customize the image tag" do
155
+ html = @title.to_html(:id => "crazy-test-id", :class => "span-12 last", :alt => "Custom Alt Tags, Yo!", :parent => nil)
156
+ assert_opening_tag html, 'img'
157
+ assert html.match(/id="crazy-test-id"/)
158
+ assert html.match(/class="span-12\slast"/)
159
+ assert html.match(/alt="Custom\sAlt\sTags\,\sYo\!"/)
160
+ assert_self_closing_tag html
161
+ end
162
+
163
+ end
164
+
165
+ context "titles with the same text" do
166
+
167
+ setup do
168
+ @title1 = MagickTitle::Image.create("hello!")
169
+ @title2 = MagickTitle::Image.create("hello!", :color => '#000')
170
+ @title3 = MagickTitle::Image.create("hello!", :color => '#000', :font_size => 16)
171
+ @title4 = MagickTitle::Image.create("hello!", :size => 16)
172
+ @title5 = MagickTitle::Image.create("HELLO!")
173
+ end
174
+
175
+ should "each have uniq filenames" do
176
+ assert @title1.filename != @title2.filename
177
+ assert @title2.filename != @title3.filename
178
+ assert @title3.filename != @title4.filename
179
+ assert @title4.filename != @title5.filename
180
+ assert @title5.filename != @title1.filename
181
+ end
182
+
183
+ end
184
+
185
+ context "a title with long text" do
186
+
187
+ should "truncate filename" do
188
+ @title = MagickTitle::Image.create("Quisque commodo hendrerit lorem quis egestas. Maecenas quis tortor arcu. Vivamus rutrum nunc non neque consectetur quis placerat neque lobortis. Nam vestibulum, arcu sodales feugiat consectetur, nisl orci bibendum elit, eu euismod magna sapien ut nibh. Donec semper quam scelerisque tortor dictum gravida. In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien. Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget, scelerisque a libero. Morbi eu porttitor ipsum. Nullam lorem nisi, posuere quis volutpat eget, luctus nec massa. Pellentesque aliquam lacinia tellus sit amet bibendum. Ut posuere justo in enim pretium scelerisque. Etiam ornare vehicula euismod. Vestibulum at.")
189
+ assert @title.filename.length < 100
190
+ end
191
+
192
+ end
193
+
194
+ end
@@ -1,34 +1,14 @@
1
1
  require 'helper'
2
2
 
3
- class TestImage < Test::Unit::TestCase
3
+ class TestMagickTitle < Test::Unit::TestCase
4
4
 
5
- def setup
6
- MagickTitle.options[:root] = File.expand_path("../dummy", __FILE__)
7
- end
8
-
9
-
10
- should "not allow empty string" do
11
- @title = MagickTitle::Image.new("")
12
- assert !@title.valid?
13
- assert !@title.save
14
- end
15
-
16
-
17
- context "a valid image title" do
5
+ should "create an instance of MagickTitle::Image" do
18
6
 
19
- setup do
20
- @title = MagickTitle::Image.new("hello")
21
- end
7
+ @title = MagickTitle.say("Hello Magick Title!")
22
8
 
23
- should "allow valid string" do
24
- assert @title.valid?
25
- end
9
+ assert @title.is_a?(MagickTitle::Image)
10
+ assert File.exists?(@title.fullpath)
26
11
 
27
- should "save a valid title" do
28
- assert @title.save
29
- assert File.exists?(@title.fullpath)
30
- end
31
-
32
12
  end
33
13
 
34
14
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: magick_title
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.1.3
5
+ version: 0.1.4
6
6
  platform: ruby
7
7
  authors:
8
8
  - Spencer Steffen
@@ -80,7 +80,7 @@ files:
80
80
  - test/dummy/views/index.erb
81
81
  - test/dummy/views/layout.erb
82
82
  - test/helper.rb
83
- - test/test_image_title.rb
83
+ - test/test_image.rb
84
84
  - test/test_magick_title.rb
85
85
  has_rdoc: true
86
86
  homepage: https://github.com/citrus/magick_title
@@ -125,5 +125,5 @@ test_files:
125
125
  - test/dummy/views/index.erb
126
126
  - test/dummy/views/layout.erb
127
127
  - test/helper.rb
128
- - test/test_image_title.rb
128
+ - test/test_image.rb
129
129
  - test/test_magick_title.rb
@@ -1,114 +0,0 @@
1
- require 'helper'
2
-
3
- class TestImage < Test::Unit::TestCase
4
-
5
- def setup
6
- MagickTitle.options[:root] = File.expand_path("../dummy", __FILE__)
7
- end
8
-
9
- def assert_opening_tag(html, tag, inline=false)
10
- assert html.match(Regexp.new("#{'^' unless inline}<#{tag}\s?")), "#{tag} opening tag"
11
- end
12
-
13
- def assert_closing_tag(html, tag, inline=false)
14
- assert html.match(Regexp.new("</#{tag}>#{'$' unless inline}")), "#{tag} closing tag"
15
- end
16
-
17
- def assert_self_closing_tag(html)
18
- assert html.match(/\/>$/), "Has self closing tag"
19
- end
20
-
21
- context "a valid title" do
22
-
23
- setup do
24
- @title = MagickTitle.say("hello!")
25
- end
26
-
27
- should "return an image title" do
28
- assert @title.is_a?(MagickTitle::Image)
29
- assert @title.fullpath.match(/\.png$/)
30
- assert File.exists?(@title.fullpath)
31
- end
32
-
33
- should "create an html img tag without a parent element" do
34
- html = @title.to_html(false)
35
- assert html.is_a?(String)
36
- assert_opening_tag html, 'img'
37
- assert html.match("src=#{@title.url.inspect}"), "Sets src to url"
38
- assert html.match("alt=#{@title.text.inspect}"), "Sets alt to text"
39
- assert_self_closing_tag html
40
- end
41
-
42
- should "set the parent html container with a string" do
43
- tag = "h3"
44
- html = @title.to_html(tag)
45
- assert_opening_tag html, tag
46
- assert_closing_tag html, tag
47
- end
48
-
49
- should "defaults the parent html container" do
50
- tag = "h1"
51
- html = @title.to_html(:parent => { :id => "custom_id" })
52
- assert_opening_tag html, tag
53
- assert_opening_tag html, 'img', true #inline img tag
54
- assert_closing_tag html, tag
55
- end
56
-
57
- should "use a different parent container" do
58
- tag = "div"
59
- html = @title.to_html(tag)
60
- assert html.match(/<div></)
61
- end
62
-
63
- should "customize the parent html container" do
64
- tag = "div"
65
- html = @title.to_html(:parent => { :tag => tag, :id => "custom_id", :class => "some-class" })
66
- assert_opening_tag html, tag
67
- assert html.match(/id="custom_id"/)
68
- assert html.match(/class="some-class"/)
69
- assert_closing_tag html, tag
70
- end
71
-
72
- should "customize the image tag" do
73
- html = @title.to_html(:id => "crazy-test-id", :class => "span-12 last", :alt => "Custom Alt Tags, Yo!", :parent => nil)
74
- assert_opening_tag html, 'img'
75
- assert html.match(/id="crazy-test-id"/)
76
- assert html.match(/class="span-12\slast"/)
77
- assert html.match(/alt="Custom\sAlt\sTags\,\sYo\!"/)
78
- assert_self_closing_tag html
79
- end
80
-
81
- end
82
-
83
- context "titles with the same text" do
84
-
85
- setup do
86
- @title1 = MagickTitle.say("hello!")
87
- @title2 = MagickTitle.say("hello!", :color => '#000')
88
- @title3 = MagickTitle.say("hello!", :color => '#000', :font_size => 16)
89
- @title4 = MagickTitle.say("hello!", :size => 16)
90
- @title5 = MagickTitle.say("HELLO!")
91
- end
92
-
93
- should "each have uniq filenames" do
94
- assert @title1.filename != @title2.filename
95
- assert @title2.filename != @title3.filename
96
- assert @title3.filename != @title4.filename
97
- assert @title4.filename != @title5.filename
98
- end
99
-
100
- end
101
-
102
- context "a title with long text" do
103
-
104
- setup do
105
- @title = MagickTitle.say("Quisque commodo hendrerit lorem quis egestas. Maecenas quis tortor arcu. Vivamus rutrum nunc non neque consectetur quis placerat neque lobortis. Nam vestibulum, arcu sodales feugiat consectetur, nisl orci bibendum elit, eu euismod magna sapien ut nibh. Donec semper quam scelerisque tortor dictum gravida. In hac habitasse platea dictumst. Nam pulvinar, odio sed rhoncus suscipit, sem diam ultrices mauris, eu consequat purus metus eu velit. Proin metus odio, aliquam eget molestie nec, gravida ut sapien. Phasellus quis est sed turpis sollicitudin venenatis sed eu odio. Praesent eget neque eu eros interdum malesuada non vel leo. Sed fringilla porta ligula egestas tincidunt. Nullam risus magna, ornare vitae varius eget, scelerisque a libero. Morbi eu porttitor ipsum. Nullam lorem nisi, posuere quis volutpat eget, luctus nec massa. Pellentesque aliquam lacinia tellus sit amet bibendum. Ut posuere justo in enim pretium scelerisque. Etiam ornare vehicula euismod. Vestibulum at.")
106
- end
107
-
108
- should "truncate filename" do
109
- assert @title.filename.length < 100
110
- end
111
-
112
- end
113
-
114
- end