mida 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/spec_helper.rb CHANGED
@@ -1,41 +1,18 @@
1
+ require 'bundler/setup'
1
2
  require 'rspec'
2
3
 
3
- def element_add_attribute(element, attribute, value)
4
- if value
5
- attr = mock(Nokogiri::XML::Attr)
6
- if value != true
7
- attr.stub!(:value).and_return(value)
8
- end
9
- end
10
- element.should_receive(:attribute).any_number_of_times.with(attribute).and_return(attr)
11
- element
4
+ def html_wrap(html)
5
+ "<!DOCTYPE html><html><body>#{html}</body></html>"
12
6
  end
13
7
 
14
- # Return a mock Nokogiri::XML::Element
15
- def mock_element(tag, attributes={}, inner_text=nil, search_return=[], id_searches={})
16
- element = mock(Nokogiri::XML::Element)
17
-
18
- ['id', 'itemid', 'itemprop', 'itemref', 'itemscope', 'itemtype'].each do |name|
19
- attributes[name] = nil unless attributes.has_key?(name)
20
- end
21
- attributes.each do |name, value|
22
- element_add_attribute(element, name, value)
23
- end
24
-
25
- element.stub!(:inner_text).and_return(inner_text)
26
- element.stub!(:name).and_return(tag)
27
-
28
- element.should_receive(:search).any_number_of_times.with('./*').and_return(search_return)
29
-
30
- # Set a valid return element for each likely id
31
- ('a'..'z').each do |id|
32
- stub = element.should_receive(:search).any_number_of_times.with("//*[@id='#{id}']")
33
- if id_searches.has_key?(id)
34
- stub.and_return([id_searches[id]])
35
- else
36
- stub.and_return([])
37
- end
38
- end
39
-
40
- element
8
+ # Return the last error message on STDERR.
9
+ # Prevents the message being output to STDERR.
10
+ def last_stderr
11
+ orig_stderr = $stderr
12
+ $stderr = StringIO.new
13
+ yield
14
+ $stderr.rewind
15
+ message = $stderr.string.chomp
16
+ $stderr = orig_stderr
17
+ message
41
18
  end
@@ -0,0 +1,148 @@
1
+ require 'spec_helper'
2
+ require 'mida'
3
+
4
+ describe Mida::Vocabulary, 'when subclassed and given has statements with no blocks' do
5
+ before do
6
+ class Organization < Mida::Vocabulary
7
+ itemtype %r{http://example\.com.*?organization$}i
8
+ has_one 'name'
9
+ has_many 'tel', 'url'
10
+ end
11
+ end
12
+
13
+ it '#itemtype should return the correct regexp' do
14
+ Organization.itemtype.should == %r{http://example\.com.*?organization$}i
15
+ end
16
+
17
+ it 'should specify name to appear once' do
18
+ Organization.properties['name'][:num].should == :one
19
+ end
20
+
21
+ it 'should specify tel and url to appear many times' do
22
+ Organization.properties['tel'][:num].should == :many
23
+ Organization.properties['url'][:num].should == :many
24
+ end
25
+ end
26
+
27
+ describe Mida::Vocabulary, 'when subclassed and given has statements with blocks' do
28
+ before do
29
+ class Rating < Mida::Vocabulary
30
+ itemtype %r{http://example\.com.*?rating$}i
31
+ has_one 'best', 'value'
32
+ end
33
+
34
+ class Comment < Mida::Vocabulary
35
+ itemtype %r{http://example\.com.*?comment$}i
36
+ has_one 'commentor', 'comment'
37
+ end
38
+
39
+ class Review < Mida::Vocabulary
40
+ itemtype %r{http://example\.com.*?review$}i
41
+ has_one 'itemreviewed'
42
+ has_one 'rating' do
43
+ extract Rating, Mida::DataType::Text
44
+ end
45
+ has_many 'comments' do
46
+ extract Comment
47
+ end
48
+ end
49
+ end
50
+
51
+ it '#itemtype should return the correct regexp' do
52
+ Review.itemtype.should == %r{http://example\.com.*?review$}i
53
+ end
54
+
55
+ it 'should specify itemreviewed to appear once' do
56
+ Review.properties['itemreviewed'][:num].should == :one
57
+ end
58
+
59
+ it 'should specify that itemreviewed only have the type Mida::DataType::Text' do
60
+ Review.properties['itemreviewed'][:types].should == [Mida::DataType::Text]
61
+ end
62
+
63
+ it 'should specify rating to appear once' do
64
+ Review.properties['rating'][:num].should == :one
65
+ end
66
+
67
+ it 'should specify rating to only have the types: Rating, Mida::DataType::Text' do
68
+ Review.properties['rating'][:types].should == [Rating, Mida::DataType::Text]
69
+ end
70
+
71
+ it 'should specify comments to appear many times' do
72
+ Review.properties['comments'][:num].should == :many
73
+ end
74
+
75
+ it 'should specify that comments only have the type Comment' do
76
+ Review.properties['comments'][:types].should == [Comment]
77
+ end
78
+ end
79
+
80
+ describe Mida::Vocabulary, 'when subclassed and used with :any for properties and types' do
81
+ before do
82
+ class Person < Mida::Vocabulary
83
+ itemtype %r{http://example.com/vocab/person}
84
+ has_one 'name'
85
+ has_many :any do
86
+ extract :any
87
+ end
88
+ end
89
+ end
90
+
91
+ it '#itemtype should return the correct regexp' do
92
+ Person.itemtype.should == %r{http://example.com/vocab/person}
93
+ end
94
+
95
+ it 'should specify that name only appears once' do
96
+ Person.properties['name'][:num].should == :one
97
+ end
98
+
99
+ it 'should specify that any other property can appear many times' do
100
+ Person.properties[:any][:num].should == :many
101
+ end
102
+
103
+ it 'should specify that any other property can have any type' do
104
+ Person.properties[:any][:types].should == [:any]
105
+ end
106
+ end
107
+
108
+ describe Mida::Vocabulary, 'when subclassed' do
109
+
110
+ before do
111
+ # Make sure the class is redefined afresh to make sure that
112
+ # inherited() hook is called
113
+ Mida::Vocabulary.unregister(Person)
114
+ Object.send(:remove_const, :Person)
115
+
116
+ class Person < Mida::Vocabulary
117
+ itemtype %r{http://example.com/vocab/person}
118
+ has_one 'name'
119
+ has_many :any do
120
+ extract :any
121
+ end
122
+ end
123
+ end
124
+
125
+ it 'should register the vocabulary subclass' do
126
+ Mida::Vocabulary.vocabularies.should include(Person)
127
+ end
128
+
129
+ end
130
+
131
+ describe Mida::Vocabulary, 'when subclassed and has no properties' do
132
+
133
+ before do
134
+
135
+ class Empty < Mida::Vocabulary
136
+ itemtype %r{http://example.com/vocab/empty}
137
+ end
138
+ end
139
+
140
+ it 'should register the vocabulary subclass' do
141
+ Mida::Vocabulary.vocabularies.should include(Empty)
142
+ end
143
+
144
+ it '#properties should return an empty hash' do
145
+ Mida::Vocabulary.properties.should == {}
146
+ end
147
+
148
+ end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mida
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.2.0
5
+ version: 0.3.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Lawrence Woodman
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-03 00:00:00 Z
13
+ date: 2011-06-29 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
@@ -48,21 +48,37 @@ files:
48
48
  - lib/mida.rb
49
49
  - lib/mida/item.rb
50
50
  - lib/mida/document.rb
51
- - lib/mida/vocabulary/generic.rb
52
- - lib/mida/vocabularydesc.rb
51
+ - lib/mida/datatype.rb
52
+ - lib/mida/propertydesc.rb
53
53
  - lib/mida/itemprop.rb
54
+ - lib/mida/datatype/float.rb
55
+ - lib/mida/datatype/number.rb
56
+ - lib/mida/datatype/iso8601date.rb
57
+ - lib/mida/datatype/text.rb
58
+ - lib/mida/datatype/boolean.rb
59
+ - lib/mida/datatype/integer.rb
60
+ - lib/mida/genericvocabulary.rb
54
61
  - lib/mida/vocabulary.rb
62
+ - lib/mida/itemscope.rb
63
+ - spec/propertydesc_spec.rb
64
+ - spec/itemscope_spec.rb
55
65
  - spec/itemprop_spec.rb
56
66
  - spec/document_spec.rb
67
+ - spec/vocabulary_spec.rb
57
68
  - spec/item_spec.rb
58
69
  - spec/spec_helper.rb
59
- - spec/vocabularydesc_spec.rb
70
+ - spec/datatype/iso8601date_spec.rb
71
+ - spec/datatype/integer_spec.rb
72
+ - spec/datatype/float_spec.rb
73
+ - spec/datatype/text_spec.rb
74
+ - spec/datatype/boolean_spec.rb
75
+ - spec/datatype/number_spec.rb
60
76
  - TODO.rdoc
61
77
  - CHANGELOG.rdoc
62
78
  - README.rdoc
63
79
  - LICENSE.rdoc
64
80
  - Rakefile
65
- homepage: http://github.com/LawrenceWoodman/mida
81
+ homepage: http://lawrencewoodman.github.com/mida/
66
82
  licenses: []
67
83
 
68
84
  post_install_message:
@@ -1,15 +0,0 @@
1
- module Mida
2
- module Vocabulary
3
-
4
- # A Generic vocabulary that will match against anything
5
- class Generic < Mida::VocabularyDesc
6
- itemtype %r{}
7
- has_many :any do
8
- types :any
9
- end
10
- end
11
-
12
- register(Generic)
13
- end
14
-
15
- end
@@ -1,57 +0,0 @@
1
- module Mida
2
-
3
- # Class used to describe a vocabulary
4
- #
5
- # To specify a vocabulary use the following methods:
6
- # +itemtype+, +has_one+, +has_many+, +types+
7
- class VocabularyDesc
8
-
9
- # Sets the regular expression to match against the +itemtype+
10
- # or returns the current regular expression
11
- def self.itemtype(regexp_arg=nil)
12
- if regexp_arg
13
- @itemtype = regexp_arg
14
- else
15
- @itemtype
16
- end
17
- end
18
-
19
- # Getter to read the created propeties specification
20
- def self.prop_spec
21
- @prop_spec || {}
22
- end
23
-
24
- # The types a property can take. E.g. +String+, or another +Vocabulary+
25
- # If you want to say any type, then use +:any+ as the class
26
- # This should be used within a +has_one+ or +has_many+ block
27
- def self.types(*type_classes)
28
- {types: type_classes}
29
- end
30
-
31
- # Defines the properties as only containing one value
32
- # If want to say any property name, then use +:any+ as a name
33
- def self.has_one(*property_names, &block)
34
- has(:one, *property_names, &block)
35
- end
36
-
37
- # Defines the properties as containing many values
38
- # If want to say any property name, then use +:any+ as a name
39
- def self.has_many(*property_names, &block)
40
- has(:many, *property_names, &block)
41
- end
42
-
43
- def self.has(num, *property_names, &block)
44
- @prop_spec ||= {}
45
- property_names.each_with_object(@prop_spec) do |name, prop_spec|
46
- prop_spec[name] = if block_given?
47
- {num: num}.merge(yield)
48
- else
49
- {num: num, types: [String]}
50
- end
51
- end
52
- end
53
-
54
- private_class_method :has
55
-
56
- end
57
- end
@@ -1,106 +0,0 @@
1
- require_relative 'spec_helper'
2
- require_relative '../lib/mida'
3
-
4
- describe Mida::VocabularyDesc, 'when subclassed and given has statements with no blocks' do
5
- before do
6
- class Organization < Mida::VocabularyDesc
7
- itemtype %r{http://example\.com.*?organization$}i
8
- has_one 'name'
9
- has_many 'tel', 'url'
10
- end
11
- end
12
-
13
- it '#itemtype should return the correct regexp' do
14
- Organization.itemtype.should == %r{http://example\.com.*?organization$}i
15
- end
16
-
17
- it 'should specify name to appear once' do
18
- Organization.prop_spec['name'][:num].should == :one
19
- end
20
-
21
- it 'should specify tel and url to appear many times' do
22
- Organization.prop_spec['tel'][:num].should == :many
23
- Organization.prop_spec['url'][:num].should == :many
24
- end
25
- end
26
-
27
- describe Mida::VocabularyDesc, 'when subclassed and given has statements with blocks' do
28
- before do
29
- class Rating < Mida::VocabularyDesc
30
- itemtype %r{http://example\.com.*?rating$}i
31
- has_one 'best', 'value'
32
- end
33
-
34
- class Comment < Mida::VocabularyDesc
35
- itemtype %r{http://example\.com.*?comment$}i
36
- has_one 'commentor', 'comment'
37
- end
38
-
39
- class Review < Mida::VocabularyDesc
40
- itemtype %r{http://example\.com.*?review$}i
41
- has_one 'itemreviewed'
42
- has_one 'rating' do
43
- types Rating, String
44
- end
45
- has_many 'comments' do
46
- types Comment
47
- end
48
- end
49
- end
50
-
51
- it '#itemtype should return the correct regexp' do
52
- Review.itemtype.should == %r{http://example\.com.*?review$}i
53
- end
54
-
55
- it 'should specify itemreviewed to appear once' do
56
- Review.prop_spec['itemreviewed'][:num].should == :one
57
- end
58
-
59
- it 'should specify that itemreviewed only have the type String' do
60
- Review.prop_spec['itemreviewed'][:types].should == [String]
61
- end
62
-
63
- it 'should specify rating to appear once' do
64
- Review.prop_spec['rating'][:num].should == :one
65
- end
66
-
67
- it 'should specify rating to only have the types: Rating, String' do
68
- Review.prop_spec['rating'][:types].should == [Rating, String]
69
- end
70
-
71
- it 'should specify comments to appear many times' do
72
- Review.prop_spec['comments'][:num].should == :many
73
- end
74
-
75
- it 'should specify that comments only have the type Comment' do
76
- Review.prop_spec['comments'][:types].should == [Comment]
77
- end
78
- end
79
-
80
- describe Mida::VocabularyDesc, 'when subclassed and used with :any for properties and types' do
81
- before do
82
- class Person < Mida::VocabularyDesc
83
- itemtype %r{}
84
- has_one 'name'
85
- has_many :any do
86
- types :any
87
- end
88
- end
89
- end
90
-
91
- it '#itemtype should return the correct regexp' do
92
- Person.itemtype.should == %r{}
93
- end
94
-
95
- it 'should specify that name only appears once' do
96
- Person.prop_spec['name'][:num].should == :one
97
- end
98
-
99
- it 'should specify that any other property can appear many times' do
100
- Person.prop_spec[:any][:num].should == :many
101
- end
102
-
103
- it 'should specify that any other property can have any type' do
104
- Person.prop_spec[:any][:types].should == [:any]
105
- end
106
- end