green_monkey 0.1.4 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 71723243b4198578e8829ba40311d49e664eacb9
4
+ data.tar.gz: 864478d04cfb3d38db351637a6b96ab387243296
5
+ SHA512:
6
+ metadata.gz: dff149c920359926f50be0fc9cca4dd1edc7ed91e3c30d0ef274ae66773788bb43eaa8fb426400b0dab066ef06f5bcc2cb1fbcdb5a58119df4c604ea3f201a47
7
+ data.tar.gz: 273e90777f8909d268f511d58c1c8c19aeda0e16b55f0deee778347faca7377042585c13a350fbbb1a25290fbe616afdf1aa2b3292ae6645996693c54d4900f3
data/.gitignore CHANGED
@@ -1,3 +1,5 @@
1
1
  .rvmrc
2
2
  tmp/*.haml
3
- tmp/rails/log
3
+ tmp/rails/log
4
+
5
+ Gemfile.*.lock
data/.rspec CHANGED
@@ -1 +1,3 @@
1
- # just let autotest know about rspec
1
+ --color
2
+ --require spec_helper
3
+ --no-profile
data/.travis.yml CHANGED
@@ -1,9 +1,11 @@
1
1
  script: "bundle exec rake"
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.2
5
6
  gemfile:
6
7
  - Gemfile.rails-3.0
7
8
  - Gemfile.rails-3.1
8
9
  - Gemfile.rails-3.2
10
+ - Gemfile.rails-4.1
9
11
  - Gemfile.haml-edge
data/Gemfile CHANGED
@@ -1,5 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
- gem "mida", git: 'git://github.com/Paxa/mida.git', ref: '5289a3be1'
4
- gem "rails", "3.2.3"
5
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
3
+
4
+ gem "rails", "4.2.0.beta1"
data/Gemfile.haml-edge CHANGED
@@ -1,6 +1,5 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
- gem "haml", git: "https://github.com/nex3/haml.git", ref: "630b8fe2033e6"
4
- gem "mida", git: 'git://github.com/Paxa/mida.git', ref: '7db7149a2cf'
5
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
3
+
4
+ gem "haml", git: "https://github.com/haml/haml.git"
6
5
  gem 'tzinfo'
data/Gemfile.lock CHANGED
@@ -1,138 +1,142 @@
1
- GIT
2
- remote: git://github.com/Paxa/chronic_duration.git
3
- revision: 8c0d0e101bdbaf2b2299f33e7bfa880b0885a017
4
- ref: 8c0d0e101b
5
- specs:
6
- chronic_duration (0.9.6)
7
- numerizer (~> 0.1.1)
8
-
9
- GIT
10
- remote: git://github.com/Paxa/mida.git
11
- revision: 5289a3be11941f4698136fafddd1c2218765181a
12
- ref: 5289a3be1
13
- specs:
14
- mida (0.3.4)
15
- mida_vocabulary (>= 0.1)
16
- nokogiri (>= 1.5)
17
-
18
1
  PATH
19
2
  remote: .
20
3
  specs:
21
- green_monkey (0.1.2)
4
+ green_monkey (0.2.2)
22
5
  chronic_duration
23
6
  haml (>= 3.1.0)
24
- mida_vocabulary (>= 0.1)
7
+ mida_vocabulary (>= 0.2.2)
25
8
 
26
9
  GEM
27
- remote: http://rubygems.org/
10
+ remote: https://rubygems.org/
28
11
  specs:
29
- actionmailer (3.2.3)
30
- actionpack (= 3.2.3)
31
- mail (~> 2.4.4)
32
- actionpack (3.2.3)
33
- activemodel (= 3.2.3)
34
- activesupport (= 3.2.3)
35
- builder (~> 3.0.0)
12
+ actionmailer (4.2.0.beta1)
13
+ actionpack (= 4.2.0.beta1)
14
+ actionview (= 4.2.0.beta1)
15
+ mail (~> 2.5, >= 2.5.4)
16
+ rails-dom-testing (~> 1.0, >= 1.0.2)
17
+ actionpack (4.2.0.beta1)
18
+ actionview (= 4.2.0.beta1)
19
+ activesupport (= 4.2.0.beta1)
20
+ rack (~> 1.6.0.beta)
21
+ rack-test (~> 0.6.2)
22
+ rails-deprecated_sanitizer (~> 1.0, >= 1.0.2)
23
+ rails-dom-testing (~> 1.0, >= 1.0.2)
24
+ actionview (4.2.0.beta1)
25
+ activesupport (= 4.2.0.beta1)
26
+ builder (~> 3.1)
36
27
  erubis (~> 2.7.0)
37
- journey (~> 1.0.1)
38
- rack (~> 1.4.0)
39
- rack-cache (~> 1.2)
40
- rack-test (~> 0.6.1)
41
- sprockets (~> 2.1.2)
42
- activemodel (3.2.3)
43
- activesupport (= 3.2.3)
44
- builder (~> 3.0.0)
45
- activerecord (3.2.3)
46
- activemodel (= 3.2.3)
47
- activesupport (= 3.2.3)
48
- arel (~> 3.0.2)
49
- tzinfo (~> 0.3.29)
50
- activeresource (3.2.3)
51
- activemodel (= 3.2.3)
52
- activesupport (= 3.2.3)
53
- activesupport (3.2.3)
54
- i18n (~> 0.6)
55
- multi_json (~> 1.0)
56
- arel (3.0.2)
57
- blankslate (2.1.2.4)
58
- builder (3.0.0)
59
- diff-lcs (1.1.3)
28
+ rails-deprecated_sanitizer (~> 1.0, >= 1.0.2)
29
+ rails-dom-testing (~> 1.0, >= 1.0.2)
30
+ activejob (4.2.0.beta1)
31
+ globalid (>= 0.2.3)
32
+ activemodel (4.2.0.beta1)
33
+ activesupport (= 4.2.0.beta1)
34
+ builder (~> 3.1)
35
+ activerecord (4.2.0.beta1)
36
+ activemodel (= 4.2.0.beta1)
37
+ activesupport (= 4.2.0.beta1)
38
+ arel (>= 6.0.0.beta1, < 6.1)
39
+ activesupport (4.2.0.beta1)
40
+ i18n (>= 0.7.0.beta1, < 0.8)
41
+ json (~> 1.7, >= 1.7.7)
42
+ minitest (~> 5.1)
43
+ thread_safe (~> 0.1)
44
+ tzinfo (~> 1.1)
45
+ arel (6.0.0.beta1)
46
+ blankslate (3.1.3)
47
+ builder (3.2.2)
48
+ chronic_duration (0.10.6)
49
+ numerizer (~> 0.1.1)
50
+ diff-lcs (1.2.5)
60
51
  erubis (2.7.0)
61
- haml (3.1.4)
62
- hike (1.2.1)
63
- i18n (0.6.0)
64
- journey (1.0.3)
65
- json (1.6.6)
66
- mail (2.4.4)
67
- i18n (>= 0.4.0)
68
- mime-types (~> 1.16)
69
- treetop (~> 1.4.8)
70
- mida_vocabulary (0.1.3)
71
- blankslate
72
- mime-types (1.18)
73
- multi_json (1.2.0)
74
- nokogiri (1.5.2)
52
+ globalid (0.2.3)
53
+ activesupport (>= 4.1.0)
54
+ haml (4.0.5)
55
+ tilt
56
+ hike (1.2.3)
57
+ i18n (0.7.0.beta1)
58
+ json (1.8.1)
59
+ mail (2.6.1)
60
+ mime-types (>= 1.16, < 3)
61
+ mida_vocabulary (0.2.2)
62
+ blankslate (~> 3.1)
63
+ mime-types (2.3)
64
+ mini_portile (0.6.0)
65
+ minitest (5.4.1)
66
+ multi_json (1.10.1)
67
+ nokogiri (1.6.3.1)
68
+ mini_portile (= 0.6.0)
75
69
  numerizer (0.1.1)
76
- polyglot (0.3.3)
77
- rack (1.4.1)
78
- rack-cache (1.2)
79
- rack (>= 0.4)
80
- rack-ssl (1.3.2)
81
- rack
82
- rack-test (0.6.1)
70
+ rack (1.6.0.beta)
71
+ rack-test (0.6.2)
83
72
  rack (>= 1.0)
84
- rails (3.2.3)
85
- actionmailer (= 3.2.3)
86
- actionpack (= 3.2.3)
87
- activerecord (= 3.2.3)
88
- activeresource (= 3.2.3)
89
- activesupport (= 3.2.3)
90
- bundler (~> 1.0)
91
- railties (= 3.2.3)
92
- railties (3.2.3)
93
- actionpack (= 3.2.3)
94
- activesupport (= 3.2.3)
95
- rack-ssl (~> 1.3.2)
73
+ rails (4.2.0.beta1)
74
+ actionmailer (= 4.2.0.beta1)
75
+ actionpack (= 4.2.0.beta1)
76
+ actionview (= 4.2.0.beta1)
77
+ activejob (= 4.2.0.beta1)
78
+ activemodel (= 4.2.0.beta1)
79
+ activerecord (= 4.2.0.beta1)
80
+ activesupport (= 4.2.0.beta1)
81
+ bundler (>= 1.3.0, < 2.0)
82
+ railties (= 4.2.0.beta1)
83
+ sprockets-rails (~> 3.0.0.beta1)
84
+ rails-deprecated_sanitizer (1.0.2)
85
+ activesupport (>= 4.2.0.alpha)
86
+ rails-dom-testing (1.0.2)
87
+ activesupport
88
+ nokogiri (~> 1.6.0)
89
+ rails-deprecated_sanitizer (>= 1.0.1)
90
+ railties (4.2.0.beta1)
91
+ actionpack (= 4.2.0.beta1)
92
+ activesupport (= 4.2.0.beta1)
96
93
  rake (>= 0.8.7)
97
- rdoc (~> 3.4)
98
- thor (~> 0.14.6)
99
- rake (0.9.2.2)
100
- rdoc (3.12)
101
- json (~> 1.4)
102
- rspec (2.9.0)
103
- rspec-core (~> 2.9.0)
104
- rspec-expectations (~> 2.9.0)
105
- rspec-mocks (~> 2.9.0)
106
- rspec-core (2.9.0)
107
- rspec-expectations (2.9.1)
108
- diff-lcs (~> 1.1.3)
109
- rspec-mocks (2.9.0)
110
- rspec-rails (2.9.0)
94
+ thor (>= 0.18.1, < 2.0)
95
+ rake (10.3.2)
96
+ rspec (3.1.0)
97
+ rspec-core (~> 3.1.0)
98
+ rspec-expectations (~> 3.1.0)
99
+ rspec-mocks (~> 3.1.0)
100
+ rspec-core (3.1.2)
101
+ rspec-support (~> 3.1.0)
102
+ rspec-expectations (3.1.0)
103
+ diff-lcs (>= 1.2.0, < 2.0)
104
+ rspec-support (~> 3.1.0)
105
+ rspec-mocks (3.1.0)
106
+ rspec-support (~> 3.1.0)
107
+ rspec-rails (3.1.0)
111
108
  actionpack (>= 3.0)
112
109
  activesupport (>= 3.0)
113
110
  railties (>= 3.0)
114
- rspec (~> 2.9.0)
115
- sprockets (2.1.2)
111
+ rspec-core (~> 3.1.0)
112
+ rspec-expectations (~> 3.1.0)
113
+ rspec-mocks (~> 3.1.0)
114
+ rspec-support (~> 3.1.0)
115
+ rspec-support (3.1.0)
116
+ sprockets (2.12.2)
116
117
  hike (~> 1.2)
118
+ multi_json (~> 1.0)
117
119
  rack (~> 1.0)
118
120
  tilt (~> 1.1, != 1.3.0)
119
- sqlite3 (1.3.5)
120
- thor (0.14.6)
121
- tilt (1.3.3)
122
- treetop (1.4.10)
123
- polyglot
124
- polyglot (>= 0.3.1)
125
- tzinfo (0.3.33)
121
+ sprockets-rails (3.0.0.beta1)
122
+ actionpack (>= 4.0)
123
+ activesupport (>= 4.0)
124
+ sprockets (~> 2.8)
125
+ sqlite3 (1.3.9)
126
+ thor (0.19.1)
127
+ thread_safe (0.3.4)
128
+ tilt (1.4.1)
129
+ tzinfo (1.2.2)
130
+ thread_safe (~> 0.1)
126
131
 
127
132
  PLATFORMS
128
133
  ruby
129
134
 
130
135
  DEPENDENCIES
131
136
  bundler (>= 1.0.0)
132
- chronic_duration!
133
137
  green_monkey!
134
- mida!
135
- rails (= 3.2.3)
136
- rspec (>= 2.7.0)
138
+ nokogiri
139
+ rails (= 4.2.0.beta1)
140
+ rspec (>= 2.9.0)
137
141
  rspec-rails
138
142
  sqlite3
data/Gemfile.rails-3.0 CHANGED
@@ -1,5 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
- gem "rails", "3.0.12"
4
- gem "mida", git: 'git://github.com/Paxa/mida.git', ref: '7db7149a2cf'
5
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
3
+
4
+ gem "rails", "3.0.20"
data/Gemfile.rails-3.1 CHANGED
@@ -1,5 +1,3 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
- gem "rails", "3.1.4"
4
- gem "mida", git: 'git://github.com/Paxa/mida.git', ref: '7db7149a2cf'
5
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
3
+ gem "rails", "3.1.12"
data/Gemfile.rails-3.2 CHANGED
@@ -1,5 +1,4 @@
1
- source :rubygems
1
+ source 'https://rubygems.org'
2
2
  gemspec
3
- gem "rails", "3.2.2"
4
- gem "mida", git: 'git://github.com/Paxa/mida.git', ref: '7db7149a2cf'
5
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
3
+
4
+ gem "rails", "3.2.19"
data/Gemfile.rails-4.1 ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ gem "rails", "4.1.5"
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ## About
4
4
 
5
- This Gem allows you to make html-layout with microdata properties easier. It works with ruby 1.9, rails 3, haml 3.
5
+ This Gem allows you to make html-layout with microdata properties easier. It works with ruby 1.9+, rails 4, rails 3, haml 3.
6
6
 
7
7
  ![Green Monkey](http://upload.wikimedia.org/wikipedia/commons/thumb/b/b1/Bijilo-Portrait-of-a-Callithrix-Monkey.jpg/320px-Bijilo-Portrait-of-a-Callithrix-Monkey.jpg)
8
8
 
@@ -11,8 +11,48 @@ This Gem allows you to make html-layout with microdata properties easier. It wor
11
11
  Add to Gemfile
12
12
 
13
13
  ```ruby
14
- gem "green_monkey", '0.1.2'
15
- gem 'chronic_duration', git: 'git://github.com/Paxa/chronic_duration.git', ref: '8c0d0e101b'
14
+ gem "green_monkey", '0.2.2'
15
+ ```
16
+
17
+ ## API
18
+
19
+ Extends `ActiveModel`:
20
+
21
+ ```ruby
22
+ class Post < ActiveRecord::Base
23
+ html_schema_type :BlogPosting
24
+ end
25
+
26
+ Post.html_schema_type # => Mida::SchemaOrg::BlogPosting
27
+ Post.new.html_schema_type # => Mida::SchemaOrg::BlogPosting
28
+
29
+ class User < ActiveRecord::Base
30
+ html_schema_type "http://example.com/User"
31
+ end
32
+
33
+ User.html_schema_type # => "http://example.com/User"
34
+ ```
35
+
36
+ Extends rails' view helpers:
37
+
38
+ ```ruby
39
+ # add correct support for itemscope attribute
40
+ content_tag(:div, :itemscope) # => "<div itemscope>" (instead of <div "itemscope">
41
+
42
+ # time_tag with datetime attribute
43
+ time_tag(Time.now) # => <time datetime="2014-09-09T09:38:00+00:00">September 09, 2014 09:38</time>
44
+
45
+ # time_tag with microdata-compatible time interval
46
+ time_tag_interval(time, 1.hour + 20.minutes) # => <time datetime="2014-09-09T09:38:00+00:00/PT1H20M">September 09, 2014 09:38 in 1 hour 20 minutes</time>
47
+
48
+ # time interval iso8601 helper
49
+ time_to_iso8601(1.hour + 20.minutes) # => "PT1H20M"
50
+
51
+ # shortcut to build "itemscope" and "itemtype" attributes
52
+ mida_scope(:Airline) # => ' itemscope itemtype="http://schema.org/Airline"'
53
+
54
+ # build a link with itemtype="http://data-vocabulary.org/Breadcrumb"
55
+ breadcrumb_link_to("Home", "/") # => <span itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/" itemprop="url"><span itemprop="title">Home</span></a></span>
16
56
  ```
17
57
 
18
58
  ## Examples
@@ -147,7 +187,3 @@ Output:
147
187
  </ul>
148
188
  </article>
149
189
  ```
150
-
151
- --------
152
-
153
- I use it in [Microdata tools](http://github.com/paxa/semantic_data/ "my own project")
data/green_monkey.gemspec CHANGED
@@ -2,25 +2,26 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = "green_monkey"
5
- s.version = '0.1.4'
5
+ s.version = '0.2.2'
6
6
  s.platform = Gem::Platform::RUBY
7
7
  s.authors = ["Pavel Evstigneev"]
8
8
  s.email = ["pavel.evst@gmail.com"]
9
9
  s.homepage = "https://github.com/paxa/green_monkey"
10
- s.summary = "Rails and Haml microdata layout helpers"
11
- s.description = "It hacks Rails and Haml"
10
+ s.summary = "Rails and Haml microdata toolkit"
11
+ s.description = "Provides useful helpers to map object with microdata types and display object's properties in view"
12
+ s.license = "MIT"
12
13
 
13
14
  s.required_rubygems_version = ">= 1.2.0"
15
+ s.required_ruby_version = '>= 1.9'
14
16
 
15
- #s.add_runtime_dependency 'rails', '>= 3.0.0'
16
17
  s.add_runtime_dependency 'haml', '>= 3.1.0'
17
- s.add_runtime_dependency 'mida_vocabulary', '>= 0.1'
18
+ s.add_runtime_dependency 'mida_vocabulary', '>= 0.2.2'
18
19
  s.add_runtime_dependency 'chronic_duration'
19
-
20
+
20
21
  s.add_development_dependency "bundler", ">= 1.0.0"
21
- s.add_development_dependency "rspec", ">= 2.7.0"
22
+ s.add_development_dependency "rspec", ">= 2.9.0"
22
23
  s.add_development_dependency "rspec-rails"
23
- s.add_development_dependency "mida"
24
+ s.add_development_dependency "nokogiri"
24
25
  s.add_development_dependency 'sqlite3'
25
26
 
26
27
  s.files = `git ls-files`.split("\n")
@@ -1,12 +1,14 @@
1
+ # Add support for "itemscope" html attributes
2
+
1
3
  module ActionView::Helpers::TagHelper
2
4
  # Rails by default does not behave with itemscope as boolean attribute
3
5
  # and renders it as itemscope="true"
4
-
6
+
5
7
  # this changes make it render as itemscope="itemscope"
6
8
  BOOLEAN_ATTRIBUTES.merge(['itemscope', :itemscope])
7
9
 
8
10
  private
9
-
11
+
10
12
  # this hack replaces itemscope="itemscope" => itemscope
11
13
  # to make it follow standarts (http://www.w3.org/TR/microdata/#typed-items)
12
14
  alias_method :tag_options_before_green_monkey, :tag_options
@@ -1,5 +1,8 @@
1
1
  # coding: utf-8
2
-
2
+ # Provides setter and getter for microdata-type
3
+ # ActiveModel.html_schema_type
4
+ # ActiveModel#html_schema_type
5
+ # ActiveModel#html_schema_type(value, [options])
3
6
  module GreenMonkey
4
7
  module ModelHelpers
5
8
  extend ActiveSupport::Concern
@@ -12,10 +15,15 @@ module GreenMonkey
12
15
  def html_schema_type(value = nil, options = {})
13
16
  return @html_schema_type unless value
14
17
 
15
- value = /#{value}/ if value.is_a?(Symbol)
16
- if value.is_a?(Regexp)
17
- value = Mida::Vocabulary.vocabularies.find do |vocabulary|
18
- vocabulary.itemtype.to_s =~ value && vocabulary.itemtype.to_s
18
+ if const = Mida::Vocabulary.try_load_const(value)
19
+ value = const
20
+ else
21
+ value = /#{value}/ if value.is_a?(Symbol)
22
+
23
+ if value.is_a?(Regexp)
24
+ value = Mida::Vocabulary.vocabularies.find do |vocabulary|
25
+ vocabulary.itemtype.to_s =~ value && vocabulary.itemtype.to_s
26
+ end
19
27
  end
20
28
  end
21
29
 
@@ -17,7 +17,7 @@ require "haml"
17
17
  # => <span itemprop="title">Hello</span>
18
18
 
19
19
  class Haml::Buffer
20
-
20
+
21
21
  # this methods calls then you pass
22
22
  # %tag[object1, object2]
23
23
  # ref argument is array
@@ -29,10 +29,10 @@ class Haml::Buffer
29
29
  end
30
30
  options
31
31
  end
32
-
32
+
33
33
  def process_object_ref(obj)
34
34
  return {} if !obj
35
-
35
+
36
36
  if obj.is_a?(Symbol)
37
37
  # symbol => "itemprop" attribute
38
38
  return {'itemprop' => obj.to_s}
@@ -45,20 +45,20 @@ class Haml::Buffer
45
45
  options = {}
46
46
  options['class'] = obj.respond_to?(:haml_object_ref) ? obj.haml_object_ref : underscore(obj.class)
47
47
  options['id'] = "#{options['class']}_#{obj.id || 'new'}" if obj.respond_to?(:id)
48
-
48
+
49
49
  # my hack for microdata attributes
50
50
  if obj.respond_to?(:html_schema_type)
51
51
  options['itemscope'] = true
52
52
  options['itemid'] = obj.id
53
-
53
+
54
54
  if obj.html_schema_type.kind_of?(Mida::Vocabulary)
55
55
  options['itemtype'] = obj.html_schema_type.itemtype.source
56
56
  else
57
- raise "No vocabulary found (#{obj.html_schema_type})" unless Mida::Vocabulary.find(obj.html_schema_type)
57
+ #raise "No vocabulary found (#{obj.html_schema_type})" unless Mida::Vocabulary.find(obj.html_schema_type)
58
58
  options['itemtype'] = obj.html_schema_type
59
59
  end
60
60
  end
61
-
61
+
62
62
  return options
63
63
  end
64
64
  end
@@ -1,5 +1,7 @@
1
1
  require "mida_vocabulary"
2
2
 
3
+ # Provides `Mida` helper funcion to search vocabulatu or create new Mida::Vocabulary::Custom
4
+
3
5
  class Mida::Vocabulary::Custom < Mida::Vocabulary
4
6
  attr_reader :itemtype
5
7
  def initialize(itemtype)
@@ -13,11 +15,11 @@ def Mida(itemtype, addition = nil)
13
15
  end
14
16
 
15
17
  found_voc = Mida::Vocabulary.find(itemtype)
16
-
18
+
17
19
  if found_voc == Mida::GenericVocabulary
18
20
  found_voc = Mida::Vocabulary::Custom.new(itemtype)
19
21
  end
20
-
22
+
21
23
  if addition
22
24
  Mida::Vocabulary::Custom.new(found_voc.itemtype.source + "/#{addition}")
23
25
  else
@@ -1,6 +1,15 @@
1
1
  # coding: utf-8
2
2
 
3
3
  require "chronic_duration"
4
+
5
+ # Provides view helpers
6
+ # time_tag with "datetime" support
7
+ # time_tag_interval for time intervals
8
+ # time_to_iso8601 time-period converter
9
+ # mida_scope shortcut to build "itemscope" and "itemtype" attributes
10
+ # breadcrumb_link_to makes a link with itemtype="http://data-vocabulary.org/Breadcrumb"
11
+
12
+
4
13
  module GreenMonkey
5
14
  module ViewHelper
6
15
 
@@ -20,7 +29,7 @@ module GreenMonkey
20
29
  title = nil
21
30
  content = args.first || I18n.l(time, format: format)
22
31
  elsif time.kind_of?(Numeric)
23
- title = ChronicDuration.output(time, :format => format)
32
+ title = ChronicDuration.output(time, format: format)
24
33
  content = args.first || distance_of_time_in_words(time)
25
34
  else
26
35
  content = time.to_s
@@ -38,7 +47,7 @@ module GreenMonkey
38
47
  if time.acts_like?(:time)
39
48
  I18n.l(from, format: format)
40
49
  else
41
- ChronicDuration.output(time, :format => format)
50
+ ChronicDuration.output(time, format: format)
42
51
  end
43
52
  end
44
53
 
@@ -52,26 +61,49 @@ module GreenMonkey
52
61
  end
53
62
 
54
63
  def time_to_iso8601(time)
64
+ # http://www.ostyn.com/standards/scorm/samples/ISOTimeForSCORM.htm
65
+ # P[yY][mM][dD][T[hH][mM][s[.s]S]]
66
+
67
+ minute = 60
68
+ hour = minute * 60
69
+ day = hour * 24
70
+ year = day * 365.25
71
+ month = year / 12
72
+
55
73
  if time.acts_like?(:time)
56
74
  time.iso8601
57
75
  elsif time.kind_of?(Numeric)
58
- ChronicDuration.output(time, :format => :iso8601)
76
+ time = time.to_f
77
+ return "PT0H0M0S" if time == 0
78
+
79
+ parts = ["P"]
80
+ parts << "#{(time / year).floor}Y" if time >= year
81
+ parts << "#{(time % year / month).floor}M" if time % year >= month
82
+ parts << "#{(time % month / day).floor}D" if time % month >= day
83
+ time = time % month
84
+ parts << "T" if time % day > 0
85
+ parts << "#{(time % day / hour).floor}H" if time % day >= hour
86
+ parts << "#{(time % hour / minute).floor}M" if time % hour >= minute
87
+ parts << "#{(time % 1 == 0 ? time.to_i : time) % minute}S" if time % minute > 0
88
+
89
+ return parts.join
59
90
  end
60
91
  end
61
92
 
62
93
  def mida_scope(object)
63
94
  options = {itemscope: true}
95
+
64
96
  if object.respond_to?(:html_schema_type)
65
97
  if object.html_schema_type.kind_of?(Mida::Vocabulary)
66
- options.merge! itemtype: object.html_schema_type.itemtype.source
98
+ options.merge!(itemtype: object.html_schema_type.itemtype.source)
67
99
  else
68
- raise "No vocabulary found (#{obj.html_schema_type})" unless Mida::Vocabulary.find(obj.html_schema_type)
69
- options.merge! itemtype: object.html_schema_type
100
+ raise "No vocabulary found (#{object.html_schema_type})" unless Mida::Vocabulary.find(object.html_schema_type)
101
+ options.merge!(itemtype: object.html_schema_type)
70
102
  end
71
103
  elsif object.is_a?(Symbol)
72
- options.merge! itemtype: Mida(object)
104
+ options.merge!(itemtype: Mida(object).itemtype.source)
73
105
  elsif object.is_a?(String)
74
- options.merge! itemtype: object
106
+ options.merge!(itemtype: object)
75
107
  end
76
108
 
77
109
  tag_options(options)
@@ -5,7 +5,10 @@ module GreenMonkey
5
5
  initializer 'green_monkey.init', :before => :load_config_initializers do
6
6
  require "green_monkey/ext/active_model"
7
7
  ActiveModel::Dirty.send :include, GreenMonkey::ModelHelpers
8
- ActiveRecord::Base.send :include, GreenMonkey::ModelHelpers
8
+
9
+ if defined?(ActiveRecord)
10
+ ActiveRecord::Base.send :include, GreenMonkey::ModelHelpers
11
+ end
9
12
 
10
13
  require 'green_monkey/ext/view_helper'
11
14
  ActionView::Base.send :include, GreenMonkey::ViewHelper
@@ -33,7 +33,10 @@ describe "ActionView hacks" do
33
33
  end
34
34
 
35
35
  it "should make breadcrumb link" do
36
- str = render_haml('= breadcrumb_link_to("Home", "/")')
36
+ str = render_haml('= breadcrumb_link_to("Home", "/")').strip
37
37
  str.index('http://data-vocabulary.org/Breadcrumb').should_not == nil
38
+ str.should == '<span itemscope itemtype="http://data-vocabulary.org/Breadcrumb">' +
39
+ '<a href="/" itemprop="url"><span itemprop="title">Home</span></a>' +
40
+ '</span>'
38
41
  end
39
42
  end
data/spec/haml_spec.rb CHANGED
@@ -1,53 +1,52 @@
1
1
  require 'spec_helper'
2
2
  require "pathname"
3
- require "mida"
4
3
 
5
4
  $t = 0
6
5
 
7
6
  describe "Haml generation" do
8
7
  include TestInlineRenderer
9
-
8
+
10
9
  it "should render itemscope as a boolean attribute" do
11
10
  render_haml("%b{:itemscope => true}").should =~ /<b\s+itemscope\s*>/
12
11
  end
13
-
12
+
14
13
  it "should get AR object" do
15
14
  user = User.create
16
15
  tpl = "%article[user]"
17
16
  str = render_haml(tpl, user: user)
18
-
17
+
19
18
  str.should =~ /itemtype=('|")#{Regexp.escape 'http://example.com/User'}('|")/
20
19
  str.should =~ /itemid=.?#{user.id}/
21
20
  str.should =~ /itemscope/
22
21
  end
23
-
22
+
24
23
  it "should render itemprop attribute" do
25
24
  render_haml("%b[:title] Dada").should =~ /itemprop=.?title/
26
25
  end
27
-
26
+
28
27
  it "should generate valid microdata layout" do
29
28
  post = Post.create(title: "Post 1", body: "Some text")
30
29
  tpl = Pathname.new(__FILE__).dirname.join("post")
31
30
  str = render_file(tpl, post: post)
32
31
 
33
- doc = Mida::Document.new(str, "http://example.com/")
34
-
35
- props = doc.items[0].properties
36
- props['url'][0].to_s.should == "http://example.com/posts/#{post.id}"
32
+ doc = parse_mida_page(str)
33
+
34
+ props = doc[0][:properties]
35
+ props['url'][0].to_s.should == "/posts/#{post.id}"
37
36
  props['name'][0].should == post.title
38
37
  props['articleBody'][0].should == post.body
39
38
  props['datePublished'][0].to_i.should == post.created_at.to_i
40
-
41
- doc.items[0].id.should == post.id.to_s
42
- doc.items[0].type.should == post.html_schema_type.itemtype.source
39
+
40
+ doc[0][:id].should == post.id.to_s
41
+ doc[0][:type].should == post.html_schema_type.itemtype.source
43
42
  end
44
-
43
+
45
44
  describe 'time_tag' do
46
45
  it "should produce itemprop if specified" do
47
46
  str = render_haml("= time_tag(Time.now, itemprop: 'time')")
48
47
  str.should =~ /itemprop="time"/
49
48
  end
50
-
49
+
51
50
  it "should run with time" do
52
51
  time = Time.now
53
52
  str = render_haml("= time_tag(time)", time: time)
@@ -72,7 +71,7 @@ describe "Haml generation" do
72
71
  str = render_haml("= time_tag_interval(*time, :format => :short)", time: time)
73
72
  str.should =~ /<time.+datetime=.?#{Regexp.escape time[0].iso8601}\/P6DT6H/
74
73
  end
75
-
74
+
76
75
  it "should add class from model" do
77
76
  user = User.create
78
77
  tpl = "%article.person[user]"
@@ -81,7 +80,7 @@ describe "Haml generation" do
81
80
  html_class.should =~ /person/
82
81
  html_class.should =~ /user/
83
82
  end
84
-
83
+
85
84
  it "should make itemscope as boolean attribute" do
86
85
  str = render_haml("%div{:itemscope => true}")
87
86
  str.should =~ %r{<div\s+itemscope\s*/?>}
data/spec/mida_spec.rb CHANGED
@@ -4,12 +4,12 @@ describe "Mida helper" do
4
4
  it "should find vocabulary class" do
5
5
  Mida(:BlogPosting).should == Mida::SchemaOrg::BlogPosting
6
6
  Mida("http://schema.org/BlogPosting").should == Mida::SchemaOrg::BlogPosting
7
-
7
+
8
8
  custom = Mida("http://t.co/BlogPosting")
9
9
  custom.itemtype.source == "http://t.co/BlogPosting"
10
10
  custom.class.should == Mida::Vocabulary::Custom
11
11
  end
12
-
12
+
13
13
  it "should make Mida::Vocabulary::Custom for extended vocabularies" do
14
14
  extended = Mida(:Blog, :SexJokes)
15
15
  extended.class.should == Mida::Vocabulary::Custom
data/spec/model_spec.rb CHANGED
@@ -4,16 +4,21 @@ describe ActiveModel do
4
4
  it "should check ActiveModel extention" do
5
5
  ActiveRecord::Base.should respond_to(:html_schema_type)
6
6
  end
7
-
7
+
8
8
  it "should return schema-type on #html_schema_type" do
9
9
  Post.html_schema_type.should == Post.new.html_schema_type
10
10
  end
11
-
11
+
12
12
  it "should return instance of kind Mida::Vocabulary" do
13
13
  Post.new.html_schema_type.should == Mida::SchemaOrg::BlogPosting
14
14
  end
15
-
15
+
16
16
  it "should return string on unknown itemtype" do
17
17
  User.html_schema_type.should == "http://example.com/User"
18
18
  end
19
+
20
+ it "should return properties" do
21
+ # make sure it load parents' properties
22
+ Post.html_schema_type.properties.keys.size.should >= 60
23
+ end
19
24
  end
data/spec/spec_helper.rb CHANGED
@@ -27,6 +27,8 @@ module Rails
27
27
  end
28
28
 
29
29
  Rails.application.config.root = Rails.root
30
+ Rails.application.config.eager_load = false
31
+
30
32
  TestApp::Application.initialize!
31
33
 
32
34
  ActiveRecord::Schema.define(:version => 20111023054000) do
@@ -67,4 +69,41 @@ module TestInlineRenderer
67
69
  File.open(file, 'w+') {|f| f.write template }
68
70
  render_file(file.sub(/.haml$/, ''), options)
69
71
  end
72
+ end
73
+
74
+ RSpec.configure do |config|
75
+ config.expect_with :rspec do |c|
76
+ c.syntax = [:should, :expect]
77
+ end
78
+
79
+ config.mock_with :rspec do |c|
80
+ c.syntax = [:should, :expect]
81
+ end
82
+ end
83
+
84
+ # Simple microdata parser
85
+ def parse_mida_page(str)
86
+ require 'nokogiri'
87
+ doc = Nokogiri::HTML(str)
88
+ items = []
89
+ doc.css('[itemscope]').each do |scope|
90
+ item = {type: scope.attr('itemtype'), id: scope.attr('itemid')}
91
+ props = {}
92
+ scope.css('[itemprop]').each do |el|
93
+ prop_name = el.attr('itemprop')
94
+ if prop_name == 'url'
95
+ prop_value = el.attr('href')
96
+ elsif prop_name == 'datePublished' || prop_name.start_with?('date')
97
+ prop_value = DateTime.parse(el.attr('datetime'))
98
+ else
99
+ prop_value = el.inner_text
100
+ end
101
+ props[prop_name] ||= []
102
+ props[prop_name] << prop_value
103
+ end
104
+
105
+ item[:properties] = props
106
+ items << item
107
+ end
108
+ items
70
109
  end
@@ -0,0 +1,42 @@
1
+ require 'spec_helper'
2
+
3
+ describe GreenMonkey::ViewHelper do
4
+ include ActionView::Helpers::TagHelper
5
+ include GreenMonkey::ViewHelper
6
+
7
+ it "should convert time period to iso8601 period" do
8
+ time_to_iso8601(3 * 60 * 60 + 20 * 60 + 19).should == "PT3H20M19S"
9
+ end
10
+
11
+ it "should convert date period to iso8601 period" do
12
+ time_to_iso8601(500 * 24 * 60 * 60 + 3 * 60 * 60 + 20 * 60 + 19).should == "P1Y4M13DT3H20M19S"
13
+ end
14
+
15
+ it "should work with simple" do
16
+ time_to_iso8601(31557600).should == "P1Y"
17
+ time_to_iso8601(2629800).should == "P1M"
18
+ time_to_iso8601(86400).should == "P1D"
19
+ time_to_iso8601(3600).should == "PT1H"
20
+ time_to_iso8601(60).should == "PT1M"
21
+ time_to_iso8601(1).should == "PT1S"
22
+ time_to_iso8601(0).should == "PT0H0M0S"
23
+ end
24
+
25
+ it "should build time tag" do
26
+ time = DateTime.parse("09-09-2014 9:38AM")
27
+ time_tag(time).should == %{<time datetime="2014-09-09T09:38:00+00:00">September 09, 2014 09:38</time>}
28
+ end
29
+
30
+ it "should make time interval tag" do
31
+ time = DateTime.parse("09-09-2014 9:38AM")
32
+ tag = time_tag_interval(time, 1.hour + 20.minutes)
33
+ expected_html = %{<time datetime="2014-09-09T09:38:00+00:00/PT1H20M">September 09, 2014 09:38 in 1 hour 20 minutes</time>}
34
+ tag.should == expected_html
35
+ end
36
+
37
+ it "should build itemscope and itentype attributes" do
38
+ mida_scope(:Airline).should == %{ itemscope itemtype="http://schema.org/Airline"}
39
+ mida_scope(Post).should == %{ itemscope itemtype="http://schema.org/BlogPosting"}
40
+ end
41
+
42
+ end
@@ -1,3 +1,4 @@
1
1
  TestApp::Application.configure do
2
2
  config.active_support.deprecation = :stderr
3
+ config.eager_load = false
3
4
  end
metadata CHANGED
@@ -1,160 +1,145 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: green_monkey
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
5
- prerelease:
4
+ version: 0.2.2
6
5
  platform: ruby
7
6
  authors:
8
7
  - Pavel Evstigneev
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2012-04-23 00:00:00.000000000 Z
11
+ date: 2014-09-09 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: haml
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - ">="
20
18
  - !ruby/object:Gem::Version
21
19
  version: 3.1.0
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - ">="
28
25
  - !ruby/object:Gem::Version
29
26
  version: 3.1.0
30
27
  - !ruby/object:Gem::Dependency
31
28
  name: mida_vocabulary
32
29
  requirement: !ruby/object:Gem::Requirement
33
- none: false
34
30
  requirements:
35
- - - ! '>='
31
+ - - ">="
36
32
  - !ruby/object:Gem::Version
37
- version: '0.1'
33
+ version: 0.2.2
38
34
  type: :runtime
39
35
  prerelease: false
40
36
  version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
37
  requirements:
43
- - - ! '>='
38
+ - - ">="
44
39
  - !ruby/object:Gem::Version
45
- version: '0.1'
40
+ version: 0.2.2
46
41
  - !ruby/object:Gem::Dependency
47
42
  name: chronic_duration
48
43
  requirement: !ruby/object:Gem::Requirement
49
- none: false
50
44
  requirements:
51
- - - ! '>='
45
+ - - ">="
52
46
  - !ruby/object:Gem::Version
53
47
  version: '0'
54
48
  type: :runtime
55
49
  prerelease: false
56
50
  version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
51
  requirements:
59
- - - ! '>='
52
+ - - ">="
60
53
  - !ruby/object:Gem::Version
61
54
  version: '0'
62
55
  - !ruby/object:Gem::Dependency
63
56
  name: bundler
64
57
  requirement: !ruby/object:Gem::Requirement
65
- none: false
66
58
  requirements:
67
- - - ! '>='
59
+ - - ">="
68
60
  - !ruby/object:Gem::Version
69
61
  version: 1.0.0
70
62
  type: :development
71
63
  prerelease: false
72
64
  version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
65
  requirements:
75
- - - ! '>='
66
+ - - ">="
76
67
  - !ruby/object:Gem::Version
77
68
  version: 1.0.0
78
69
  - !ruby/object:Gem::Dependency
79
70
  name: rspec
80
71
  requirement: !ruby/object:Gem::Requirement
81
- none: false
82
72
  requirements:
83
- - - ! '>='
73
+ - - ">="
84
74
  - !ruby/object:Gem::Version
85
- version: 2.7.0
75
+ version: 2.9.0
86
76
  type: :development
87
77
  prerelease: false
88
78
  version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
79
  requirements:
91
- - - ! '>='
80
+ - - ">="
92
81
  - !ruby/object:Gem::Version
93
- version: 2.7.0
82
+ version: 2.9.0
94
83
  - !ruby/object:Gem::Dependency
95
84
  name: rspec-rails
96
85
  requirement: !ruby/object:Gem::Requirement
97
- none: false
98
86
  requirements:
99
- - - ! '>='
87
+ - - ">="
100
88
  - !ruby/object:Gem::Version
101
89
  version: '0'
102
90
  type: :development
103
91
  prerelease: false
104
92
  version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
93
  requirements:
107
- - - ! '>='
94
+ - - ">="
108
95
  - !ruby/object:Gem::Version
109
96
  version: '0'
110
97
  - !ruby/object:Gem::Dependency
111
- name: mida
98
+ name: nokogiri
112
99
  requirement: !ruby/object:Gem::Requirement
113
- none: false
114
100
  requirements:
115
- - - ! '>='
101
+ - - ">="
116
102
  - !ruby/object:Gem::Version
117
103
  version: '0'
118
104
  type: :development
119
105
  prerelease: false
120
106
  version_requirements: !ruby/object:Gem::Requirement
121
- none: false
122
107
  requirements:
123
- - - ! '>='
108
+ - - ">="
124
109
  - !ruby/object:Gem::Version
125
110
  version: '0'
126
111
  - !ruby/object:Gem::Dependency
127
112
  name: sqlite3
128
113
  requirement: !ruby/object:Gem::Requirement
129
- none: false
130
114
  requirements:
131
- - - ! '>='
115
+ - - ">="
132
116
  - !ruby/object:Gem::Version
133
117
  version: '0'
134
118
  type: :development
135
119
  prerelease: false
136
120
  version_requirements: !ruby/object:Gem::Requirement
137
- none: false
138
121
  requirements:
139
- - - ! '>='
122
+ - - ">="
140
123
  - !ruby/object:Gem::Version
141
124
  version: '0'
142
- description: It hacks Rails and Haml
125
+ description: Provides useful helpers to map object with microdata types and display
126
+ object's properties in view
143
127
  email:
144
128
  - pavel.evst@gmail.com
145
129
  executables: []
146
130
  extensions: []
147
131
  extra_rdoc_files: []
148
132
  files:
149
- - .gitignore
150
- - .rspec
151
- - .travis.yml
133
+ - ".gitignore"
134
+ - ".rspec"
135
+ - ".travis.yml"
152
136
  - Gemfile
153
137
  - Gemfile.haml-edge
154
138
  - Gemfile.lock
155
139
  - Gemfile.rails-3.0
156
140
  - Gemfile.rails-3.1
157
141
  - Gemfile.rails-3.2
142
+ - Gemfile.rails-4.1
158
143
  - README.md
159
144
  - Rakefile
160
145
  - green_monkey.gemspec
@@ -176,32 +161,34 @@ files:
176
161
  - spec/model_spec.rb
177
162
  - spec/post.haml
178
163
  - spec/spec_helper.rb
164
+ - spec/view_helper_spec.rb
179
165
  - tmp/rails/assets/stylesheets/application.sass
180
166
  - tmp/rails/config/database.yml
181
167
  - tmp/rails/config/environments/development.rb
182
168
  - tmp/rails/config/environments/test.rb
183
169
  homepage: https://github.com/paxa/green_monkey
184
- licenses: []
170
+ licenses:
171
+ - MIT
172
+ metadata: {}
185
173
  post_install_message:
186
174
  rdoc_options: []
187
175
  require_paths:
188
176
  - lib
189
177
  required_ruby_version: !ruby/object:Gem::Requirement
190
- none: false
191
178
  requirements:
192
- - - ! '>='
179
+ - - ">="
193
180
  - !ruby/object:Gem::Version
194
- version: '0'
181
+ version: '1.9'
195
182
  required_rubygems_version: !ruby/object:Gem::Requirement
196
- none: false
197
183
  requirements:
198
- - - ! '>='
184
+ - - ">="
199
185
  - !ruby/object:Gem::Version
200
186
  version: 1.2.0
201
187
  requirements: []
202
188
  rubyforge_project:
203
- rubygems_version: 1.8.22
189
+ rubygems_version: 2.4.1
204
190
  signing_key:
205
- specification_version: 3
206
- summary: Rails and Haml microdata layout helpers
191
+ specification_version: 4
192
+ summary: Rails and Haml microdata toolkit
207
193
  test_files: []
194
+ has_rdoc: