Class: MIME::Types::Loader
- Inherits:
-
Object
- Object
- MIME::Types::Loader
- Defined in:
- lib/mime/types/loader.rb
Overview
This class is responsible for initializing the MIME::Types registry from the data files supplied with the mime-types library.
The Loader will use one of the following paths:
-
The
path
provided in its constructor argument; -
The value of ENV; or
-
The value of MIME::Types::Data::PATH.
When #load is called, the path
will be searched recursively for all YAML (.yml or .yaml) files. By convention, there is one file for each media type (application.yml, audio.yml, etc.), but this is not required.
Instance Attribute Summary collapse
-
#container ⇒ Object
readonly
The MIME::Types container instance that will be loaded.
-
#path ⇒ Object
readonly
The path that will be read for the MIME::Types files.
Class Method Summary collapse
-
.load(options = { columnar: false }) ⇒ Object
Loads the default MIME::Type registry.
-
.load_from_json(filename) ⇒ Object
Loads MIME::Types from a single JSON file.
-
.load_from_yaml(filename) ⇒ Object
Loads MIME::Types from a single YAML file.
Instance Method Summary collapse
-
#initialize(path = nil, container = nil) ⇒ Loader
constructor
Creates a Loader object that can be used to load MIME::Types registries into memory, using YAML, JSON, or Columnar registry format loaders.
-
#load(options = { columnar: false }) ⇒ Object
Loads a MIME::Types registry.
-
#load_columnar ⇒ Object
Loads a MIME::Types registry from columnar files recursively found in
path
. -
#load_json ⇒ Object
Loads a MIME::Types registry from JSON files (
*.json
) recursively found inpath
. -
#load_yaml ⇒ Object
Loads a MIME::Types registry from YAML files (
*.yml
or*.yaml
) recursively found inpath
.
Constructor Details
#initialize(path = nil, container = nil) ⇒ Loader
Creates a Loader object that can be used to load MIME::Types registries into memory, using YAML, JSON, or Columnar registry format loaders.
30 31 32 33 34 35 36 37 38 |
# File 'lib/mime/types/loader.rb', line 30 def initialize(path = nil, container = nil) path = path || ENV['RUBY_MIME_TYPES_DATA'] || MIME::Types::Data::PATH @container = container || MIME::Types.new @path = File.(path) # begin # require 'mime/lazy_types' # @container.extend(MIME::LazyTypes) # end end |
Instance Attribute Details
#container ⇒ Object (readonly)
The MIME::Types container instance that will be loaded. If not provided at initialization, a new MIME::Types instance will be constructed.
26 27 28 |
# File 'lib/mime/types/loader.rb', line 26 def container @container end |
#path ⇒ Object (readonly)
The path that will be read for the MIME::Types files.
23 24 25 |
# File 'lib/mime/types/loader.rb', line 23 def path @path end |
Class Method Details
.load(options = { columnar: false }) ⇒ Object
Loads the default MIME::Type registry.
95 96 97 |
# File 'lib/mime/types/loader.rb', line 95 def load( = { columnar: false }) new.load() end |
.load_from_json(filename) ⇒ Object
Loads MIME::Types from a single JSON file.
It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.
123 124 125 126 |
# File 'lib/mime/types/loader.rb', line 123 def load_from_json(filename) require 'json' JSON.parse(read_file(filename)).map { |type| MIME::Type.new(type) } end |
.load_from_yaml(filename) ⇒ Object
Loads MIME::Types from a single YAML file.
It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type
.
Note that the YAML format is about 2½ times slower than the JSON format.
NOTE: The purpose of this format is purely for maintenance reasons.
108 109 110 111 112 113 114 115 116 |
# File 'lib/mime/types/loader.rb', line 108 def load_from_yaml(filename) begin require 'psych' rescue LoadError nil end require 'yaml' YAML.load(read_file(filename)) end |
Instance Method Details
#load(options = { columnar: false }) ⇒ Object
Loads a MIME::Types registry. Loads from JSON files by default (#load_json).
This will load from columnar files (#load_columnar) if columnar: true
is provided in options
and there are columnar files in path
.
85 86 87 88 89 90 91 |
# File 'lib/mime/types/loader.rb', line 85 def load( = { columnar: false }) if [:columnar] && !Dir[columnar_path].empty? load_columnar else load_json end end |
#load_columnar ⇒ Object
Loads a MIME::Types registry from columnar files recursively found in path
.
72 73 74 75 76 77 78 |
# File 'lib/mime/types/loader.rb', line 72 def load_columnar require 'mime/types/columnar' unless defined?(MIME::Types::Columnar) container.extend(MIME::Types::Columnar) container.load_base_data(path) container end |
#load_json ⇒ Object
Loads a MIME::Types registry from JSON files (*.json
) recursively found in path
.
It is expected that the JSON objects will be an array of hash objects. The JSON format is the registry format for the MIME types registry shipped with the mime-types library.
62 63 64 65 66 67 68 |
# File 'lib/mime/types/loader.rb', line 62 def load_json Dir[json_path].sort.each do |f| types = self.class.load_from_json(f) container.add(*types, :silent) end container end |
#load_yaml ⇒ Object
Loads a MIME::Types registry from YAML files (*.yml
or *.yaml
) recursively found in path
.
It is expected that the YAML objects contained within the registry array will be tagged as !ruby/object:MIME::Type
.
Note that the YAML format is about 2½ times slower than the JSON format.
NOTE: The purpose of this format is purely for maintenance reasons.
49 50 51 52 53 54 |
# File 'lib/mime/types/loader.rb', line 49 def load_yaml Dir[yaml_path].sort.each do |f| container.add(*self.class.load_from_yaml(f), :silent) end container end |