manage_meta 0.0.5 → 0.0.6

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
@@ -33,21 +33,26 @@ defined meta tags.
33
33
  What it Adds
34
34
  ------------
35
35
 
36
- ManageMeta defines six (6) methods and three (3) instance variables into all classes
36
+ ManageMeta defines seven (7) methods and three (3) instance variables into all classes
37
37
  derived from ApplicationController
38
38
 
39
- The methods are:
39
+ The public methods are:
40
40
 
41
41
  * `add_meta()` - adds a meta tag
42
42
  * `del_meta()` - which deletes a meta tag
43
43
  * `add_meta_format()` - which adds a meta tag format
44
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.
45
+
46
+ private methods:
47
+
48
+ * `_manage_meta_init` - initializes required instance variables. `_manage_meta_init` is
49
+ called automatically from the public methods
50
+ * `_manage_meta_sym_to_name` - returns Content-Length when given :content_length, etc.
46
51
  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'.
52
+ * `_manage_meta_name_to_sym` - returns symbol :foo_bar_baz when given a name of the form 'Foo-Bar-Baz'.
48
53
  It also works when given a symbol and strips extra underscores and hyphens.
49
54
 
50
- The instance variables are three hashes:
55
+ The instance variables are three hashes. None have readers or writers:
51
56
 
52
57
  * `@manage_meta_meta_hash` - a Hash containing mapping defined meta tag names to content values
53
58
  * `@manage_meta_format_hash` - a Hash mapping known meta tag format symbols to actual format strings.
@@ -106,12 +111,12 @@ Nothing bad happens if it isn't.
106
111
  `add_meta_format(format_name, format_string)` adds the `format_string` to
107
112
  `@manage_meta_format_hash` under the key `format_name`
108
113
 
109
- *format_name* will be converted to a symbol using `manage_meta_sym_to_name()`.
114
+ *format_name* will be converted to a symbol using `_manage_meta_sym_to_name()`.
110
115
 
111
116
  It's your responsibility to format the string properly:
112
117
  `render_meta()` will replace `#{name}` and `#{content}` with the string values given for the meta
113
118
  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.
119
+ The value used for `#{name}` result of calling `_manage_meta_sym_to_name()` on the meta element key.
115
120
 
116
121
  #### render_meta() ####
117
122
 
@@ -122,7 +127,7 @@ returns their associated format strings after replacing the `#{name}` and `#{con
122
127
  symbols with their values.
123
128
 
124
129
  `#{name}` is replaced with the meta tag key [as in `:content_type`] passed through
125
- `manage_meta_sym_to_name()`.
130
+ `_manage_meta_sym_to_name()`.
126
131
 
127
132
  `#{value}` is replaced by the value assigned in `@manage_meta_meta_hash`.
128
133
 
@@ -1,55 +1,33 @@
1
1
  module ManageMeta
2
- def self.included(mod)
3
- # OK this is a hack based on section 25.4 of Programming Ruby 1.9 by Dave Thomas
4
- # we are using the _included_ hook from Ruby's Module module to run some code
5
- # which replaces the 'initialize' routine with one which creates our required instance
6
- # data.
7
- # 1. We are saving the original 'initialize' as old_initialize.
8
- # 2. we execute the private method 'define_method' via mod.send which has the side effect
9
- # of carrying the definition of 'old_initialize' into the closure.
10
- # 3. we have to bind 'old_initialize' to the run-time value of 'self' because it is an unbound
11
- # method and 'self' will have the right value when it is run in the context of 'mod' creating
12
- # an instance
13
- # 4. we then define our instance variables so that everything will work properly
2
+ private
3
+ #- initialize instance variables
4
+ def _manage_meta_init
5
+ return if @manage_meta_meta_hash.instance_of? Hash
6
+ @manage_meta_meta_hash = {}
14
7
 
15
- mod.helper_method :render_meta if mod.respond_to? :helper_method
8
+ @manage_meta_format_hash = {
9
+ :named => '<meta name="#{name}" content="#{content}" char-encoding="utf-8" />',
10
+ :http_equiv => '<meta http-equiv="#{name}" content="#{content}" char-encoding="utf-8" />',
11
+ :canonical => '<link rel="canonical" href="#{content}" />',
12
+ }
16
13
 
17
- begin
18
- old_initialize = mod.instance_method(:initialize)
19
- rescue
20
- old_initialize = nil
21
- end
22
- mod.send(:define_method, :initialize) do |*args, &block|
23
- result = old_initialize.bind(self).call(*args, &block) unless old_initialize.nil?
14
+ @manage_meta_name_to_format = {}
15
+ #-- set up http-equiv meta tags
16
+ [:accept, :accept_charset, :accept_encoding, :accept_language, :accept_ranges,
17
+ :age, :allow, :authorization, :cache_control, :connecting, :content_encoding,
18
+ :content_language, :content_length, :content_location, :content_md5, :content_range,
19
+ :content_type, :date, :etag, :expect, :expires, :from, :host, :if_match, :if_modified_since,
20
+ :if_none_match, :if_range, :if_unmodified_since, :last_modified, :location,
21
+ :max_forwards, :pragma, :proxy_authenticate, :proxy_authorization, :range, :referer,
22
+ :retry_after, :server, :te, :trailer, :transfer_encoding, :upgrade, :user_agent,
23
+ :vary, :via, :warning, :www_authenticate, ].each { |name| @manage_meta_name_to_format[name] = :http_equiv }
24
+ # set up Google's canonical link tag
25
+ [:canonical].each { |name| @manage_meta_name_to_format[name] = :canonical }
26
+ # set up normal meta tags
27
+ [:description, :keywords, :language, :robots].each { |name| @manage_meta_name_to_format[name] = :named }
24
28
 
25
- @manage_meta_meta_hash = {}
26
-
27
- @manage_meta_format_hash = {
28
- :named => '<meta name="#{name}" content="#{content}" char-encoding="utf-8" />',
29
- :http_equiv => '<meta http-equiv="#{name}" content="#{content}" char-encoding="utf-8" />',
30
- :canonical => '<link rel="canonical" href="#{content}" />',
31
- }
32
-
33
- @manage_meta_name_to_format = {}
34
- #-- set up http-equiv meta tags
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 }
43
- # set up Google's canonical link tag
44
- [:canonical].each { |name| @manage_meta_name_to_format[name] = :canonical }
45
- # set up normal meta tags
46
- [:description, :keywords, :language, :robots].each { |name| @manage_meta_name_to_format[name] = :named }
47
-
48
- add_meta 'robots', 'index follow'
49
- add_meta 'generator', "Rails #{Rails.version}" if defined?(Rails)
50
- # add_meta 'canonical', request.fullpath
51
- result || nil
52
- end
29
+ add_meta 'robots', 'index follow'
30
+ add_meta 'generator', "Rails #{Rails.version}" if defined?(Rails)
53
31
  end
54
32
 
55
33
  #--
@@ -66,8 +44,10 @@ module ManageMeta
66
44
  # all other options keys are ignored
67
45
  #--
68
46
  def add_meta(name, opt_value = nil, options = {}, &block)
47
+ _manage_meta_init unless @manage_meta_meta_hash.instance_of? Hash
48
+
69
49
  # make sure name is a string
70
- name = manage_meta_name_to_sym name
50
+ name = _manage_meta_name_to_sym name
71
51
 
72
52
  # handle optional nonsense
73
53
  case
@@ -109,7 +89,9 @@ module ManageMeta
109
89
  # if _name_ is in @manage_meta_meta_hash, then it will be deleted
110
90
  #--
111
91
  def del_meta(name)
112
- name = manage_meta_name_to_sym name
92
+ _manage_meta_init unless @manage_meta_meta_hash.instance_of? Hash
93
+
94
+ name = _manage_meta_name_to_sym name
113
95
  @manage_meta_meta_hash.delete name if @manage_meta_meta_hash.has_key? name
114
96
  end
115
97
 
@@ -119,6 +101,8 @@ module ManageMeta
119
101
  # adds the format _format_ to @manage_meta_format_hash using the key _key_
120
102
  #--
121
103
  def add_meta_format(key, format)
104
+ _manage_meta_init unless @manage_meta_meta_hash.instance_of? Hash
105
+
122
106
  key = key.to_sym
123
107
  @manage_meta_format_hash[key] = format
124
108
  end
@@ -130,16 +114,19 @@ module ManageMeta
130
114
  # using their name-specific formats and indented two spaces.
131
115
  #--
132
116
  def render_meta
117
+ _manage_meta_init unless @manage_meta_meta_hash.instance_of? Hash
118
+
133
119
  ' ' + @manage_meta_meta_hash.map do |name, content|
134
- @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', manage_meta_sym_to_name(name)).sub('#{content}', content)
120
+ @manage_meta_format_hash[@manage_meta_name_to_format[name]].sub('#{name}', _manage_meta_sym_to_name(name)).sub('#{content}', content)
135
121
  end.join("\n ") + " \n"
136
122
  end
137
123
 
138
- def manage_meta_sym_to_name(sym)
124
+ private
125
+ def _manage_meta_sym_to_name(sym)
139
126
  sym.to_s.split(/[_-]/).map {|x| x.capitalize }.join('-')
140
127
  end
141
128
 
142
- def manage_meta_name_to_sym(name)
129
+ def _manage_meta_name_to_sym(name)
143
130
  name.to_s.downcase.gsub(/[-_]+/, '_').to_sym
144
131
  end
145
132
  end
@@ -6,11 +6,6 @@ class NoToS
6
6
  end
7
7
  NoToS.send( :undef_method, :to_s )
8
8
 
9
- class NoInitializer
10
- undef_method :initialize if methods.include? :initialize
11
- include ManageMeta
12
- end
13
-
14
9
  class ManageMetaTest < Test::Unit::TestCase
15
10
  include ManageMeta
16
11
 
@@ -25,38 +20,53 @@ class ManageMetaTest < Test::Unit::TestCase
25
20
  end
26
21
  end
27
22
 
23
+ def test__manage_meta_init
24
+ refute self.instance_variables.map {|x| x.to_sym }.include?(:@manage_meta_meta_hash), "self does not contain @manage_meta_meta_hash"
25
+ self.send(:_manage_meta_init)
26
+ assert self.instance_variables.map {|x| x.to_sym }.include?(:@manage_meta_meta_hash), "self contains @manage_meta_meta_hash"
27
+ end
28
+
29
+ def test__manage_meta_init_is_itempotent
30
+ self.send(:_manage_meta_init)
31
+ h = {}
32
+ self.instance_variables.grep(/manage_meta/).each do |name|
33
+ h[name] = self.instance_variable_get name
34
+ end
35
+ self.send(:_manage_meta_init)
36
+ assert h.keys.sort == self.instance_variables.grep(/manage_meta/).sort, "no manage_meta instance variables added or removed"
37
+ h.each do |name, val|
38
+ assert h[name] == self.instance_variable_get(name), "instance variable #{name} has not changed"
39
+ end
40
+ end
41
+
28
42
  # test 'existence of add_meta method' do
29
43
  def test_methods_exist
30
44
  assert_respond_to self, :add_meta, "responds to add_meta()"
31
45
  assert_respond_to self, :del_meta, "responds to del_meta()"
32
46
  assert_respond_to self, :add_meta_format, "responds to add_meta_format()"
33
47
  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()"
48
+ if RUBY_VERSION =~ /^1\.9/
49
+ refute_respond_to self, :_manage_meta_init, "does not respond to _manage_meta_init()"
50
+ refute_respond_to self, :_manage_meta_sym_to_name, "does not respond to _manage_meta_sym_to_name()"
51
+ refute_respond_to self, :_manage_meta_name_to_sym, "does not respond to _manage_meta_name_to_sym()"
52
+ end
53
+ assert self.private_methods.grep(/_manage_meta_init/), "has private method _manage_meta_init"
54
+ assert self.private_methods.grep(/_manage_meta_name_to_sym/), "has private method _manage_meta_name_to_sym"
55
+ assert self.private_methods.grep(/_manage_meta_sym_to_name/), "has private method _manage_meta_sym_to_name"
36
56
  end
37
57
 
38
- def test_manage_meta_sym_to_name
58
+ def test__manage_meta_sym_to_name
39
59
  {:foo => 'Foo', :foo_bar => "Foo-Bar", :foo_bar_baz => "Foo-Bar-Baz", "Foo-Bar" => "Foo-Bar",
40
60
  "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}"
61
+ assert_equal _manage_meta_sym_to_name(key), val, "_manage_meta_sym_to_name(#{key}) == #{val}"
42
62
  end
43
63
  end
44
64
 
45
- def test_manage_meta_name_to_sym
65
+ def test__manage_meta_name_to_sym
46
66
  { 'Foo' => :foo, 'Foo-Bar' => :foo_bar, 'Foo--Bar_Baz' => :foo_bar_baz,
47
67
  :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
68
+ assert_equal _manage_meta_name_to_sym(key), val, "_manage_meta_name_to_sym(#{key}) == #{val}"
57
69
  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"
60
70
  end
61
71
 
62
72
  # add_meta tests
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manage_meta
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 5
10
- version: 0.0.5
9
+ - 6
10
+ version: 0.0.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Mike Howard
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-04-16 00:00:00 -06:00
18
+ date: 2011-04-17 00:00:00 -06:00
19
19
  default_executable:
20
20
  dependencies: []
21
21