metaa 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,6 +4,8 @@ require "metaa/version"
4
4
  require 'metaa/tag'
5
5
  require 'metaa/tag_collection'
6
6
  require 'metaa/view_helper'
7
+ require 'metaa/definition'
8
+ require 'metaa/meta'
7
9
 
8
10
  module Metaa
9
11
  end
@@ -0,0 +1,20 @@
1
+ module Metaa
2
+ class Definition
3
+ attr_reader :attributes
4
+
5
+ def initialize(attributes = {})
6
+ @attributes = attributes
7
+ end
8
+
9
+ def attributes_for(object)
10
+ object_attributes = {}
11
+
12
+ attributes.each do |attr, val|
13
+ object_val = val.respond_to?(:call) ? val.call(object) : val.to_s
14
+ object_attributes[attr] = object_val
15
+ end
16
+
17
+ object_attributes
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ module Metaa
2
+ class Meta
3
+ class << self
4
+ def meta(attributes = {})
5
+ definitions << Definition.new(attributes)
6
+ end
7
+
8
+ def definitions
9
+ @definitions ||= []
10
+ end
11
+ end
12
+
13
+ attr_reader :object
14
+
15
+ def initialize(object)
16
+ @object = object
17
+ end
18
+
19
+ def tags
20
+ @tag_collection ||= TagCollection.new(
21
+ self.class.definitions.map do |definition|
22
+ definition.attributes_for(object)
23
+ end
24
+ )
25
+
26
+ @tag_collection.html
27
+ end
28
+ end
29
+ end
@@ -1,3 +1,3 @@
1
1
  module Metaa
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ module Metaa
4
+ describe Definition do
5
+ describe '#attributes_for' do
6
+ it 'returns calculated attributes for an object' do
7
+ definition = Definition.new({
8
+ name: 'title',
9
+ property: :text,
10
+ content: ->(object){ object.title }
11
+ })
12
+
13
+ object_mock = double title: 'a title'
14
+
15
+ expect(definition.attributes_for(object_mock)).to eq({
16
+ name: 'title',
17
+ property: 'text',
18
+ content: 'a title'
19
+ })
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ module Metaa
4
+ describe Meta do
5
+ describe 'class methods' do
6
+ describe '#meta' do
7
+ it 'defines a meta tag' do
8
+ klass = Class.new(Meta)
9
+ block = ->(object){ object.title }
10
+
11
+ klass.class_eval do
12
+ meta name: 'title',
13
+ property: :text,
14
+ content: block
15
+ end
16
+
17
+ expect(klass.definitions.length).to eq 1
18
+
19
+ definition = klass.definitions.first
20
+ expect(definition.attributes).to eq({
21
+ name: 'title',
22
+ property: :text,
23
+ content: block
24
+ })
25
+ end
26
+ end
27
+ end
28
+
29
+ describe '#tags' do
30
+ it 'returns meta tags for an object' do
31
+ klass = Class.new(Meta)
32
+
33
+ klass.class_eval do
34
+ meta name: 'title',
35
+ property: :text,
36
+ content: ->(object){ object.title }
37
+ end
38
+
39
+ object_mock = double title: 'a title'
40
+ meta = klass.new(object_mock)
41
+
42
+ expect(meta.tags).to eq "<meta content=\"a title\" name=\"title\" property=\"text\" />"
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,29 +1,30 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Metaa::TagCollection do
4
- describe '#add' do
5
- it 'add a meta object to internal list' do
6
- meta_collection = Metaa::TagCollection.new
7
- meta_collection.add Metaa::Tag.new({name: 'keywords', content: 'abc'})
8
- expect(meta_collection.tags.first.attributes).to eq({name: 'keywords', content: 'abc'})
9
- end
3
+ module Metaa
4
+ describe TagCollection do
5
+ describe '#add' do
6
+ it 'add a meta object to internal list' do
7
+ meta_collection = TagCollection.new
8
+ meta_collection.add Tag.new({name: 'keywords', content: 'abc'})
9
+ expect(meta_collection.tags.first.attributes).to eq({name: 'keywords', content: 'abc'})
10
+ end
10
11
 
11
- it 'add a hash as a new meta object to internal list' do
12
- meta_collection = Metaa::TagCollection.new
13
- meta_collection.add({name: 'keywords', content: 'abc'})
14
- expect(meta_collection.tags.first.attributes).to eq({name: 'keywords', content: 'abc'})
12
+ it 'add a hash as a new meta object to internal list' do
13
+ meta_collection = TagCollection.new
14
+ meta_collection.add({name: 'keywords', content: 'abc'})
15
+ expect(meta_collection.tags.first.attributes).to eq({name: 'keywords', content: 'abc'})
16
+ end
15
17
  end
16
- end
17
18
 
18
- describe '#html' do
19
- it 'converts list of meta objects to html' do
20
- meta_list = [
21
- Metaa::Tag.new({name: 'keywords', content: 'abc'}),
22
- Metaa::Tag.new({name: 'keywords', content: '123, 456'})
23
- ]
24
- meta_collection = Metaa::TagCollection.new(meta_list)
25
- expect(meta_collection.html).to eq "<meta content=\"abc\" name=\"keywords\" /><meta content=\"123, 456\" name=\"keywords\" />"
19
+ describe '#html' do
20
+ it 'converts list of meta objects to html' do
21
+ meta_list = [
22
+ Tag.new({name: 'keywords', content: 'abc'}),
23
+ Tag.new({name: 'keywords', content: '123, 456'})
24
+ ]
25
+ meta_collection = TagCollection.new(meta_list)
26
+ expect(meta_collection.html).to eq "<meta content=\"abc\" name=\"keywords\" /><meta content=\"123, 456\" name=\"keywords\" />"
27
+ end
26
28
  end
27
29
  end
28
-
29
30
  end
@@ -1,8 +1,10 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe Metaa::Tag do
4
- it 'converts a meta tag to html' do
5
- tag = Metaa::Tag.new({name: 'keywords', content: 'abc'})
6
- expect(tag.html).to eq "<meta content=\"abc\" name=\"keywords\" />"
3
+ module Metaa
4
+ describe Tag do
5
+ it 'converts a meta tag to html' do
6
+ tag = Tag.new({name: 'keywords', content: 'abc'})
7
+ expect(tag.html).to eq "<meta content=\"abc\" name=\"keywords\" />"
8
+ end
7
9
  end
8
10
  end
@@ -34,7 +34,6 @@ module Metaa
34
34
 
35
35
  expect(subject.display_meta_tags).to eq "<meta content=\"abc\" name=\"keywords\" />"
36
36
  end
37
-
38
37
  end
39
38
  end
40
39
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: metaa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-10-24 00:00:00.000000000 Z
12
+ date: 2013-10-25 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -137,11 +137,15 @@ files:
137
137
  - README.md
138
138
  - Rakefile
139
139
  - lib/metaa.rb
140
+ - lib/metaa/definition.rb
141
+ - lib/metaa/meta.rb
140
142
  - lib/metaa/tag.rb
141
143
  - lib/metaa/tag_collection.rb
142
144
  - lib/metaa/version.rb
143
145
  - lib/metaa/view_helper.rb
144
146
  - metaa.gemspec
147
+ - spec/metaa/definition_spec.rb
148
+ - spec/metaa/meta_spec.rb
145
149
  - spec/metaa/tag_collection_spec.rb
146
150
  - spec/metaa/tag_spec.rb
147
151
  - spec/metaa/view_helper_spec.rb
@@ -171,6 +175,8 @@ signing_key:
171
175
  specification_version: 3
172
176
  summary: Meta tags with ease
173
177
  test_files:
178
+ - spec/metaa/definition_spec.rb
179
+ - spec/metaa/meta_spec.rb
174
180
  - spec/metaa/tag_collection_spec.rb
175
181
  - spec/metaa/tag_spec.rb
176
182
  - spec/metaa/view_helper_spec.rb