has_meta 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in has_meta.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Philip Hallstrom
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ # HasMeta
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/has_meta.png)](http://badge.fury.io/rb/has_meta)
4
+
5
+ Adds convenience methods to extract "meta" (as in http meta) strings from
6
+ models by using existing fields for source data. Strings are stripped of html
7
+ tags and truncated to length (default 255).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ gem 'has_meta'
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install has_meta
22
+
23
+ ## Usage
24
+
25
+ class BlogPost < ActiveRecord::Base
26
+ has_meta :keywords => :keywords, :description => [:short_description, :content]
27
+ end
28
+
29
+ bp = BlogPost.new(...)
30
+
31
+ bp.meta_keywords == bp.meta_keywords
32
+
33
+ # if short_description is not blank and less than 255 characters then
34
+ bp.meta_description == bp.short_description
35
+
36
+ # if short_description is blank then
37
+ bp.meta_description == bp.content.slice(0,255)
38
+
39
+ ## Contributing
40
+
41
+ 1. Fork it
42
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
43
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
44
+ 4. Push to the branch (`git push origin my-new-feature`)
45
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/testtask'
4
+ Rake::TestTask.new(:test) do |test|
5
+ test.libs << 'lib'
6
+ test.libs << 'test'
7
+ test.pattern = 'test/**/test_*.rb'
8
+ test.verbose = true
9
+ end
data/has_meta.gemspec ADDED
@@ -0,0 +1,19 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'has_meta/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "has_meta"
8
+ gem.version = HasMeta::VERSION
9
+ gem.authors = ["Philip Hallstrom"]
10
+ gem.email = ["philip@pjkh.com"]
11
+ gem.description = %q{Adds convenience methods to extract "meta" (as in http meta) strings from models.}
12
+ gem.summary = %q{Adds convenience methods to extract "meta" (as in http meta) strings from models by using existing fields for source data. Strings are stripped of html tags and truncated to length (default 255).}
13
+ gem.homepage = "https://github.com/phallstrom/has_meta"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+ end
@@ -0,0 +1,3 @@
1
+ module HasMeta
2
+ VERSION = "0.0.1"
3
+ end
data/lib/has_meta.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "cgi"
2
+ require "has_meta/version"
3
+
4
+ module HasMeta
5
+ module Extensions
6
+ def self.included(base)
7
+ base.extend(ClassMethods)
8
+ end
9
+
10
+ module ClassMethods
11
+ def has_meta(options = {})
12
+ options.each_pair do |meth, fields|
13
+ define_method("meta_#{meth}") {|*args|
14
+ length = args.first if args.is_a? Array
15
+ length ||= 255
16
+ field = [*fields].detect{|f| send(f).present?}
17
+ return nil if field.nil?
18
+ str = send(field).to_s.strip
19
+ str.gsub!('&nbsp;', ' ')
20
+ str.gsub!(/<.*?>/, '')
21
+ str = ::CGI::unescapeHTML(str)
22
+ str = (str[0,length - 3] + '...') if str.size > length
23
+ str
24
+ }
25
+ end
26
+ end
27
+ end
28
+
29
+ end
30
+ end
31
+
32
+ ActiveModel::AttributeMethods.send :include, HasMeta::Extensions
@@ -0,0 +1,48 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
4
+ require 'active_model'
5
+ require 'action_controller'
6
+ require 'has_meta'
7
+
8
+ class Widget
9
+ include ActiveModel::AttributeMethods
10
+ include HasMeta::Extensions
11
+ attr_accessor :short_description, :content, :keywords
12
+ has_meta :description => [:short_description, :content], :keywords => :keywords
13
+ end
14
+
15
+ ################################################################################
16
+
17
+
18
+ class HasMetaTest < Test::Unit::TestCase
19
+ def setup
20
+ @widget = Widget.new
21
+ @widget.short_description = 'Short Description'
22
+ @widget.content = 'Long Description'
23
+ @widget.keywords = ''
24
+ end
25
+
26
+ def test_knows_its_meta_description
27
+ assert_equal 'Short Description', @widget.meta_description
28
+ end
29
+
30
+ def test_knows_its_meta_description_when_first_option_blank
31
+ @widget.short_description = nil
32
+ assert_equal 'Long Description', @widget.meta_description
33
+ end
34
+
35
+ def test_knows_its_truncated_meta_description
36
+ assert_equal 'Short...', @widget.meta_description(8)
37
+ end
38
+
39
+ def test_strip_tags
40
+ @widget.short_description = "<i>ital</i> <b>bold</b> <a href='http://pjkh.com'>pjkh.com</a> the end"
41
+ assert_equal 'ital bold pjkh.com the end', @widget.meta_description
42
+ end
43
+
44
+ def test_knows_its_meta_keywords
45
+ assert_equal nil, @widget.meta_keywords
46
+ end
47
+
48
+ end
metadata ADDED
@@ -0,0 +1,58 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_meta
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Philip Hallstrom
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-01-24 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Adds convenience methods to extract "meta" (as in http meta) strings
15
+ from models.
16
+ email:
17
+ - philip@pjkh.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - has_meta.gemspec
28
+ - lib/has_meta.rb
29
+ - lib/has_meta/version.rb
30
+ - test/test_has_meta.rb
31
+ homepage: https://github.com/phallstrom/has_meta
32
+ licenses: []
33
+ post_install_message:
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ none: false
39
+ requirements:
40
+ - - ! '>='
41
+ - !ruby/object:Gem::Version
42
+ version: '0'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ none: false
45
+ requirements:
46
+ - - ! '>='
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ requirements: []
50
+ rubyforge_project:
51
+ rubygems_version: 1.8.24
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: Adds convenience methods to extract "meta" (as in http meta) strings from
55
+ models by using existing fields for source data. Strings are stripped of html tags
56
+ and truncated to length (default 255).
57
+ test_files:
58
+ - test/test_has_meta.rb