mongoid_oslc 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ services: mongodb
3
+ rvm:
4
+ - 1.9.3
data/README.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # MongoidOslc
2
2
 
3
+ [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/versative/mongoid_oslc)
4
+ [![Dependency Status](https://gemnasium.com/versative/mongoid_oslc.png)](https://gemnasium.com/versative/mongoid_oslc)
5
+ [![Build Status](https://travis-ci.org/versative/mongoid_oslc.png)](https://travis-ci.org/versative/mongoid_oslc)
6
+
3
7
  Extension for mongoid to parse oslc.where
4
8
 
5
9
  ## Installation
@@ -18,54 +22,68 @@ Or install it yourself as:
18
22
 
19
23
  ## Usage
20
24
 
25
+ ### Configure
26
+
21
27
  Add initializer with your configuration.
22
28
 
23
- Mongoid::Oslc.configure do |config|
24
- config.default_namespaces = [:rtc]
25
- end
29
+ ```ruby
30
+ Mongoid::Oslc.configure do |config|
31
+ config.default_namespaces = [:oslc_custom]
32
+ end
33
+ ```
26
34
 
27
35
  Include Mongoid::Oslc module to your document
28
36
 
29
- class Article
30
- include Mongoid::Document
31
- include Mongoid::Oslc
32
-
33
- domain 'oslc_cm'
34
- describe 'http://open-services.net/ns/cm#ChangeRequest'
35
-
36
- oslc_field :title, type: String, mapping: 'dcterms:title'
37
- oslc_field :description, type: String, mapping: 'dcterms:description'
38
-
39
- end
40
-
41
- Article.oslc_where('dcterms:title="First article" and dcterms:description="First article description"')
42
- => #<Mongoid::Criteria
43
- selector: {"title"=>"First article", "description"=>"First article description"},
44
- options: {},
45
- class: Article,
46
- embedded: false>
47
-
48
- Mongoid::Oslc.resources.mapping
49
- => {
50
- "dcterms:title"=>{
51
- :name=>:title,
52
- :value_type=>"http://www.w3.org/2001/XMLSchema#string",
53
- :read_only=>false,
54
- :occurs=>"http://open-services.net/ns/core#Zero-or-one",
55
- :allowed_values=>[],
56
- :embedded=>false,
57
- :range=>[]
58
- },
59
- "dcterms:description"=>{
60
- :name=>:description,
61
- :value_type=>"http://www.w3.org/2001/XMLSchema#string",
62
- :read_only=>false,
63
- :occurs=>"http://open-services.net/ns/core#Zero-or-one",
64
- :allowed_values=>[],
65
- :embedded=>false,
66
- :range=>[]
67
- }
68
- }
37
+ ```ruby
38
+ class Article
39
+ include Mongoid::Document
40
+ include Mongoid::Oslc
41
+
42
+ domain 'oslc_cm'
43
+ describe 'http://open-services.net/ns/cm#ChangeRequest'
44
+
45
+ oslc_field :title, type: String, mapping: 'dcterms:title'
46
+ oslc_field :description, type: String, mapping: 'dcterms:description'
47
+
48
+ end
49
+ ```
50
+
51
+ ### Search
52
+
53
+ ```ruby
54
+ Article.oslc_where('dcterms:title="First article" and dcterms:description="First article description"')
55
+ => #<Mongoid::Criteria
56
+ selector: {"title"=>"First article", "description"=>"First article description"},
57
+ options: {},
58
+ class: Article,
59
+ embedded: false>
60
+ ```
61
+
62
+ ### Mappings
63
+
64
+ ```ruby
65
+ Mongoid::Oslc.resources.mapping
66
+ => {
67
+ "dcterms:title"=>{
68
+ :name=>:title,
69
+ :value_type=>"http://www.w3.org/2001/XMLSchema#string",
70
+ :read_only=>false,
71
+ :occurs=>"http://open-services.net/ns/core#Zero-or-one",
72
+ :allowed_values=>[],
73
+ :embedded=>false,
74
+ :range=>[]
75
+ },
76
+ "dcterms:description"=>{
77
+ :name=>:description,
78
+ :value_type=>"http://www.w3.org/2001/XMLSchema#string",
79
+ :read_only=>false,
80
+ :occurs=>"http://open-services.net/ns/core#Zero-or-one",
81
+ :allowed_values=>[],
82
+ :embedded=>false,
83
+ :range=>[]
84
+ }
85
+ }
86
+ ```
69
87
 
70
88
  ## Contributing
71
89
 
@@ -18,25 +18,25 @@ module Mongoid
18
18
  end
19
19
 
20
20
  def domain
21
- Mongoid::Oslc.resources[self._type][:domain]
21
+ Mongoid::Oslc.resources[self.class.name][:domain]
22
22
  end
23
23
 
24
24
  def describe
25
- Mongoid::Oslc.resources[self._type][:describe]
25
+ Mongoid::Oslc.resources[self.class.name][:describe]
26
26
  end
27
27
 
28
28
  def oslc_value(oslc_name)
29
- field_properties = Mongoid::Oslc.resources[self._type][:fields][oslc_name]
30
- self[field_properties[:name]]
29
+ field_name = Mongoid::Oslc.resources.field_name(oslc_name)
30
+ self[field_name]
31
31
  end
32
32
 
33
33
  module ClassMethods
34
34
  def domain(name)
35
- Mongoid::Oslc.resources[self.to_s].merge!(domain: name)
35
+ Mongoid::Oslc.resources[self.name].merge!(domain: name)
36
36
  end
37
37
 
38
38
  def describe(describe)
39
- Mongoid::Oslc.resources[self.to_s].merge!(describe: describe)
39
+ Mongoid::Oslc.resources[self.name].merge!(describe: describe)
40
40
  end
41
41
 
42
42
  def oslc_field(name, options = {})
@@ -48,12 +48,13 @@ module Mongoid
48
48
  def oslc_embeds_many(name, options = {})
49
49
  oslc_options = options_for_oslc(options)
50
50
  embeds_many(name, options)
51
- register_oslc_field(name, oslc_options.merge(type: Array, embedded: true))
51
+ embedded_class = relations[name.to_s].class_name.constantize
52
+ register_oslc_field(name, oslc_options.merge(type: Array, embedded: embedded_class))
52
53
  end
53
54
 
54
55
  def oslc_fields
55
56
  (ancestors - included_modules).inject({}) do |hash, ancestor_class|
56
- hash.merge(Mongoid::Oslc.resources[ancestor_class.to_s][:fields])
57
+ hash.merge(Mongoid::Oslc.resources[ancestor_class.name][:fields])
57
58
  end
58
59
  end
59
60
 
@@ -63,34 +64,33 @@ module Mongoid
63
64
  end
64
65
 
65
66
  def register_oslc_field(name, options)
66
- mandatory = options[:mandatory] || false
67
- occurs = if mandatory && options[:type] == Array
68
- ::Oslc::ONE_OR_MANY
69
- elsif mandatory
70
- ::Oslc::EXACTLY_ONE
71
- elsif options[:type] == Array
72
- ::Oslc::ZERO_OR_MANY
73
- else
74
- ::Oslc::ZERO_OR_ONE
75
- end
76
-
77
- Mongoid::Oslc.resources[self.to_s][:fields][options[:mapping]] = {
67
+ Mongoid::Oslc.resources[self.name][:fields][options[:mapping]] = {
78
68
  name: name,
79
69
  value_type: ::Oslc::VALUE_TYPES[options[:value_type] || fields[name.to_s].type.to_s],
80
70
  read_only: (options[:read_only] || false),
81
- occurs: occurs,
71
+ occurs: occurs(options),
82
72
  allowed_values: (options[:allowed_values] || []),
83
- embedded: (options[:embedded] ? relations[name.to_s].class_name.constantize : false),
73
+ embedded: (options[:embedded] || false),
84
74
  range: (options[:range] || [])
85
75
  }
86
76
  end
87
77
 
88
78
  private
89
79
 
80
+ def occurs(options)
81
+ mandatory = options[:mandatory] || false
82
+ case
83
+ when mandatory && options[:type] == Array then ::Oslc::ONE_OR_MANY
84
+ when mandatory then ::Oslc::EXACTLY_ONE
85
+ when options[:type] == Array then ::Oslc::ZERO_OR_MANY
86
+ else ::Oslc::ZERO_OR_ONE
87
+ end
88
+ end
89
+
90
90
  def options_for_oslc(options)
91
91
  [:value_type, :read_only, :allowed_values, :mapping, :mandatory, :range].inject({}) do |hash, field|
92
92
  hash.merge(field => options.delete(field))
93
- end.merge(type: options[:type])
93
+ end.reject{|field, value| value.nil?}.merge(type: options[:type])
94
94
  end
95
95
  end
96
96
  end
@@ -31,6 +31,6 @@ module Mongoid::Oslc
31
31
 
32
32
  # this is ugly. why can't we pass the default value to config_accessor...?
33
33
  configure do |config|
34
- config.default_namespaces = [:rtc]
34
+ config.default_namespaces = [:oslc_custom]
35
35
  end
36
36
  end
@@ -14,7 +14,7 @@ module Mongoid
14
14
  def to_mongo_query
15
15
  operand_1 = elements[0].to_mongo_query
16
16
  operand_2 = elements[2].to_mongo_query
17
- operand_1.merge(operand_2)
17
+ operand_1.deep_merge(operand_2)
18
18
  end
19
19
  end
20
20
 
@@ -81,18 +81,12 @@ module Mongoid
81
81
  end
82
82
  end
83
83
 
84
- class NilValue < GrammarNode
84
+ class BlankValue < GrammarNode
85
85
  def to_mongo_query
86
86
  nil
87
87
  end
88
88
  end
89
89
 
90
- class EmptyValue < GrammarNode
91
- def to_mongo_query
92
- []
93
- end
94
- end
95
-
96
90
  class DecimalValue < GrammarNode
97
91
  def to_mongo_query
98
92
  text_value.to_f
@@ -101,14 +95,22 @@ module Mongoid
101
95
 
102
96
  class StringEsc < GrammarNode
103
97
  def to_mongo_query
104
- text_value[1...-1]
98
+ value = text_value[1...-1]
99
+ if value.starts_with?("*") || value.ends_with?("*")
100
+ value.starts_with?("*") ? value.slice!(0) : value.prepend("\\A")
101
+ value.ends_with?("*") ? value.slice!(-1) : value.concat("\\z")
102
+ value = Regexp.new(value)
103
+ end
104
+ value
105
105
  end
106
106
  end
107
107
 
108
108
  class Condition < GrammarNode
109
109
  def to_mongo_query
110
- field = Mongoid::Oslc.resources.field_name(elements[0].text_value)
111
- value = elements[2].to_mongo_query
110
+ oslc_name = elements[0].text_value
111
+ field = Mongoid::Oslc.resources.field_name(oslc_name)
112
+ value = elements[2].to_mongo_query
113
+ value = [] if value.nil? && Mongoid::Oslc.resources.is_field_array?(oslc_name)
112
114
  operator_with_value = elements[1].to_mongo_query_with_value(value)
113
115
  { field => operator_with_value }
114
116
  end
@@ -21,9 +21,8 @@
21
21
  # LANGTAG ::= /* see "SPARQL Query Language for RDF", http://www.w3.org/TR/rdf-sparql-query/#rLANGTAG */
22
22
 
23
23
  # Extension
24
- # literal_value ::= | nil_value | empty_value
25
- # nil_value ::= "nil"
26
- # empty_value ::= "empty"
24
+ # literal_value ::= | blank_value
25
+ # blank_value ::= "blank"
27
26
 
28
27
  grammar Grammar
29
28
 
@@ -76,19 +75,15 @@ grammar Grammar
76
75
  end
77
76
 
78
77
  rule literal_value
79
- boolean / decimal / string_esc / nil_value / empty_value
78
+ boolean / decimal / string_esc / blank_value
80
79
  end
81
80
 
82
81
  rule boolean
83
82
  'true' <Mongoid::Oslc::Grammar::BooleanValue> / 'false' <Mongoid::Oslc::Grammar::BooleanValue>
84
83
  end
85
84
 
86
- rule nil_value
87
- 'nil' <Mongoid::Oslc::Grammar::NilValue>
88
- end
89
-
90
- rule empty_value
91
- 'empty' <Mongoid::Oslc::Grammar::EmptyValue>
85
+ rule blank_value
86
+ 'blank' <Mongoid::Oslc::Grammar::BlankValue>
92
87
  end
93
88
 
94
89
  rule decimal
@@ -18,12 +18,27 @@ module Mongoid
18
18
  end
19
19
 
20
20
  def field_name(oslc_name)
21
- if Mongoid::Oslc.config.default_namespaces.include?(oslc_name.split(":")[0].to_sym)
22
- oslc_name.split(':')[1].to_sym
21
+ if custom_field?(oslc_name)
22
+ oslc_name.split(":")[1].to_sym
23
23
  else
24
24
  mapping[oslc_name][:name]
25
25
  end
26
26
  end
27
+
28
+ def is_field_array?(oslc_name)
29
+ if custom_field?(oslc_name)
30
+ false
31
+ else
32
+ occurs = self.mapping[oslc_name][:occurs]
33
+ occurs == ::Oslc::ZERO_OR_MANY || occurs == ::Oslc::ONE_OR_MANY
34
+ end
35
+ end
36
+
37
+ private
38
+
39
+ def custom_field?(oslc_name)
40
+ Mongoid::Oslc.config.default_namespaces.include? oslc_name.split(":")[0].to_sym
41
+ end
27
42
  end
28
43
  end
29
44
  end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Oslc
3
- VERSION = '0.1.1'
3
+ VERSION = '0.1.2'
4
4
  end
5
5
  end
@@ -6,9 +6,10 @@ Gem::Specification.new do |gem|
6
6
  gem.version = Mongoid::Oslc::VERSION
7
7
  gem.authors = ["Martin Magnusek"]
8
8
  gem.email = ["magnusekm@gmail.com"]
9
- gem.description = %q{Extension for mongoid to parse oslc.where}
10
- gem.summary = ""
11
- gem.homepage = ""
9
+ gem.description = "Extension for mongoid to parse oslc.where"
10
+ gem.summary = "Extension for mongoid to parse oslc.where"
11
+ gem.homepage = "http://github.com/versative/mongoid_oslc"
12
+ gem.licenses = ["MIT"]
12
13
 
13
14
  gem.add_runtime_dependency 'activesupport', '~> 3.0'
14
15
  gem.add_runtime_dependency 'mongoid', '>= 3.0', '< 4.0'
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Oslc::Resources do
4
+ subject { Mongoid::Oslc.resources }
5
+
6
+ describe "#class_for" do
7
+ it "should return correct class name" do
8
+ subject.class_for('http://open-services.net/ns/cm#ChangeRequest').should eql 'MongoDocument'
9
+ end
10
+ end
11
+
12
+ describe "#domain" do
13
+ it "should return correct domain" do
14
+ subject.domain('http://open-services.net/ns/cm#ChangeRequest').should eql 'oslc_cm'
15
+ end
16
+ end
17
+
18
+ describe "#mapping" do
19
+ it "should register all document fields" do
20
+ subject.mapping.keys.should eql ['dcterms:title', 'dcterms:description', 'dcterms:creator', 'oslc_cm:closed', 'dcterms:created', 'dcterms:modified']
21
+ end
22
+ end
23
+
24
+ describe "#field_name" do
25
+ it "should return field name" do
26
+ subject.field_name('dcterms:title').should eql :title
27
+ end
28
+
29
+ it "should return field name for custom field" do
30
+ subject.field_name('oslc_custom:custom_field').should eql :custom_field
31
+ end
32
+ end
33
+ end
@@ -1,19 +1,5 @@
1
1
  require 'spec_helper'
2
2
 
3
- class MongoDocument
4
- include Mongoid::Document
5
- include Mongoid::Oslc
6
- include Mongoid::Timestamps
7
-
8
- oslc_field :title, type: String, mapping: 'dcterms:title'
9
- oslc_field :description, type: String, mapping: 'dcterms:description'
10
- oslc_field :closed, type: Boolean,mapping: 'oslc_cm:closed'
11
-
12
- register_oslc_field :created_at, mapping: 'dcterms:created', value_type: 'DateTime'
13
- register_oslc_field :updated_at, mapping: 'dcterms:modified', value_type: 'DateTime'
14
-
15
- end
16
-
17
3
  describe Mongoid::Oslc::Strategy do
18
4
  let(:parser) { Mongoid::Oslc::Strategy }
19
5
 
@@ -41,16 +27,20 @@ describe Mongoid::Oslc::Strategy do
41
27
  parse_query('dcterms:created>"2010-04-01"').should eql({created_at: {"$gt"=>"2010-04-01"}})
42
28
  end
43
29
 
30
+ it "should parse query with range" do
31
+ parse_query('dcterms:created>"2012-12-08" and dcterms:created<"2012-12-30"').should eql({created_at: {"$gt"=>"2012-12-08", "$lt"=>"2012-12-30"}})
32
+ end
33
+
44
34
  it "should parse query with not value" do
45
35
  parse_query('dcterms:title!="First"').should eql({title: {"$ne" => "First"}})
46
36
  end
47
37
 
48
- it "should parse query with nil value" do
49
- parse_query('dcterms:title=nil').should eql({title: nil})
38
+ it "should parse query with blank value" do
39
+ parse_query('dcterms:title=blank').should eql({title: nil})
50
40
  end
51
41
 
52
- it "should parse query with empty value" do
53
- parse_query('dcterms:title=empty').should eql({title: []})
42
+ it "should parse query with blank value" do
43
+ parse_query('dcterms:creator=blank').should eql({creators: []})
54
44
  end
55
45
 
56
46
  it "should parse query with decimal value" do
@@ -77,9 +67,17 @@ describe Mongoid::Oslc::Strategy do
77
67
  parse_query('dcterms:title in ["First","Second"]').should eql({title: {"$in"=>["First", "Second"]}})
78
68
  end
79
69
 
70
+ it "should parse query with wild char" do
71
+ parse_query('dcterms:title="*requirement*"').should eql({title: /requirement/})
72
+ end
73
+
74
+ it "should parse query with wild char" do
75
+ parse_query('dcterms:title="requirement*"').should eql({title: /\Arequirement/})
76
+ end
77
+
80
78
  describe "custom fields" do
81
79
  it "should parse query with custom namespace" do
82
- parse_query('rtc:color="Black"').should eql({color: "Black"})
80
+ parse_query('oslc_custom:color="Black"').should eql({color: "Black"})
83
81
  end
84
82
  end
85
83
  end
@@ -1,15 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
- class MongoDocument
4
- include Mongoid::Document
5
- include Mongoid::Oslc
3
+ describe Mongoid::Oslc do
4
+ subject { MongoDocument.new }
6
5
 
7
- oslc_field :title, type: String, mapping: 'dcterms:title'
6
+ it { subject.domain.should eql 'oslc_cm' }
7
+ it { subject.describe.should eql 'http://open-services.net/ns/cm#ChangeRequest' }
8
8
 
9
- end
9
+ describe "#oslc_value" do
10
+ before do
11
+ subject.title = 'Document title'
12
+ end
10
13
 
11
- describe Mongoid::Oslc do
12
- subject { MongoDocument.new }
14
+ it "should return title value" do
15
+ subject.oslc_value('dcterms:title').should eql 'Document title'
16
+ end
17
+ end
13
18
 
14
19
  describe "class" do
15
20
  it "should have method #oslc_where" do
@@ -5,6 +5,24 @@ require 'mongoid/oslc'
5
5
 
6
6
  require 'rspec'
7
7
 
8
+ class MongoDocument
9
+ include Mongoid::Document
10
+ include Mongoid::Oslc
11
+ include Mongoid::Timestamps
12
+
13
+ domain 'oslc_cm'
14
+ describe 'http://open-services.net/ns/cm#ChangeRequest'
15
+
16
+ oslc_field :title, type: String, mapping: 'dcterms:title'
17
+ oslc_field :description, type: String, mapping: 'dcterms:description'
18
+ oslc_field :creators, type: Array, mapping: 'dcterms:creator', value_type: 'Resource', default: []
19
+ oslc_field :closed, type: Boolean,mapping: 'oslc_cm:closed'
20
+
21
+ register_oslc_field :created_at, mapping: 'dcterms:created', value_type: 'DateTime'
22
+ register_oslc_field :updated_at, mapping: 'dcterms:modified', value_type: 'DateTime'
23
+
24
+ end
25
+
8
26
  Mongoid.configure do |config|
9
27
  config.connect_to('mongoid_oslc_where_test')
10
28
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_oslc
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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: 2012-12-05 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -121,6 +121,7 @@ extensions: []
121
121
  extra_rdoc_files: []
122
122
  files:
123
123
  - .gitignore
124
+ - .travis.yml
124
125
  - Gemfile
125
126
  - LICENSE.txt
126
127
  - README.md
@@ -135,11 +136,13 @@ files:
135
136
  - lib/mongoid/oslc/version.rb
136
137
  - lib/mongoid_oslc.rb
137
138
  - mongoid_oslc.gemspec
139
+ - spec/lib/mongoid/oslc/resources_spec.rb
138
140
  - spec/lib/mongoid/oslc/strategy_spec.rb
139
141
  - spec/lib/mongoid/oslc_spec.rb
140
142
  - spec/spec_helper.rb
141
- homepage: ''
142
- licenses: []
143
+ homepage: http://github.com/versative/mongoid_oslc
144
+ licenses:
145
+ - MIT
143
146
  post_install_message:
144
147
  rdoc_options: []
145
148
  require_paths:
@@ -152,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
155
  version: '0'
153
156
  segments:
154
157
  - 0
155
- hash: 3353479294008417306
158
+ hash: -3967237591695565408
156
159
  required_rubygems_version: !ruby/object:Gem::Requirement
157
160
  none: false
158
161
  requirements:
@@ -161,14 +164,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
164
  version: '0'
162
165
  segments:
163
166
  - 0
164
- hash: 3353479294008417306
167
+ hash: -3967237591695565408
165
168
  requirements: []
166
169
  rubyforge_project:
167
170
  rubygems_version: 1.8.24
168
171
  signing_key:
169
172
  specification_version: 3
170
- summary: ''
173
+ summary: Extension for mongoid to parse oslc.where
171
174
  test_files:
175
+ - spec/lib/mongoid/oslc/resources_spec.rb
172
176
  - spec/lib/mongoid/oslc/strategy_spec.rb
173
177
  - spec/lib/mongoid/oslc_spec.rb
174
178
  - spec/spec_helper.rb