has_meta 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -4,7 +4,8 @@
4
4
 
5
5
  Adds convenience methods to extract "meta" (as in http meta) strings from
6
6
  models by using existing fields or lambda/Procs for source data. Result is stripped of html
7
- tags and truncated to length (default 255).
7
+ tags and truncated to length (default 255). If the meta is 'keywords' the result will take extra
8
+ steps to strip white space and remove blank elements (ie. multiple commas)
8
9
 
9
10
  ## Installation
10
11
 
@@ -34,7 +35,7 @@ Or install it yourself as:
34
35
 
35
36
  bp = BlogPost.new(...)
36
37
 
37
- bp.meta_keywords == bp.meta_keywords
38
+ bp.meta_keywords == bp.keywords
38
39
 
39
40
  # if short_description is not blank and less than 255 characters then
40
41
  bp.meta_description == bp.short_description
@@ -47,6 +48,13 @@ Or install it yourself as:
47
48
  sleep 1
48
49
  bp.meta_foo == "Feb 27, 4:36:01 PM" # one second later
49
50
 
51
+ # 'keywords' gets extra special treatment.
52
+ bp.short_description = ',one two, ,three, '
53
+ bp.meta_description == ',one two, ,three, '
54
+
55
+ bp.keywords = ',one two, ,three, '
56
+ bp.meta_keywords == 'one,two,three'
57
+
50
58
  ## Contributing
51
59
 
52
60
  1. Fork it
@@ -1,3 +1,3 @@
1
1
  module HasMeta
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/has_meta.rb CHANGED
@@ -26,6 +26,14 @@ module HasMeta
26
26
  str = str.to_s.strip
27
27
  str.gsub!(' ', ' ')
28
28
  str.gsub!(/<.*?>/, '')
29
+
30
+ if meth.to_s == 'keywords'
31
+ str = str.gsub(/[\s,]+/, ',').
32
+ gsub(/\s+/, ' ').
33
+ gsub(/^,|,$/, '').
34
+ strip
35
+ end
36
+
29
37
  str = ::CGI::unescapeHTML(str)
30
38
  str = (str[0,length - 3] + '...') if str.size > length
31
39
  str
@@ -26,28 +26,38 @@ end
26
26
  class HasMetaTest < Test::Unit::TestCase
27
27
  def setup
28
28
  @widget = Widget.new
29
- @widget.short_description = 'Short Description'
30
- @widget.content = 'Long Description'
31
- @widget.keywords = ''
29
+ @widget.short_description = "Short Description"
30
+ @widget.content = "Long Description"
31
+ @widget.keywords = ""
32
32
  @widget.some_ivar = 1
33
33
  end
34
34
 
35
35
  def test_knows_its_meta_description
36
- assert_equal 'Short Description', @widget.meta_description
36
+ assert_equal "Short Description", @widget.meta_description
37
37
  end
38
38
 
39
39
  def test_knows_its_meta_description_when_first_option_blank
40
40
  @widget.short_description = nil
41
- assert_equal 'Long Description', @widget.meta_description
41
+ assert_equal "Long Description", @widget.meta_description
42
42
  end
43
43
 
44
44
  def test_knows_its_truncated_meta_description
45
- assert_equal 'Short...', @widget.meta_description(8)
45
+ assert_equal "Short...", @widget.meta_description(8)
46
46
  end
47
47
 
48
48
  def test_strip_tags
49
49
  @widget.short_description = "<i>ital</i> <b>bold</b> <a href='http://pjkh.com'>pjkh.com</a> the end"
50
- assert_equal 'ital bold pjkh.com the end', @widget.meta_description
50
+ assert_equal "ital bold pjkh.com the end", @widget.meta_description
51
+ end
52
+
53
+ def test_treat_keywords_special
54
+ @widget.keywords = " , , one , , two , three , , "
55
+ assert_equal "one,two,three", @widget.meta_keywords
56
+ end
57
+
58
+ def test_do_not_treat_description_special
59
+ @widget.short_description = " , , one , , two , three , , "
60
+ assert_equal ", , one , , two , three , ,", @widget.meta_description
51
61
  end
52
62
 
53
63
  def test_knows_its_meta_keywords
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_meta
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: