manage_meta 0.0.4 → 0.0.5

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
@@ -4,24 +4,24 @@ ManageMeta
4
4
  ManageMeta is yet another meta tag manager for Rails 3.x. Its features are: recognizes tags
5
5
  should render as HTTP-EQUIV; supports the Google 'canonical' link; is extensible; is non-intrusive
6
6
 
7
- How it works
7
+ How to use it
8
8
  -----------
9
9
 
10
10
  Include the gem by adding this to your Gemfile file
11
11
 
12
12
  `gem "manage_meta", :git => "git://github.com/mikehoward/manage_meta.git"`
13
13
 
14
- Then, in your controller actions, call _add_meta_ for each meta tag you want
14
+ Then, in your controller actions, call *add_meta()* for each meta tag you want
15
15
  to define.
16
16
 
17
17
  `add_meta :author, 'Fred Fink'`
18
18
 
19
- If there are meta tags you don't want to define, you can use _del_meta_ to remove them.
19
+ If there are meta tags you don't want to define, you can use *del_meta()* to remove them.
20
20
  At present there are only two automatically defined: 'robots' and 'generator'. You may
21
- also redefine them by using _add_meta_ to give them new values.
21
+ also redefine them by using *add_meta()* to give them new values.
22
22
 
23
- If there is a meta tag which requires a currently unsupported format, you may add it
24
- using _add_meta_format_.
23
+ If there is a meta tag which requires a currently unsupported format, you may add the
24
+ format using *add_meta_format()*, and then add the tag using *add_meta()*.
25
25
 
26
26
  Finally, edit app/views/layouts/application.html.erb - or equivalent - to insert
27
27
 
@@ -33,21 +33,28 @@ defined meta tags.
33
33
  What it Adds
34
34
  ------------
35
35
 
36
- ManageMeta defines four (4) methods and three (3) instance variables into all classes
36
+ ManageMeta defines six (6) methods and three (3) instance variables into all classes
37
37
  derived from ApplicationController
38
38
 
39
39
  The methods are:
40
40
 
41
- * add_meta - adds a meta tag
42
- * del_meta - which deletes a meta tag
43
- * add_meta_format - which adds a meta tag format
44
- * render_meta - which returns a string rendering all currently defined meta tags.
41
+ * `add_meta()` - adds a meta tag
42
+ * `del_meta()` - which deletes a meta tag
43
+ * `add_meta_format()` - which adds a meta tag format
44
+ * `render_meta()` - which returns a string rendering all currently defined meta tags.
45
+ * `manage_meta_sym_to_name` - returns Content-Length when given :content_length, etc.
46
+ It works with either symbols or strings and strips extra underscores and hyphens
47
+ * `manage_meta_name_to_sym` - returns symbol :foo_bar_baz when given a name of the form 'Foo-Bar-Baz'.
48
+ It also works when given a symbol and strips extra underscores and hyphens.
45
49
 
46
- The instance variables are:
50
+ The instance variables are three hashes:
47
51
 
48
- * @manage_meta_meta_hash - a Hash containing mapping defined meta tag names to content values
49
- * @manage_meta_format_hash - a Hash mapping known meta tag format strings to actual format strings
50
- * @manage_meta_name_to_format - a Hash mapping meta tag name strings to known formats
52
+ * `@manage_meta_meta_hash` - a Hash containing mapping defined meta tag names to content values
53
+ * `@manage_meta_format_hash` - a Hash mapping known meta tag format symbols to actual format strings.
54
+ Contains entries such as `:content_type => :named` and `:content_length => :http_equiv`
55
+ * `@manage_meta_name_to_format` - a Hash mapping meta tag name strings to known formats. Formats
56
+ are initialized to the three symbols: `:named`, `:http_equiv`, and `:canonical` (see below). `add_meta_format()`
57
+ is used to add formats to this hash.
51
58
 
52
59
  The Details
53
60
  --------------
@@ -56,22 +63,24 @@ Here are the ugly details
56
63
 
57
64
  ### Methods in Detail ###
58
65
 
59
- #### add_meta ####
66
+ #### `add_meta()` ####
60
67
 
61
- _add_meta_ accepts values in any of three formats:
68
+ *add_meta()* accepts values in any of three formats:
62
69
 
63
- `add_meta(name, value[, :format => :format_name])`
64
- `add_meta(name, value[, :format => :format_name]) do ... end`
65
- `add_meta(name[, :format => :format_name]) do ... end`
70
+ * `add_meta(name, value[, :format => :format_name])`
71
+ * `add_meta(name, value[, :format => :format_name]) do ... end`
72
+ * `add_meta(name[, :format => :format_name]) do ... end`
66
73
 
67
- * name must be something which responds to 'to_s'. It will be used for the _name_ (or _http-equiv_)
74
+ Arguments:
75
+
76
+ * *name* must be something which responds to 'to_s'. It will be used for the *name* (or *http-equiv*)
68
77
  attribute of the meta tag.
69
- * value must be something which responds to 'to_s' and is not a Hash. Normally it will simply
70
- be a string. If given, it supplies the leading part of the _content_ attribute of the meta tag
71
- * The single option :format must supply an existing key in @manage_meta_format_hash. It is
72
- used to associate a meta tag format with _name_. If not given and not currently defined in
73
- @manage_meta_format_hash, it will be set to _:named_. (see below for details)
74
- * The optional block is evaluated and the return value is used as the second [or only] part
78
+ * *value* must be something which responds to 'to_s' and is not a Hash. Normally it will simply
79
+ be a string. If given, it supplies the leading part of the *content* attribute of the meta tag
80
+ * The single *option* :format must supply an existing key in @manage_meta_format_hash. It is
81
+ used to associate a meta tag format with *name*. If not given and not currently defined
82
+ in `@manage_meta_format_hash`, it will be set to *:named*. (see below for details)
83
+ * The *optional block* is evaluated and the return value is used as the second [or only] part
75
84
  of the _content_ attribute of the meta tag.
76
85
 
77
86
  Three meta tag formats are defined automatically:
@@ -83,50 +92,64 @@ Three meta tag formats are defined automatically:
83
92
  The _@manage_meta_name_to_format_ is populated with entries mapping known HTTP-EQUIV tags
84
93
  and the CANONICAL Google link tag to the correct format.
85
94
 
86
- #### del_meta ####
95
+ #### `del_meta()` ####
87
96
 
88
- _del_meta_ is almost useless. It's there in case you want to get ride of a default tag.
97
+ `del_meta()` is almost useless. It's there in case you want to get ride of a default tag.
89
98
 
90
99
  `del_meta(name)`
91
100
 
92
- where _name_ is something which responds to 'to_s' [or is a string]. If the meta tag is
93
- defined in @manage_meta_meta_hash, then it will be deleted. Nothing bad happens if it isn't.
94
-
95
- #### add_meta_format_ ####
96
-
97
- `add_meta_format(format_name, format_string)`
101
+ If the meta tag is defined in `@manage_meta_meta_hash`, then it will be deleted.
102
+ Nothing bad happens if it isn't.
98
103
 
99
- _format_name__ will be converted to a symbol.
104
+ #### `add_meta_format()` ####
100
105
 
101
- _format_string_ will be the value of @manage_meta_format_hash[_format_name.to_sym_].
106
+ `add_meta_format(format_name, format_string)` adds the `format_string` to
107
+ `@manage_meta_format_hash` under the key `format_name`
102
108
 
103
- It's your responsibility to format the string properly.
109
+ *format_name* will be converted to a symbol using `manage_meta_sym_to_name()`.
104
110
 
105
- _render_meta_ will replace _name_ and _content_ with the string values given for the meta
106
- tag - if present. It is not an error to omit either or both _name_ and/or _content_
111
+ It's your responsibility to format the string properly:
112
+ `render_meta()` will replace `#{name}` and `#{content}` with the string values given for the meta
113
+ tag - if present. It is not an error to omit either or both `#{name}` and/or `#{content}`.
114
+ The value used for `#{name}` result of calling `manage_meta_sym_to_name()` on the meta element key.
107
115
 
108
- #### render_meta ####
116
+ #### render_meta() ####
109
117
 
110
- `render_meta`
118
+ `render_meta()`
111
119
 
112
120
  simply goes through all the defined key, value pairs in @manage_meta_meta_hash and
113
- returns their associated format strings after replacing the _#{name}_ and _#{content}_
121
+ returns their associated format strings after replacing the `#{name}` and `#{content}`
114
122
  symbols with their values.
115
123
 
124
+ `#{name}` is replaced with the meta tag key [as in `:content_type`] passed through
125
+ `manage_meta_sym_to_name()`.
126
+
127
+ `#{value}` is replaced by the value assigned in `@manage_meta_meta_hash`.
128
+
116
129
  ### Instance Variables in Detail ###
117
130
 
118
- #### manage_meta_format_hash ####
131
+ All three hashes use symbols for keys.
132
+
133
+ #### `@manage_meta_format_hash` ####
134
+
135
+ maps format keys to format strings. See `add_meta_format()` above for details
119
136
 
120
137
  keys are symbols,
121
138
 
122
139
  values are strings which are used to render meta tags
123
140
 
124
- #### manage_meta_meta_hash ####
141
+ #### `@manage_meta_meta_hash` ####
142
+
143
+ Maps meta tag `name` keys to values to insert into the `content` field of the meta tag
144
+ element.
145
+
146
+ keys are symbols which are used for the names of meta tags
125
147
 
126
- keys are strings which are used for the names of meta tags
148
+ values are strings
127
149
 
128
- values are symbols which are keys in @manage_meta_format_hash
150
+ #### `@manage_meta_name_to_format` ####
129
151
 
130
- #### manage_meta_name_to_format ####
152
+ Maps meta tag name symbols to meta tag format string symbols. Contains entries
153
+ such as `:content_length => :http_equiv`
131
154
 
132
- keys are strings which map meta tag names to keys in @manage_meta_format_hash
155
+ both keys and values are symbols
@@ -14,9 +14,14 @@ module ManageMeta
14
14
 
15
15
  mod.helper_method :render_meta if mod.respond_to? :helper_method
16
16
 
17
- old_initialize = mod.instance_method :initialize
17
+ begin
18
+ old_initialize = mod.instance_method(:initialize)
19
+ rescue
20
+ old_initialize = nil
21
+ end
18
22
  mod.send(:define_method, :initialize) do |*args, &block|
19
- result = old_initialize.bind(self).call(*args, &block)
23
+ result = old_initialize.bind(self).call(*args, &block) unless old_initialize.nil?
24
+
20
25
  @manage_meta_meta_hash = {}
21
26
 
22
27
  @manage_meta_format_hash = {
@@ -27,23 +32,23 @@ module ManageMeta
27
32
 
28
33
  @manage_meta_name_to_format = {}
29
34
  #-- set up http-equiv meta tags
30
- ['accept', 'accept-charset', 'accept-encoding', 'accept-language', 'accept-ranges',
31
- 'age', 'allow', 'authorization', 'cache-control', 'connecting', 'content-encoding',
32
- 'content-language', 'content-length', 'content-location', 'content-md5', 'content-range',
33
- 'content-type', 'date', 'etag', 'expect', 'expires', 'from', 'host', 'if-match', 'if-modified-since',
34
- 'if-none-match', 'if-range', 'if-unmodified-since', 'last-modified', 'location',
35
- 'max-forwards', 'pragma', 'proxy-authenticate', 'proxy-authorization', 'range', 'referer',
36
- 'retry-after', 'server', 'te', 'trailer', 'transfer-encoding', 'upgrade', 'user-agent',
37
- 'vary', 'via', 'warning', 'www-authenticate', ].each { |name| @manage_meta_name_to_format[name] = :http_equiv }
35
+ [:accept, :accept_charset, :accept_encoding, :accept_language, :accept_ranges,
36
+ :age, :allow, :authorization, :cache_control, :connecting, :content_encoding,
37
+ :content_language, :content_length, :content_location, :content_md5, :content_range,
38
+ :content_type, :date, :etag, :expect, :expires, :from, :host, :if_match, :if_modified_since,
39
+ :if_none_match, :if_range, :if_unmodified_since, :last_modified, :location,
40
+ :max_forwards, :pragma, :proxy_authenticate, :proxy_authorization, :range, :referer,
41
+ :retry_after, :server, :te, :trailer, :transfer_encoding, :upgrade, :user_agent,
42
+ :vary, :via, :warning, :www_authenticate, ].each { |name| @manage_meta_name_to_format[name] = :http_equiv }
38
43
  # set up Google's canonical link tag
39
- ['canonical'].each { |name| @manage_meta_name_to_format[name] = :canonical }
44
+ [:canonical].each { |name| @manage_meta_name_to_format[name] = :canonical }
40
45
  # set up normal meta tags
41
- ['description', 'keywords', 'language', 'robots'].each { |name| @manage_meta_name_to_format[name] = :named }
46
+ [:description, :keywords, :language, :robots].each { |name| @manage_meta_name_to_format[name] = :named }
42
47
 
43
48
  add_meta 'robots', 'index follow'
44
49
  add_meta 'generator', "Rails #{Rails.version}" if defined?(Rails)
45
50
  # add_meta 'canonical', request.fullpath
46
- result
51
+ result || nil
47
52
  end
48
53
  end
49
54
 
@@ -62,7 +67,7 @@ module ManageMeta
62
67
  #--
63
68
  def add_meta(name, opt_value = nil, options = {}, &block)
64
69
  # make sure name is a string
65
- name = name.to_s
70
+ name = manage_meta_name_to_sym name
66
71
 
67
72
  # handle optional nonsense
68
73
  case
@@ -90,7 +95,8 @@ module ManageMeta
90
95
 
91
96
  # if format is explicitly called out or if name is not yet known
92
97
  if options.has_key?(:format)
93
- raise RuntimeError, "Unsuported Format: #{options[:format]}: formats are #{@manage_meta_format_hash.keys.join(',')}" if !@manage_meta_format_hash.has_key?(options[:format].to_sym)
98
+ raise RuntimeError, "Unsuported Format: #{options[:format]}: formats are #{@manage_meta_format_hash.keys.join(',')}" \
99
+ if !@manage_meta_format_hash.has_key?(options[:format].to_sym)
94
100
  @manage_meta_name_to_format[name] = options[:format].to_sym
95
101
  elsif !@manage_meta_name_to_format.has_key?(name)
96
102
  @manage_meta_name_to_format[name] = :named
@@ -103,7 +109,7 @@ module ManageMeta
103
109
  # if _name_ is in @manage_meta_meta_hash, then it will be deleted
104
110
  #--
105
111
  def del_meta(name)
106
- name = name.to_s
112
+ name = manage_meta_name_to_sym name
107
113
  @manage_meta_meta_hash.delete name if @manage_meta_meta_hash.has_key? name
108
114
  end
109
115
 
@@ -125,7 +131,15 @@ module ManageMeta
125
131
  #--
126
132
  def render_meta
127
133
  ' ' + @manage_meta_meta_hash.map do |name, content|
128
- @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', name).sub('#{content}', content)
134
+ @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', manage_meta_sym_to_name(name)).sub('#{content}', content)
129
135
  end.join("\n ") + " \n"
130
136
  end
137
+
138
+ def manage_meta_sym_to_name(sym)
139
+ sym.to_s.split(/[_-]/).map {|x| x.capitalize }.join('-')
140
+ end
141
+
142
+ def manage_meta_name_to_sym(name)
143
+ name.to_s.downcase.gsub(/[-_]+/, '_').to_sym
144
+ end
131
145
  end
@@ -2,12 +2,18 @@ $LOAD_PATH << File.expand_path("../../lib", __FILE__)
2
2
  require 'test/unit'
3
3
  require 'manage_meta'
4
4
 
5
+ class NoToS
6
+ end
7
+ NoToS.send( :undef_method, :to_s )
8
+
9
+ class NoInitializer
10
+ undef_method :initialize if methods.include? :initialize
11
+ include ManageMeta
12
+ end
13
+
5
14
  class ManageMetaTest < Test::Unit::TestCase
6
15
  include ManageMeta
7
16
 
8
- class NoToS ; end
9
- NoToS.send :undef_method, :to_s
10
-
11
17
  # add refute methods for ruby 1.8.7
12
18
  if !self.instance_methods.include? :refute_respond_to
13
19
  def refute_respond_to(obj, func, msg = nil)
@@ -25,6 +31,32 @@ class ManageMetaTest < Test::Unit::TestCase
25
31
  assert_respond_to self, :del_meta, "responds to del_meta()"
26
32
  assert_respond_to self, :add_meta_format, "responds to add_meta_format()"
27
33
  assert_respond_to self, :render_meta, "responds to render_meta()"
34
+ assert_respond_to self, :manage_meta_sym_to_name, "responds to manage_meta_sym_to_name()"
35
+ assert_respond_to self, :manage_meta_name_to_sym, "responds to manage_meta_name_to_sym()"
36
+ end
37
+
38
+ def test_manage_meta_sym_to_name
39
+ {:foo => 'Foo', :foo_bar => "Foo-Bar", :foo_bar_baz => "Foo-Bar-Baz", "Foo-Bar" => "Foo-Bar",
40
+ "Foo_bar" => "Foo-Bar" }.each do |key, val|
41
+ assert_equal manage_meta_sym_to_name(key), val, "manage_meta_sym_to_name(#{key}) == #{val}"
42
+ end
43
+ end
44
+
45
+ def test_manage_meta_name_to_sym
46
+ { 'Foo' => :foo, 'Foo-Bar' => :foo_bar, 'Foo--Bar_Baz' => :foo_bar_baz,
47
+ :foo_bar_baz => :foo_bar_baz, :foo____bar => :foo_bar }.each do |key, val|
48
+ assert_equal manage_meta_name_to_sym(key), val, "manage_meta_name_to_sym(#{key}) == #{val}"
49
+ end
50
+ end
51
+
52
+ def test_works_wo_initialize
53
+ refute NoInitializer.methods.include?(:initialize), "NoInitializer does not have an initialize method"
54
+ foo = nil
55
+ assert_nothing_raised(Exception, "instantiating a class w/o an initialize method should work") do
56
+ foo = NoInitializer.new
57
+ end
58
+ assert foo.instance_of?(NoInitializer), "foo is an instance of NoInitializer"
59
+ refute foo.methods.include?(:initialize), "foo does not have an initialize method"
28
60
  end
29
61
 
30
62
  # add_meta tests
@@ -47,24 +79,24 @@ class ManageMetaTest < Test::Unit::TestCase
47
79
  # test "add_meta adds methods to meta_hash" do
48
80
  def test_add_meta_adds_meta
49
81
  assert_nothing_raised(Exception, "add_meta foo, bar is ok") { add_meta :foo, "bar" }
50
- assert self.instance_variable_get("@manage_meta_meta_hash").key?('foo'),
82
+ assert self.instance_variable_get("@manage_meta_meta_hash").key?(:foo),
51
83
  "meta variable 'foo' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
52
- assert self.instance_variable_get("@manage_meta_meta_hash")['foo'] == 'bar',
84
+ assert self.instance_variable_get("@manage_meta_meta_hash")[:foo] == 'bar',
53
85
  "meta variable 'foo' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
54
86
  assert_nothing_raised(Exception, "add_meta(bar) {'value'}") { add_meta( :bar ) { 'value' }}
55
- assert self.instance_variable_get("@manage_meta_meta_hash").key?('bar'),
87
+ assert self.instance_variable_get("@manage_meta_meta_hash").key?(:bar),
56
88
  "meta variable 'bar' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
57
- assert self.instance_variable_get("@manage_meta_meta_hash")['bar'] == 'value',
89
+ assert self.instance_variable_get("@manage_meta_meta_hash")[:bar] == 'value',
58
90
  "meta variable 'bar' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
59
91
  end
60
92
 
61
93
  # test "add_meta concatenates value and output of block" do
62
94
  def test_add_meta_concats_value_and_block
63
95
  add_meta(:foo, 'arg value') { ' block value' }
64
- assert_equal self.instance_variable_get("@manage_meta_meta_hash")['foo'], 'arg value block value',
96
+ assert_equal self.instance_variable_get("@manage_meta_meta_hash")[:foo], 'arg value block value',
65
97
  "add meta must concatenate value of both arg value and output of block"
66
98
  add_meta(:foo, 'arg value', :format => :canonical) { ' block value' }
67
- assert_equal self.instance_variable_get("@manage_meta_meta_hash")['foo'], 'arg value block value',
99
+ assert_equal self.instance_variable_get("@manage_meta_meta_hash")[:foo], 'arg value block value',
68
100
  "add meta must concatenate value of both arg value and output of block with option present"
69
101
  end
70
102
 
@@ -79,12 +111,12 @@ class ManageMetaTest < Test::Unit::TestCase
79
111
  # test ' del_meta' do
80
112
  def test_del_meta_deletes_meta_tag
81
113
  assert_nothing_raised(Exception, "add_meta foo, bar is ok") { add_meta :foo, "bar" }
82
- assert self.instance_variable_get("@manage_meta_meta_hash").key?('foo'),
114
+ assert self.instance_variable_get("@manage_meta_meta_hash").key?(:foo),
83
115
  "meta variable 'foo' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
84
- assert self.instance_variable_get("@manage_meta_meta_hash")['foo'] == 'bar',
116
+ assert self.instance_variable_get("@manage_meta_meta_hash")[:foo] == 'bar',
85
117
  "meta variable 'foo' not defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
86
118
  del_meta(:foo)
87
- refute self.instance_variable_get("@manage_meta_meta_hash").key?('foo'),
119
+ refute self.instance_variable_get("@manage_meta_meta_hash").key?(:foo),
88
120
  "meta variable 'foo' should not be defined #{self.instance_variable_get('@manage_meta_meta_hash')}"
89
121
  end
90
122
 
@@ -105,11 +137,11 @@ class ManageMetaTest < Test::Unit::TestCase
105
137
 
106
138
  # test 'render_meta' do
107
139
  def test_render_meta_renders_meta
108
- assert_match /name="robots"/, render_meta, "render_meta contains robots meta tag"
109
- assert_match /name="generator"/, render_meta, "render_meta contains generator meta tag" \
140
+ assert_match /name="Robots"/, render_meta, "render_meta contains robots meta tag"
141
+ assert_match /name="Generator"/, render_meta, "render_meta contains generator meta tag" \
110
142
  if defined? Rails
111
143
  add_meta :foo, 'a value'
112
- assert_match /name="foo"/, render_meta, "render_meta contains 'foo' meta tag"
144
+ assert_match /name="Foo"/, render_meta, "render_meta contains 'foo' meta tag"
113
145
  assert_match /content="a value"/, render_meta, "render_meta tag for foo has content 'a value'"
114
146
  end
115
147
 
metadata CHANGED
@@ -1,8 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manage_meta
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 21
4
5
  prerelease:
5
- version: 0.0.4
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 5
10
+ version: 0.0.5
6
11
  platform: ruby
7
12
  authors:
8
13
  - Mike Howard
@@ -10,7 +15,7 @@ autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
17
 
13
- date: 2011-04-13 00:00:00 -06:00
18
+ date: 2011-04-16 00:00:00 -06:00
14
19
  default_executable:
15
20
  dependencies: []
16
21
 
@@ -43,12 +48,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
48
  requirements:
44
49
  - - ">="
45
50
  - !ruby/object:Gem::Version
51
+ hash: 3
52
+ segments:
53
+ - 0
46
54
  version: "0"
47
55
  required_rubygems_version: !ruby/object:Gem::Requirement
48
56
  none: false
49
57
  requirements:
50
58
  - - ">="
51
59
  - !ruby/object:Gem::Version
60
+ hash: 3
61
+ segments:
62
+ - 0
52
63
  version: "0"
53
64
  requirements: []
54
65