fedora_lens 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4aed41f4f03e56d39025a4c4afc83c2f104fbe13
4
- data.tar.gz: 67a75716a078d141c39762c027683aa815bd85bf
3
+ metadata.gz: 8ddccef7b4b1d4c0bf21e0afe5596f375b79ae40
4
+ data.tar.gz: f979c71a8daa29d9e291e6c2c06d4c3abd7c9223
5
5
  SHA512:
6
- metadata.gz: 5b625651f21a93d934616e81deb447c1a3946fa146512a6d2ff15b737a95cab4fa1dfe0c7e4ad692a11dcb365d5dd5250432a5c72e98573a161df36671a31003
7
- data.tar.gz: d70beed3541744109be8e91cc1aec93e40ebfc9e20b70a3df828f8476cb97b510ea96914ea62339abc75353135032a6e589daa6d4cf68ce700dcbea828fbc59f
6
+ metadata.gz: 8fef1c7a9b0b4bc6739d003278978791705661bec3dd8722cdbfa1e8c34f8f0b01700fa1bc39da55dc71258a3d82b2ddc74ee1c030b197651809af67b619cd79
7
+ data.tar.gz: 117fcc9c8a4236d629d2517a77c6b61ee84f004064d4d0c6abd1df77160faa2eb51ad8902c7d2a19c02ddc8f85bfd9cdcafb9d446ddb9b6bc70c4055363063d7
@@ -1,222 +1,14 @@
1
- require 'rdf'
2
- require 'ldp'
3
- require 'rdf/turtle'
4
- require 'nokogiri'
5
1
  require 'active_model'
6
- require 'active_support/concern'
7
- require 'active_support/core_ext/object'
8
- require 'active_support/core_ext/class/attribute'
9
- require 'active_support/core_ext/module/attribute_accessors'
10
- require 'active_support/core_ext/hash'
11
- require 'fedora_lens/errors'
2
+ require 'fedora_lens/core'
12
3
 
13
4
  module FedoraLens
14
5
  extend ActiveSupport::Concern
15
- extend ActiveSupport::Autoload
16
- autoload :AttributeMethods
17
-
18
- module AttributeMethods
19
- extend ActiveSupport::Autoload
20
-
21
- eager_autoload do
22
- autoload :Declarations
23
- autoload :Read
24
- autoload :Write
25
- end
26
- end
27
-
28
- HOST = "http://localhost:8983/fedora/rest"
29
-
30
- class << self
31
- def connection
32
- @@connection ||= Ldp::Client.new(host)
33
- end
34
-
35
- def host
36
- HOST
37
- end
38
-
39
- def base_path
40
- @@base_path ||= ''
41
- end
42
-
43
- # Set a base path if you want to put all your objects below a certain path
44
- # example:
45
- # FedoraLens.base_path = '/text
46
- def base_path= path
47
- @@base_path = path
48
- end
49
-
50
- def id_to_uri(id)
51
- id = "/#{id}" unless id.start_with? '/'
52
- id = FedoraLens.base_path + id unless id.start_with? "#{FedoraLens.base_path}/"
53
- FedoraLens.host + id
54
- end
55
-
56
- def uri_to_id(uri)
57
- uri.to_s.sub(FedoraLens.host + FedoraLens.base_path, '')
58
- end
59
- end
60
6
 
61
7
  included do
62
8
  extend ActiveModel::Naming
63
9
  include ActiveModel::Validations
64
10
  include ActiveModel::Conversion
65
- include FedoraLens::AttributeMethods
66
-
67
- attr_reader :orm
68
- end
69
-
70
- def initialize(subject_or_data = {}, data = nil)
71
- init_core(subject_or_data, data)
72
- end
73
-
74
- def persisted?() false end
75
-
76
- def errors
77
- obj = Object.new
78
- def obj.[](key) [] end
79
- def obj.full_messages() [] end
80
- obj
81
- end
82
-
83
- def read_attribute_for_validation(key)
84
- @attributes[key]
85
- end
86
-
87
- def reload
88
- @orm = @orm.reload
89
- @attributes = get_attributes_from_orm(@orm)
90
- end
91
-
92
- def delete
93
- @orm.resource.delete
94
- end
95
-
96
- def save
97
- new_record? ? create_record : update_record
98
- end
99
-
100
- def save!
101
- save || raise(RecordNotSaved)
102
- end
103
-
104
- def new_record?
105
- @orm.resource.new?
106
- end
107
-
108
- def uri
109
- @orm.try(:resource).try(:subject_uri).try(:to_s)
110
- end
111
-
112
- def id
113
- self.class.uri_to_id(URI.parse(uri)) if uri.present?
114
- end
115
-
116
- protected
117
- # This allows you to overide the initializer, but still use this behavior
118
- def init_core(subject_or_data = {}, data = nil)
119
- case subject_or_data
120
- when Ldp::Resource::RdfSource
121
- @orm = Ldp::Orm.new(subject_or_data)
122
- @attributes = get_attributes_from_orm(@orm)
123
- when NilClass, Hash
124
- data = subject_or_data || {}
125
- @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, nil, RDF::Graph.new, FedoraLens.host + FedoraLens.base_path))
126
- @attributes = data.with_indifferent_access
127
- when String
128
- data ||= {}
129
- @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, subject_or_data, RDF::Graph.new))
130
- @attributes = data.with_indifferent_access
131
- else
132
- raise ArgumentError, "#{subject_or_data.class} is not acceptable"
133
- end
134
- end
135
-
136
- def create_record
137
- push_attributes_to_orm
138
- create_and_fetch_attributes
139
- true
140
- end
141
-
142
- def update_record
143
- push_attributes_to_orm
144
- update_and_fetch_attributes
145
- end
146
-
147
-
148
- private
149
-
150
- def update_and_fetch_attributes
151
- orm.save!.tap do
152
- clear_cached_response
153
- # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#lastModified
154
- # TODO perhaps attributes could be lazily fetched
155
- @attributes = get_attributes_from_orm(@orm)
156
- clear_cached_response
157
- end
158
- end
159
-
160
- def create_and_fetch_attributes
161
- @orm = orm.create
162
- # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#created
163
- # TODO perhaps attributes could be lazily fetched
164
- @attributes = get_attributes_from_orm(@orm)
165
- clear_cached_response
166
- end
167
-
168
- # TODO this causes slowness because we're losing the cached GET response. However it prevents ETag exceptions caused when a
169
- # subnode is added before an update.
170
- def clear_cached_response
171
- # This strips the current cached response (and ETag) from the current ORM to avoid ETag exceptions on the next update,
172
- # since the etag will change if a child node is added.
173
- @orm = Ldp::Orm.new @orm.resource.class.new @orm.resource.client, @orm.resource.subject
174
- end
175
-
176
-
177
- def push_attributes_to_orm
178
- @orm = self.class.orm_to_hash.put(@orm, @attributes)
179
- end
180
-
181
- def get_attributes_from_orm(orm)
182
- self.class.orm_to_hash.get(orm).with_indifferent_access
183
- end
184
-
185
- module ClassMethods
186
- def find(id)
187
- resource = Ldp::Resource::RdfSource.new(FedoraLens.connection, id_to_uri(id))
188
- raise Ldp::NotFound if resource.new?
189
- self.new(resource)
190
- end
191
-
192
- def id_to_uri(id)
193
- FedoraLens.id_to_uri(id)
194
- end
195
-
196
- def uri_to_id(uri)
197
- FedoraLens.uri_to_id(uri)
198
- end
199
-
200
- def create(data)
201
- model = self.new(data)
202
- model.save
203
- model
204
- end
205
-
206
- def orm_to_hash
207
- if @orm_to_hash.nil?
208
- aggregate_lens = attributes_as_lenses.inject({}) do |acc, (name, path)|
209
- lens = path.inject {|outer, inner| Lenses.compose(outer, inner)}
210
- acc.merge(name => lens)
211
- end
212
- @orm_to_hash = Lenses.orm_to_hash(aggregate_lens)
213
- end
214
- @orm_to_hash
215
- end
216
-
217
- # def has_one(name, scope = nil, options = {})
218
- # ActiveRecord::Associations::Builder::HasOne.build(self, name, scope, options)
219
- # end
11
+ include FedoraLens::Core
220
12
  end
221
13
  end
222
14
 
@@ -0,0 +1,220 @@
1
+ require 'rdf'
2
+ require 'ldp'
3
+ require 'active_support/concern'
4
+ require 'active_support/core_ext/object'
5
+ require 'active_support/core_ext/class/attribute'
6
+ require 'active_support/core_ext/module/attribute_accessors'
7
+ require 'active_support/core_ext/hash'
8
+ require 'fedora_lens/errors'
9
+
10
+ module FedoraLens
11
+ extend ActiveSupport::Autoload
12
+ autoload :AttributeMethods
13
+
14
+ module AttributeMethods
15
+ extend ActiveSupport::Autoload
16
+
17
+ eager_autoload do
18
+ autoload :Declarations
19
+ autoload :Read
20
+ autoload :Write
21
+ end
22
+ end
23
+
24
+ HOST = "http://localhost:8983/fedora/rest"
25
+
26
+ class << self
27
+ def connection
28
+ @@connection ||= Ldp::Client.new(host)
29
+ end
30
+
31
+ def host
32
+ HOST
33
+ end
34
+
35
+ def base_path
36
+ @@base_path ||= ''
37
+ end
38
+
39
+ # Set a base path if you want to put all your objects below a certain path
40
+ # example:
41
+ # FedoraLens.base_path = '/text
42
+ def base_path= path
43
+ @@base_path = path
44
+ end
45
+
46
+ def id_to_uri(id)
47
+ id = "/#{id}" unless id.start_with? '/'
48
+ id = FedoraLens.base_path + id unless id.start_with? "#{FedoraLens.base_path}/"
49
+ FedoraLens.host + id
50
+ end
51
+
52
+ def uri_to_id(uri)
53
+ uri.to_s.sub(FedoraLens.host + FedoraLens.base_path, '')
54
+ end
55
+ end
56
+
57
+ module Core
58
+ extend ActiveSupport::Concern
59
+
60
+ included do
61
+ include FedoraLens::AttributeMethods
62
+ attr_reader :orm
63
+ end
64
+
65
+ def initialize(subject_or_data = {}, data = nil)
66
+ init_core(subject_or_data, data)
67
+ end
68
+
69
+ def persisted?() false end
70
+
71
+ def errors
72
+ obj = Object.new
73
+ def obj.[](key) [] end
74
+ def obj.full_messages() [] end
75
+ obj
76
+ end
77
+
78
+ def read_attribute_for_validation(key)
79
+ @attributes[key]
80
+ end
81
+
82
+ def reload
83
+ @orm = @orm.reload
84
+ @attributes = get_attributes_from_orm(@orm)
85
+ end
86
+
87
+ def delete
88
+ @orm.resource.delete
89
+ end
90
+
91
+ def save
92
+ new_record? ? create_record : update_record
93
+ end
94
+
95
+ def save!
96
+ save || raise(RecordNotSaved)
97
+ end
98
+
99
+ def new_record?
100
+ @orm.resource.new?
101
+ end
102
+
103
+ def uri
104
+ @orm.try(:resource).try(:subject_uri).try(:to_s)
105
+ end
106
+
107
+ def id
108
+ self.class.uri_to_id(URI.parse(uri)) if uri.present?
109
+ end
110
+
111
+ protected
112
+ # This allows you to overide the initializer, but still use this behavior
113
+ def init_core(subject_or_data = {}, data = nil)
114
+ case subject_or_data
115
+ when Ldp::Resource::RdfSource
116
+ @orm = Ldp::Orm.new(subject_or_data)
117
+ @attributes = get_attributes_from_orm(@orm)
118
+ when NilClass, Hash
119
+ data = subject_or_data || {}
120
+ @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, nil, RDF::Graph.new, FedoraLens.host + FedoraLens.base_path))
121
+ @attributes = data.with_indifferent_access
122
+ when String
123
+ data ||= {}
124
+ @orm = Ldp::Orm.new(Ldp::Resource::RdfSource.new(FedoraLens.connection, subject_or_data, RDF::Graph.new))
125
+ @attributes = data.with_indifferent_access
126
+ else
127
+ raise ArgumentError, "#{subject_or_data.class} is not acceptable"
128
+ end
129
+ end
130
+
131
+ def create_record
132
+ push_attributes_to_orm
133
+ create_and_fetch_attributes
134
+ true
135
+ end
136
+
137
+ def update_record
138
+ push_attributes_to_orm
139
+ update_and_fetch_attributes
140
+ end
141
+
142
+
143
+ private
144
+
145
+ def update_and_fetch_attributes
146
+ orm.save!.tap do
147
+ clear_cached_response
148
+ # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#lastModified
149
+ # TODO perhaps attributes could be lazily fetched
150
+ @attributes = get_attributes_from_orm(@orm)
151
+ clear_cached_response
152
+ end
153
+ end
154
+
155
+ def create_and_fetch_attributes
156
+ @orm = orm.create
157
+ # This is slow, but it enables us to get attributes like http://fedora.info/definitions/v4/repository#created
158
+ # TODO perhaps attributes could be lazily fetched
159
+ @attributes = get_attributes_from_orm(@orm)
160
+ clear_cached_response
161
+ end
162
+
163
+ # TODO this causes slowness because we're losing the cached GET response. However it prevents ETag exceptions caused when a
164
+ # subnode is added before an update.
165
+ def clear_cached_response
166
+ # This strips the current cached response (and ETag) from the current ORM to avoid ETag exceptions on the next update,
167
+ # since the etag will change if a child node is added.
168
+ @orm = Ldp::Orm.new @orm.resource.class.new @orm.resource.client, @orm.resource.subject
169
+ end
170
+
171
+
172
+ def push_attributes_to_orm
173
+ @orm = self.class.orm_to_hash.put(@orm, @attributes)
174
+ end
175
+
176
+ def get_attributes_from_orm(orm)
177
+ self.class.orm_to_hash.get(orm).with_indifferent_access
178
+ end
179
+
180
+ module ClassMethods
181
+ def find(id)
182
+ resource = Ldp::Resource::RdfSource.new(FedoraLens.connection, id_to_uri(id))
183
+ raise Ldp::NotFound if resource.new?
184
+ self.new(resource)
185
+ end
186
+
187
+ def id_to_uri(id)
188
+ FedoraLens.id_to_uri(id)
189
+ end
190
+
191
+ def uri_to_id(uri)
192
+ FedoraLens.uri_to_id(uri)
193
+ end
194
+
195
+ def create(data)
196
+ model = self.new(data)
197
+ model.save
198
+ model
199
+ end
200
+
201
+ def orm_to_hash
202
+ if @orm_to_hash.nil?
203
+ aggregate_lens = attributes_as_lenses.inject({}) do |acc, (name, path)|
204
+ lens = path.inject {|outer, inner| Lenses.compose(outer, inner)}
205
+ acc.merge(name => lens)
206
+ end
207
+ @orm_to_hash = Lenses.orm_to_hash(aggregate_lens)
208
+ end
209
+ @orm_to_hash
210
+ end
211
+
212
+ # def has_one(name, scope = nil, options = {})
213
+ # ActiveRecord::Associations::Builder::HasOne.build(self, name, scope, options)
214
+ # end
215
+ end
216
+ end
217
+ end
218
+
219
+ require 'fedora_lens/lenses'
220
+
@@ -1,3 +1,3 @@
1
1
  module FedoraLens
2
- VERSION = '0.0.11'
2
+ VERSION = '0.0.12'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fedora_lens
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Justin Coyne
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-07-29 00:00:00.000000000 Z
12
+ date: 2014-07-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -151,6 +151,7 @@ files:
151
151
  - lib/fedora_lens/attribute_methods/declarations.rb
152
152
  - lib/fedora_lens/attribute_methods/read.rb
153
153
  - lib/fedora_lens/attribute_methods/write.rb
154
+ - lib/fedora_lens/core.rb
154
155
  - lib/fedora_lens/errors.rb
155
156
  - lib/fedora_lens/lens.rb
156
157
  - lib/fedora_lens/lens_tests.rb