Class: MasterView::DirectiveLoadPath::PathEntry

Inherits:
Object
  • Object
show all
Defined in:
lib/masterview/directive_load_path.rb

Overview

A PathEntry contains the specification for a directory on the MasterView directives load path.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dir_path, options = nil) ⇒ PathEntry

Define an entry for a directive path with the path name of a directory containing directive implementation classes to be loaded into the MasterView processing configuration.

Optionally specify options for the directives loaded from this directory:

:default - metadata defaults

Metadata defaults extend or override any defaults specified in the dir_path/.metadata file, if defined, allowing application customization of the default defaults.



110
111
112
113
# File 'lib/masterview/directive_load_path.rb', line 110

def initialize( dir_path, options=nil )
  @dir_path = dir_path
  @options = options.nil? ? {} : self.class.copy_options(options)
end

Instance Attribute Details

#dir_pathObject (readonly)

The path name of a directory containing directive implementations

The pathname is normalized into an absolute path by the validation checking.



93
94
95
# File 'lib/masterview/directive_load_path.rb', line 93

def dir_path
  @dir_path
end

#optionsObject (readonly)

Returns the value of attribute options.



95
96
97
# File 'lib/masterview/directive_load_path.rb', line 95

def options
  @options
end

Class Method Details

.copy_options(options) ⇒ Object

Answer a deep copy so we have a proper clone of the options.

Raises:

  • (ArgumentError)


84
85
86
87
# File 'lib/masterview/directive_load_path.rb', line 84

def self.copy_options(options)
  raise ArgumentError, "PathEntry options must be a hash: #{options.inspect}" if ! options.is_a?(Hash)
  Marshal.load(Marshal.dump(options))
end

Instance Method Details

#deep_copyObject

Answer a deep copy so we have a proper clone of the options.



122
123
124
# File 'lib/masterview/directive_load_path.rb', line 122

def deep_copy()
  Marshal.load(Marshal.dump(self))
end

#exists?Boolean

Answer whether the directory exists

Returns:

  • (Boolean)


127
128
129
# File 'lib/masterview/directive_load_path.rb', line 127

def exists?
  File.directory?(dir_path)
end

#inspectObject

:nodoc:



115
116
117
118
119
# File 'lib/masterview/directive_load_path.rb', line 115

def inspect #:nodoc:
  abbrev_class_name = self.class.name.split('::')
  abbrev_class_name = abbrev_class_name[1..abbrev_class_name.size].join('::')
  "#{abbrev_class_name}('#{dir_path}', options=#{options.inspect})"
end

#load_metadata_specs(options = {}) ⇒ Object

Load the metadata specifications for a directives directory



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'lib/masterview/directive_load_path.rb', line 184

def (options={})

  create_if_not_defined = options.fetch(:create_if_not_defined, true)

  md_file_path = "#{dir_path}/#{METADATA_SPECS_FILE_NAME}"
  dir_has_md_specs = File.file?( md_file_path )
  if dir_has_md_specs
    STDOUT.puts "\n###Loading directive metadata specs: #{md_file_path}" if DEBUG_MD_DIR_SPECS
    md_config_specs = DirectiveLoadPath.(md_file_path)
    md_config_specs[:default] ||= {}  # ensure that there are default specs
    STDOUT.puts "...md_config_specs=#{md_config_specs.inspect}" if DEBUG_MD_DIR_SPECS
  elsif create_if_not_defined
    md_config_specs = { :default => {} }
  else
    return nil
  end

  md_defaults = md_config_specs[:default]
  if ! md_defaults.has_key?( :description )
    md_defaults[:description] = "MasterView directive from #{dir_path}"
  end
  # raises ArgumentError if props are bad; has side effect of normalizing the prop values
  DirectiveMetadata. md_defaults

  md_config_specs

end

#metadata_defaultsObject

The metadata defaults for this directory, if any

Metadata defaults specified on a load path entry supplement any .metadata options specified in the directory itself by overridding and extending any options defined statically in the directives directory.



138
139
140
# File 'lib/masterview/directive_load_path.rb', line 138

def 
  options.fetch(:default, {})
end

#normalizeObject

ensure normalized representation with absolute pathnames and standard options



175
176
177
178
179
180
181
# File 'lib/masterview/directive_load_path.rb', line 175

def normalize #:nodoc:
  @dir_path = File.expand_path( @dir_path )
  #assert ! (['/', '\\'].contains?dir_path[-1..-1])
  #tbd: (recursively) ensure keys are symbolized?
  @options[:default] = {} if ! @options.has_key?(:default)
  #md_defaults entries were already normalized by side effects of the validation checker
end

#use_extensions_namespaceObject

Answer whether directives loaded from this directory use the masterview extensions directive namespace by default.



154
155
156
# File 'lib/masterview/directive_load_path.rb', line 154

def use_extensions_namespace #:nodoc:
  ! use_masterview_namespace
end

#use_masterview_namespaceObject

Answer whether directives loaded from this directory use the masterview namespace by default.

This option is ordinarily specified only for builtin masterview directives.



147
148
149
# File 'lib/masterview/directive_load_path.rb', line 147

def use_masterview_namespace #:nodoc:
  options.fetch(:use_masterview_namespace, false)
end

#validateObject

Validate the directory path entry specification.

Ensures that the directory exists and that the entry specification is normalized to an absolute pathname.



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/masterview/directive_load_path.rb', line 162

def validate
  if ! File.directory?(dir_path)
      err_msg = "Invalid directive load path directory: '#{dir_path}'"
      raise InvalidDirectivePathError.new(self, err_msg)
  end
  if @options.has_key?(:default)
    md_defaults = @options[:default]
    # raises ArgumentError if props are bad; has side effect of normalizing the prop values
    DirectiveMetadata. md_defaults
  end
end