Module: FedoraLens::Core::ClassMethods

Defined in:
lib/fedora_lens/core.rb

Instance Method Summary collapse

Instance Method Details

#create(data) ⇒ Object


189
190
191
192
193
# File 'lib/fedora_lens/core.rb', line 189

def create(data)
  model = self.new(data)
  model.save
  model
end

#find(id) ⇒ Object

Raises:

  • (Ldp::NotFound)

172
173
174
175
176
# File 'lib/fedora_lens/core.rb', line 172

def find(id)
  resource = Ldp::Resource::RdfSource.new(FedoraLens.connection, id_to_uri(id))
  raise Ldp::NotFound if resource.new?
  self.new(resource)
end

#id_to_uri(id) ⇒ Object


178
179
180
181
182
# File 'lib/fedora_lens/core.rb', line 178

def id_to_uri(id)
  id = "/#{id}" unless id.start_with? '/'
  id = FedoraLens.base_path + id unless id.start_with? "#{FedoraLens.base_path}/"
  FedoraLens.host + id
end

#orm_to_hashObject


195
196
197
198
199
200
201
202
203
204
# File 'lib/fedora_lens/core.rb', line 195

def orm_to_hash
  if @orm_to_hash.nil?
    aggregate_lens = attributes_as_lenses.inject({}) do |acc, (name, path)|
      lens = path.inject {|outer, inner| Lenses.compose(outer, inner)}
      acc.merge(name => lens)
    end
    @orm_to_hash = Lenses.orm_to_hash(aggregate_lens)
  end
  @orm_to_hash
end

#uri_to_id(uri) ⇒ Object


184
185
186
187
# File 'lib/fedora_lens/core.rb', line 184

def uri_to_id(uri)
  id = uri.to_s.sub(FedoraLens.host + FedoraLens.base_path, '')
  id.start_with?('/') ? id[1..-1] : id
end