infopark_cloud_connector 6.8.0.328.4a753fc → 6.8.0.348.160665197

Sign up to get free protection for your applications and to get access to all the features.
@@ -50,17 +50,19 @@ module RailsConnector
50
50
  end
51
51
  end
52
52
 
53
- # (notice: not yet implemented)
54
- # Returns a list of all Objs.
53
+ # Returns a {ObjSearchEnumerator} of all Objs.
55
54
  # If invoked on a subclass of Obj, the result will be restricted to Obj of that subclass.
56
55
  def self.all
57
- raise "not yet implemented!"
56
+ if self.name == 'RailsConnector::Obj'
57
+ ObjSearchEnumerator.new(nil)
58
+ else
59
+ find_all_by_obj_class(self.name)
60
+ end
58
61
  end
59
62
 
60
- # (notice: not yet implemented)
61
- # returns an Array of all Objs with the given obj_class.
63
+ # Returns an {ObjSearchEnumerator} of all Objs with the given obj_class.
62
64
  def self.find_all_by_obj_class(obj_class)
63
- raise "not yet implemented!"
65
+ ObjSearchEnumerator.new([{:field => '_obj_class', :operator => 'equal', :value => obj_class}])
64
66
  end
65
67
 
66
68
  # Find the Obj with the given path.
@@ -74,18 +76,18 @@ module RailsConnector
74
76
  find_objs_by(:path, pathes).map(&:first)
75
77
  end
76
78
 
77
- # (notice: not yet implemented)
78
79
  # Find an Obj with the given name.
79
80
  # If several Objs exist with the given name, one of them is chosen and returned.
80
81
  # If no Obj with the name exits, nil is returned.
81
82
  def self.find_by_name(name)
82
- raise "not yet implemented!"
83
+ enum = ObjSearchEnumerator.new(
84
+ [{:field => '_name', :operator => 'equal', :value => name}], {:batch_size => 1})
85
+ enum.first
83
86
  end
84
87
 
85
- # (notice: not yet implemented)
86
- # Find all Objs with the given name.
88
+ # Returns an {ObjSearchEnumerator} of all Objs with the given name.
87
89
  def self.find_all_by_name(name)
88
- raise "not yet implemented!"
90
+ ObjSearchEnumerator.new([{:field => '_name', :operator => 'equal', :value => name}])
89
91
  end
90
92
 
91
93
  # Return the Obj with the given permalink or nil if no matching Obj exists.
@@ -0,0 +1,83 @@
1
+ module RailsConnector
2
+ # Provides an enumerator, to iterate over obj search results and return "real" objs.
3
+ # This is done using the {http://ruby-doc.org/core-1.8.7/Enumerable.html <code>Enumerable</code> mixin}, which provided methods like <code>map</code>, <code>select</code> or <code>take</code>.
4
+ #
5
+ # This enumerator is lazy. Let's say, that when looking for Objs with the ObjClass Publication there are 123 objs in total. Now <code>enum.take(10)</code> will only fetch the first 10 objs, leaving the other 113 objs untouched.
6
+ # This also means, that iterating multiple times over this enumerator, each time the search results and objs are fetched again. If you want to get all objs at once, use <code>enum.to_a</code>.
7
+ #
8
+ # For all possible <code>query</code>'s and <code>options</code> have a look at the cms api documentation about objs/search.
9
+ # There is one exception: To adjust how many search results should be fetch with one chunk, use the option <code>:batch_size</code>. The default is <code>10</code>. The <code>:size</code> option will be silently ignored.
10
+ #
11
+ # @example All objs, which are Publications
12
+ # enum = ObjSearchEnumerator([{:field => '_obj_class', :operator => 'equal', :value => 'Publication'}])
13
+ # enum.each { |obj| puts obj.path }
14
+ class ObjSearchEnumerator
15
+ include Enumerable
16
+
17
+ attr_reader :query
18
+ attr_reader :options
19
+
20
+ # @param [Array] query
21
+ # @param [Hash] options
22
+ def initialize(query, options = {})
23
+ @query = query
24
+ @query.freeze
25
+
26
+ options.delete(:size)
27
+ options.delete('size')
28
+ if batch_size = options.delete(:batch_size)
29
+ options[:size] = batch_size
30
+ end
31
+ @options = options
32
+ @options.freeze
33
+ end
34
+
35
+ def each
36
+ offset = 0
37
+ current_batch, total = fetch_next_batch(offset)
38
+ loop do
39
+ if current_batch.size == 0
40
+ if offset < total
41
+ current_batch, total = fetch_next_batch(offset)
42
+ else
43
+ raise StopIteration
44
+ end
45
+ end
46
+
47
+ offset += 1
48
+ hit = current_batch.shift
49
+ yield Obj.find(hit['id'])
50
+ end
51
+ end
52
+
53
+ # The total size of all results.
54
+ # @return [Integer]
55
+ def size
56
+ if @size.blank?
57
+ @size = CmsRestApi.get(resource_path, {:query => query, :size => 1})['total'].to_i
58
+ end
59
+ @size
60
+ end
61
+
62
+ alias_method :length, :size
63
+
64
+ private
65
+
66
+ def fetch_next_batch(offset)
67
+ request_result = CmsRestApi.get(resource_path, search_dsl(offset))
68
+
69
+ [request_result['results'], request_result['total'].to_i]
70
+ end
71
+
72
+ def resource_path
73
+ "revisions/#{Workspace.current.revision_id}/objs/search"
74
+ end
75
+
76
+ def search_dsl(offset)
77
+ options.merge({
78
+ :offset => offset,
79
+ :query => query,
80
+ })
81
+ end
82
+ end
83
+ end
metadata CHANGED
@@ -1,18 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infopark_cloud_connector
3
3
  version: !ruby/object:Gem::Version
4
- hash: 383871886
5
- prerelease: 11
4
+ hash: 321329493
5
+ prerelease:
6
6
  segments:
7
7
  - 6
8
8
  - 8
9
9
  - 0
10
- - 328
11
- - 4
12
- - a
13
- - 753
14
- - fc
15
- version: 6.8.0.328.4a753fc
10
+ - 348
11
+ - 160665197
12
+ version: 6.8.0.348.160665197
16
13
  platform: ruby
17
14
  authors:
18
15
  - Infopark AG
@@ -20,7 +17,7 @@ autorequire:
20
17
  bindir: bin
21
18
  cert_chain: []
22
19
 
23
- date: 2012-11-16 00:00:00 +01:00
20
+ date: 2012-11-23 00:00:00 +01:00
24
21
  default_executable:
25
22
  dependencies:
26
23
  - !ruby/object:Gem::Dependency
@@ -60,17 +57,14 @@ dependencies:
60
57
  requirements:
61
58
  - - "="
62
59
  - !ruby/object:Gem::Version
63
- hash: 383871886
60
+ hash: 321329493
64
61
  segments:
65
62
  - 6
66
63
  - 8
67
64
  - 0
68
- - 328
69
- - 4
70
- - a
71
- - 753
72
- - fc
73
- version: 6.8.0.328.4a753fc
65
+ - 348
66
+ - 160665197
67
+ version: 6.8.0.348.160665197
74
68
  version_requirements: *id003
75
69
  name: kvom
76
70
  prerelease: false
@@ -114,6 +108,7 @@ files:
114
108
  - lib/rails_connector/obj_data_from_database.rb
115
109
  - lib/rails_connector/obj_data_from_hash.rb
116
110
  - lib/rails_connector/obj_data_from_service.rb
111
+ - lib/rails_connector/obj_search_enumerator.rb
117
112
  - lib/rails_connector/path_conversion.rb
118
113
  - lib/rails_connector/permission.rb
119
114
  - lib/rails_connector/rack_middlewares.rb