mongoid_oslc 0.0.9 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,8 +17,7 @@ module Mongoid::Oslc
17
17
  # need a Class for 3.0
18
18
  class Configuration #:nodoc:
19
19
  include ActiveSupport::Configurable
20
- config_accessor :default_namespace
21
- config_accessor :fields_mapping
20
+ config_accessor :default_namespaces
22
21
 
23
22
  def param_name
24
23
  config.param_name.respond_to?(:call) ? config.param_name.call : config.param_name
@@ -32,12 +31,6 @@ module Mongoid::Oslc
32
31
 
33
32
  # this is ugly. why can't we pass the default value to config_accessor...?
34
33
  configure do |config|
35
- config.default_namespace = :rtc
36
- config.fields_mapping = {
37
- "dcterms:title" => "title",
38
- "dcterms:description" => "description",
39
- "dcterms:created" => "created_at",
40
- "oslc_cm:closed" => "closed"
41
- }
34
+ config.default_namespaces = [:rtc]
42
35
  end
43
36
  end
@@ -116,8 +116,8 @@ module Mongoid
116
116
  private
117
117
 
118
118
  def field_for(oslc_field)
119
- field = Mongoid::Oslc.config.fields_mapping[oslc_field]
120
- field ||= oslc_field.split(":")[1] if oslc_field.start_with?(Mongoid::Oslc.config.default_namespace.to_s)
119
+ field = Mongoid::Oslc.resources.mapping[oslc_field].try(:fetch, :name)
120
+ field ||= oslc_field.split(":")[1] if Mongoid::Oslc.config.default_namespaces.include?(oslc_field.split(":")[0])
121
121
  field
122
122
  end
123
123
  end
@@ -0,0 +1,21 @@
1
+ module Mongoid
2
+ module Oslc
3
+ class Resources < Hash
4
+ def initialize
5
+ super{|h, k| h[k] = {fields: {}}}
6
+ end
7
+
8
+ def class_for(rdf_type)
9
+ self.select{|klass,properties| properties[:describe] == rdf_type}.keys.first
10
+ end
11
+
12
+ def domain(rdf_type)
13
+ self[class_for(rdf_type)][:domain]
14
+ end
15
+
16
+ def mapping
17
+ self.inject({}){|hash, klass| hash.merge(klass[1][:fields]) }
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,5 +1,5 @@
1
1
  module Mongoid
2
2
  module Oslc
3
- VERSION = '0.0.9'
3
+ VERSION = '0.1.0'
4
4
  end
5
5
  end
data/lib/mongoid/oslc.rb CHANGED
@@ -1,19 +1,97 @@
1
1
  require 'mongoid'
2
+ require 'oslc'
2
3
  require 'mongoid/oslc/config'
3
4
  require 'mongoid/oslc/version'
4
5
  require 'mongoid/oslc/exceptions'
5
6
  require 'mongoid/oslc/strategy'
7
+ require 'mongoid/oslc/resources'
6
8
  require 'active_support/concern'
7
9
 
8
10
  module Mongoid
9
11
  module Oslc
10
12
  extend ActiveSupport::Concern
11
13
 
14
+ @@resources = Resources.new
15
+
16
+ def self.resources
17
+ @@resources
18
+ end
19
+
20
+ def domain
21
+ Mongoid::Oslc.resources[self._type][:domain]
22
+ end
23
+
24
+ def describe
25
+ Mongoid::Oslc.resources[self._type][:describe]
26
+ end
27
+
28
+ def oslc_value(oslc_name)
29
+ field_properties = Mongoid::Oslc.resources[self._type][:fields][oslc_name]
30
+ self[field_properties[:name]]
31
+ end
32
+
12
33
  module ClassMethods
34
+ def domain(name)
35
+ Mongoid::Oslc.resources[self.to_s].merge!(domain: name)
36
+ end
37
+
38
+ def describe(describe)
39
+ Mongoid::Oslc.resources[self.to_s].merge!(describe: describe)
40
+ end
41
+
42
+ def oslc_field(name, options = {})
43
+ oslc_options = options_for_oslc(options)
44
+ field(name, options)
45
+ register_oslc_field(name, oslc_options)
46
+ end
47
+
48
+ def oslc_embeds_many(name, options = {})
49
+ oslc_options = options_for_oslc(options)
50
+ embeds_many(name, options)
51
+ register_oslc_field(name, oslc_options.merge(type: Array, embedded: true))
52
+ end
53
+
54
+ def oslc_fields
55
+ (ancestors - included_modules).inject({}) do |hash, ancestor_class|
56
+ hash.merge(Mongoid::Oslc.resources[ancestor_class.to_s][:fields])
57
+ end
58
+ end
59
+
13
60
  def oslc_where(query)
14
61
  mongoid_query = Strategy.to_query(query)
15
62
  where(mongoid_query)
16
63
  end
64
+
65
+ 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]] = {
78
+ name: name,
79
+ value_type: ::Oslc::VALUE_TYPES[options[:value_type] || fields[name.to_s].type.to_s],
80
+ read_only: (options[:read_only] || false),
81
+ occurs: occurs,
82
+ allowed_values: (options[:allowed_values] || []),
83
+ embedded: (options[:embedded] ? relations[name.to_s].class_name.constantize : false),
84
+ range: (options[:range] || [])
85
+ }
86
+ end
87
+
88
+ private
89
+
90
+ def options_for_oslc(options)
91
+ [:value_type, :read_only, :allowed_values, :mapping, :mandatory, :range].inject({}) do |hash, field|
92
+ hash.merge(field => options.delete(field))
93
+ end.merge(type: options[:type])
94
+ end
17
95
  end
18
96
  end
19
97
  end
data/mongoid_oslc.gemspec CHANGED
@@ -13,6 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.add_runtime_dependency 'activesupport', '~> 3.0'
14
14
  gem.add_runtime_dependency 'mongoid', '>= 3.0', '< 4.0'
15
15
  gem.add_runtime_dependency 'treetop'
16
+ gem.add_runtime_dependency 'oslc'
16
17
 
17
18
  gem.add_development_dependency('rake', ['>= 0.9.2'])
18
19
  gem.add_development_dependency('rspec', ['~> 2.8'])
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.0.9
4
+ version: 0.1.0
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-11-15 00:00:00.000000000 Z
12
+ date: 2012-12-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -65,6 +65,22 @@ dependencies:
65
65
  - - ! '>='
66
66
  - !ruby/object:Gem::Version
67
67
  version: '0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: oslc
70
+ requirement: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ none: false
80
+ requirements:
81
+ - - ! '>='
82
+ - !ruby/object:Gem::Version
83
+ version: '0'
68
84
  - !ruby/object:Gem::Dependency
69
85
  name: rake
70
86
  requirement: !ruby/object:Gem::Requirement
@@ -114,6 +130,7 @@ files:
114
130
  - lib/mongoid/oslc/exceptions.rb
115
131
  - lib/mongoid/oslc/grammar.rb
116
132
  - lib/mongoid/oslc/grammar.treetop
133
+ - lib/mongoid/oslc/resources.rb
117
134
  - lib/mongoid/oslc/strategy.rb
118
135
  - lib/mongoid/oslc/version.rb
119
136
  - lib/mongoid_oslc.rb
@@ -133,18 +150,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
133
150
  - - ! '>='
134
151
  - !ruby/object:Gem::Version
135
152
  version: '0'
136
- segments:
137
- - 0
138
- hash: 2215901431613586552
139
153
  required_rubygems_version: !ruby/object:Gem::Requirement
140
154
  none: false
141
155
  requirements:
142
156
  - - ! '>='
143
157
  - !ruby/object:Gem::Version
144
158
  version: '0'
145
- segments:
146
- - 0
147
- hash: 2215901431613586552
148
159
  requirements: []
149
160
  rubyforge_project:
150
161
  rubygems_version: 1.8.24