Module: Hydra::Models::FileAsset
- Extended by:
- ActiveSupport::Concern
- Included in:
- FileAsset
- Defined in:
- lib/hydra/models/file_asset.rb
Overview
Defines Behaviors for a FileAsset Model Conforms to the Hydra genericContent cModel – the “content” is in a datastream called content TODO: Could we just move this behavior into app/models/file_asset.rb?
Instance Method Summary collapse
-
#add_file_datastream(file, opts = {}) ⇒ Object
augments add_file_datastream to also put file size (in bytes/KB/MB/GB/TB) in dc:extent.
-
#bits_to_human_readable(num) ⇒ Object
Returns a human readable filesize appropriate for the given number of bytes (ie. automatically chooses ‘bytes’,‘KB’,‘MB’,‘GB’,‘TB’) Based on a bit of python code posted here: blogmag.net/blog/read/38/Print_human_readable_file_size.
- #label ⇒ Object
- #label=(label) ⇒ Object
-
#to_solr(solr_doc = Hash.new, opts = {}) ⇒ Object
Override ActiveFedora::Base.to_solr to…
Instance Method Details
#add_file_datastream(file, opts = {}) ⇒ Object
augments add_file_datastream to also put file size (in bytes/KB/MB/GB/TB) in dc:extent
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/hydra/models/file_asset.rb', line 42 def add_file_datastream(file, opts={}) super if file.respond_to?(:size) size = bits_to_human_readable(file.size) elsif file.kind_of?(File) size = bits_to_human_readable(File.size(file)) else size = "" end datastreams["descMetadata"].extent = size end |
#bits_to_human_readable(num) ⇒ Object
Returns a human readable filesize appropriate for the given number of bytes (ie. automatically chooses ‘bytes’,‘KB’,‘MB’,‘GB’,‘TB’) Based on a bit of python code posted here: blogmag.net/blog/read/38/Print_human_readable_file_size
21 22 23 24 25 26 27 28 29 |
# File 'lib/hydra/models/file_asset.rb', line 21 def bits_to_human_readable(num) ['bytes','KB','MB','GB','TB'].each do |x| if num < 1024.0 return "#{num.to_i} #{x}" else num = num/1024.0 end end end |
#label ⇒ Object
32 33 34 |
# File 'lib/hydra/models/file_asset.rb', line 32 def label descMetadata.title.first end |
#label=(label) ⇒ Object
36 37 38 39 |
# File 'lib/hydra/models/file_asset.rb', line 36 def label=(label) super descMetadata.title = label end |
#to_solr(solr_doc = Hash.new, opts = {}) ⇒ Object
Override ActiveFedora::Base.to_solr to… Check if we are dealing with a child of FileAsset and if so when calling to_solr from Solrizer indexer we want to skip loading parent metadata again
if known models greater than one (without ActiveFedora::Base) and
known models contains a child of FileAsset and
opts[:model_only] == true and
current object class is FileAsset
that means that the child already has had to_solr called which included metadata from FileAsset if any of the above is false then call to_solr as normal
63 64 65 66 67 68 69 70 71 72 |
# File 'lib/hydra/models/file_asset.rb', line 63 def to_solr(solr_doc=Hash.new, opts={}) active_fedora_model_s = solr_doc["active_fedora_model_s"] if solr_doc["active_fedora_model_s"] actual_class = active_fedora_model_s.constantize if active_fedora_model_s if actual_class && actual_class != self.class && actual_class.superclass == ::FileAsset solr_doc else super(solr_doc,opts) end end |