mongoid_oslc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mongoid_oslc.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Martin Magnusek
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # MongoidOslc
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mongoid_oslc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mongoid_oslc
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,83 @@
1
+ require 'treetop'
2
+
3
+ module Mongoid
4
+ module Oslc
5
+ module OslcGrammar
6
+ class GrammarNode < Treetop::Runtime::SyntaxNode
7
+ def to_mongo_query
8
+ elements[0].to_mongo_query
9
+ end
10
+ end
11
+
12
+ class BinaryDecision < GrammarNode
13
+
14
+ def to_mongo_query
15
+ operand_1 = elements[0].to_mongo_query
16
+ operand_2 = elements[2].to_mongo_query
17
+ operand_1.merge(operand_2)
18
+ end
19
+ end
20
+
21
+ class LogicalOperator < GrammarNode
22
+ end
23
+
24
+ class Field < GrammarNode
25
+ def to_mongo_query
26
+ text_content
27
+ end
28
+ end
29
+
30
+ class Operator < GrammarNode
31
+ OPERATORS = {
32
+ "=" => "$in",
33
+ "!=" => "$neq",
34
+ ">" => "$gt",
35
+ ">=" => "$gte",
36
+ "<" => "$lt",
37
+ "<=" => "$lte",
38
+ "in" => "$in"
39
+ }
40
+
41
+ def to_mongo_query
42
+ OPERATORS[text_value]
43
+ end
44
+
45
+ def to_mongo_query_with_value(value)
46
+ if text_value == "="
47
+ value
48
+ else
49
+ { OPERATORS[text_value] => value }
50
+ end
51
+ end
52
+ end
53
+
54
+ class UriRefEsc < GrammarNode
55
+ def to_mongo_query
56
+ # TODO: an angle bracket-delimited URI reference in which > and \ are \-escaped.
57
+ text_content
58
+ end
59
+ end
60
+
61
+ class BooleanValue < GrammarNode
62
+ def to_mongo_query
63
+ text_value == "true"
64
+ end
65
+ end
66
+
67
+ class StringEsc < GrammarNode
68
+ def to_mongo_query
69
+ text_value[1...-1]
70
+ end
71
+ end
72
+
73
+ class Condition < GrammarNode
74
+ def to_mongo_query
75
+ field = OSLC_FIELDS_MAPPING[elements[0].text_value]
76
+ value = elements[2].to_mongo_query
77
+ operator_with_value = elements[1].to_mongo_query_with_value(value)
78
+ { field => operator_with_value }
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,80 @@
1
+ # oslc_where ::= "oslc.where=" compound_term
2
+ # compound_term ::= simple_term (space? boolean_op space? simple_term)*
3
+ # simple_term ::= term | scoped_term
4
+ # space ::= " " /* a space character */
5
+ # boolean_op ::= "and"
6
+ # term ::= identifier_wc comparison_op value | identifier_wc space in_op space? in_val
7
+ # scoped_term ::= identifier_wc "{" compound_term "}"
8
+ # identifier_wc ::= identifier | wildcard
9
+ # identifier ::= PrefixedName
10
+ # PrefixedName ::= /* see "SPARQL Query Language for RDF", http://www.w3.org/TR/rdf-sparql-query/#rPrefixedName */
11
+ # wildcard ::= "*"
12
+ # comparison_op ::= "=" | "!=" | "<" | ">" | "<=" | ">="
13
+ # in_op ::= "in"
14
+ # in_val ::= "[" value ("," value)* "]"
15
+ # value ::= uri_ref_esc | literal_value
16
+ # uri_ref_esc ::= /* an angle bracket-delimited URI reference in which > and \ are \-escaped. */
17
+ # literal_value ::= boolean | decimal | string_esc (LANGTAG | ("^^" PrefixedName))?
18
+ # boolean ::= "true" | "false"
19
+ # decimal ::= /* see "XML Schema Part 2: Datatypes Second Edition", http://www.w3.org/TR/xmlschema-2/ */
20
+ # string_esc ::= /* a string enclosed in double quotes, with certain characters escaped. See below. */
21
+ # LANGTAG ::= /* see "SPARQL Query Language for RDF", http://www.w3.org/TR/rdf-sparql-query/#rLANGTAG */
22
+
23
+ grammar OslcGrammar
24
+
25
+ rule decision
26
+ binary_decision / simple_term
27
+ end
28
+
29
+ rule binary_decision
30
+ operand_1:simple_term space? boolean_op space? operand_2:decision <Mongoid::Oslc::OslcGrammar::BinaryDecision>
31
+ end
32
+
33
+ rule simple_term
34
+ term
35
+ end
36
+
37
+ rule term
38
+ field comparison_op value <Mongoid::Oslc::OslcGrammar::Condition>
39
+ end
40
+
41
+ rule boolean_op
42
+ 'and' <Mongoid::Oslc::OslcGrammar::LogicalOperator>
43
+ end
44
+
45
+ rule space
46
+ ' '
47
+ end
48
+
49
+ rule field
50
+ [\w\:]+ <Mongoid::Oslc::OslcGrammar::Field>
51
+ end
52
+
53
+ rule comparison_op
54
+ [\=\>\<\!] <Mongoid::Oslc::OslcGrammar::Operator>
55
+ end
56
+
57
+ rule value
58
+ literal_value / uri_ref_esc
59
+ end
60
+
61
+ rule uri_ref_esc
62
+ [\w]+ <Mongoid::Oslc::OslcGrammar::UriRefEsc>
63
+ end
64
+
65
+ rule literal_value
66
+ boolean / decimal / string_esc
67
+ end
68
+
69
+ rule boolean
70
+ 'true' <Mongoid::Oslc::OslcGrammar::BooleanValue> / 'false' <Mongoid::Oslc::OslcGrammar::BooleanValue>
71
+ end
72
+
73
+ rule decimal
74
+ [\d]+
75
+ end
76
+
77
+ rule string_esc
78
+ '"' [^\"]* '"' <Mongoid::Oslc::OslcGrammar::StringEsc>
79
+ end
80
+ end
@@ -0,0 +1,33 @@
1
+ require 'treetop'
2
+ require 'mongoid/oslc/oslc_grammar'
3
+
4
+ module Mongoid
5
+ module Oslc
6
+ class Strategy
7
+ attr_reader :parser
8
+
9
+ def self.to_query(query, *args)
10
+ options = { :verbose => true }
11
+ options.merge!(args.extract_options!)
12
+
13
+ Treetop.load File.join(File.dirname(__FILE__), "oslc_grammar")
14
+ @parser = OslcGrammarParser.new
15
+
16
+ tree = @parser.parse(query)
17
+ raise Search::QueryParsingError.new(query, @parser.index) if tree.nil?
18
+
19
+ clean_tree(tree)
20
+
21
+ tree.to_mongo_query
22
+ end
23
+
24
+ private
25
+
26
+ def self.clean_tree(root_node)
27
+ return if(root_node.elements.nil?)
28
+ root_node.elements.delete_if{|node| node.class.name == "Treetop::Runtime::SyntaxNode" }
29
+ root_node.elements.each {|node| self.clean_tree(node) }
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,3 @@
1
+ module Mongoid::Oslc
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,22 @@
1
+ require 'mongoid/oslc/strategy'
2
+
3
+ # OSLC
4
+ OSLC_FIELDS_MAPPING ||= {
5
+ "dcterms:title" => "title",
6
+ "dcterms:description" => "description",
7
+ "dcterms:created" => "created_at",
8
+ "oslc_cm:closed" => "closed"
9
+ }
10
+
11
+ module Mongoid
12
+ module Oslc
13
+ extend ActiveSupport::Concern
14
+
15
+ module ClassMethods
16
+ def oslc_where(query)
17
+ mongoid_query = Strategy.to_query(query)
18
+ where(mongoid_query)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,3 @@
1
+ require "mongoid"
2
+ require "mongoid/oslc/version"
3
+
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mongoid'
5
+ require 'mongoid/oslc/version'
6
+
7
+ Gem::Specification.new do |gem|
8
+ gem.name = "mongoid_oslc"
9
+ gem.version = Mongoid::Oslc::VERSION
10
+ gem.authors = ["Martin Magnusek"]
11
+ gem.email = ["magnusekm@gmail.com"]
12
+ gem.description = %q{Extension for mongoid to parse oslc.where}
13
+ gem.summary = ""
14
+ gem.homepage = ""
15
+
16
+ gem.add_runtime_dependency 'mongoid', ['<= 4.0', '>= 3.0']
17
+ gem.add_runtime_dependency 'treetop'
18
+
19
+ gem.add_development_dependency('rake', ['>= 0.9.2'])
20
+ gem.add_development_dependency('rspec', ['~> 2.8'])
21
+
22
+ gem.files = `git ls-files`.split($/)
23
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
24
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
25
+ gem.require_paths = ["lib"]
26
+ end
@@ -0,0 +1,39 @@
1
+ require 'spec_helper'
2
+
3
+ describe Mongoid::Oslc::Strategy do
4
+ let(:parser) { Mongoid::Oslc::Strategy }
5
+
6
+ def parse_query(query, verbose = true)
7
+ parser.to_query(query, :verbose => verbose)
8
+ end
9
+
10
+ it "has to_query method" do
11
+ parser.respond_to?(:to_query).should be_true
12
+ end
13
+
14
+ describe "#parse" do
15
+
16
+ it "should parse simple query" do
17
+ parse_query('dcterms:title="requirement"').should eql({"title"=>"requirement"})
18
+ end
19
+
20
+ it "should parse query with conjunction" do
21
+ parse_query('dcterms:title="requirement" and dcterms:description="first"').should eql({
22
+ "title"=>"requirement",
23
+ "description"=>"first"
24
+ })
25
+ end
26
+
27
+ it "should parse query with other comparison_op" do
28
+ parse_query('dcterms:created>"2010-04-01"').should eql({"created_at"=>{"$gt"=>"2010-04-01"}})
29
+ end
30
+
31
+ it "should parse query with boolean value" do
32
+ parse_query('oslc_cm:closed=true').should eql({"closed"=> true})
33
+ end
34
+
35
+ it "should parse query with boolean value" do
36
+ parse_query('oslc_cm:closed=false').should eql({"closed"=> false})
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ class MongoDocument
4
+ include Mongoid::Document
5
+ include Mongoid::Oslc
6
+
7
+ end
8
+
9
+ describe Mongoid::Oslc do
10
+ subject { MongoDocument.new }
11
+
12
+ describe "class" do
13
+ it "should have method #oslc_where" do
14
+ MongoDocument.respond_to?(:oslc_where).should be_true
15
+ end
16
+
17
+ it "should create criteria" do
18
+ query = MongoDocument.oslc_where('dcterms:title="Requirement"')
19
+ query.class.should eql Mongoid::Criteria
20
+ query.selector.should eql({"title" => "Requirement"})
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'bundler/setup'
3
+ require 'mongoid'
4
+ require 'mongoid/oslc'
5
+
6
+ require 'rspec'
7
+
8
+ Mongoid.configure do |config|
9
+ config.connect_to('mongoid_oslc_where_test')
10
+ end
11
+
12
+ RSpec.configure do |config|
13
+ config.mock_with :rspec
14
+ config.after(:each) { Mongoid.purge! }
15
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_oslc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Martin Magnusek
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mongoid
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - <=
20
+ - !ruby/object:Gem::Version
21
+ version: '4.0'
22
+ - - ! '>='
23
+ - !ruby/object:Gem::Version
24
+ version: '3.0'
25
+ type: :runtime
26
+ prerelease: false
27
+ version_requirements: !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - <=
31
+ - !ruby/object:Gem::Version
32
+ version: '4.0'
33
+ - - ! '>='
34
+ - !ruby/object:Gem::Version
35
+ version: '3.0'
36
+ - !ruby/object:Gem::Dependency
37
+ name: treetop
38
+ requirement: !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :runtime
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ none: false
48
+ requirements:
49
+ - - ! '>='
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ - !ruby/object:Gem::Dependency
53
+ name: rake
54
+ requirement: !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: 0.9.2
60
+ type: :development
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ! '>='
66
+ - !ruby/object:Gem::Version
67
+ version: 0.9.2
68
+ - !ruby/object:Gem::Dependency
69
+ name: rspec
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '2.8'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ~>
82
+ - !ruby/object:Gem::Version
83
+ version: '2.8'
84
+ description: Extension for mongoid to parse oslc.where
85
+ email:
86
+ - magnusekm@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - Gemfile
93
+ - LICENSE.txt
94
+ - README.md
95
+ - Rakefile
96
+ - lib/mongoid/oslc.rb
97
+ - lib/mongoid/oslc/oslc_grammar.rb
98
+ - lib/mongoid/oslc/oslc_grammar.treetop
99
+ - lib/mongoid/oslc/strategy.rb
100
+ - lib/mongoid/oslc/version.rb
101
+ - lib/mongoid_oslc.rb
102
+ - mongoid_oslc.gemspec
103
+ - spec/lib/mongoid/oslc/strategy_spec.rb
104
+ - spec/lib/mongoid/oslc_spec.rb
105
+ - spec/spec_helper.rb
106
+ homepage: ''
107
+ licenses: []
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ segments:
119
+ - 0
120
+ hash: -3408470218474486260
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ none: false
123
+ requirements:
124
+ - - ! '>='
125
+ - !ruby/object:Gem::Version
126
+ version: '0'
127
+ segments:
128
+ - 0
129
+ hash: -3408470218474486260
130
+ requirements: []
131
+ rubyforge_project:
132
+ rubygems_version: 1.8.24
133
+ signing_key:
134
+ specification_version: 3
135
+ summary: ''
136
+ test_files:
137
+ - spec/lib/mongoid/oslc/strategy_spec.rb
138
+ - spec/lib/mongoid/oslc_spec.rb
139
+ - spec/spec_helper.rb