Class: DTK::DSL::FileType

Inherits:
Object
  • Object
show all
Defined in:
lib/dsl/file_type.rb,
lib/dsl/file_type/match.rb,
lib/dsl/file_type/subclasses.rb,
lib/dsl/file_type/matching_files.rb

Direct Known Subclasses

CommonModule, ServiceInstance

Defined Under Namespace

Classes: CommonModule, Match, MatchingFiles, ServiceInstance

Constant Summary collapse

SERVICE_INSTANCE_NESTED_MODULE_DIR =
'modules'
TYPES =
{
  CommonModule::DSLFile::Top => {
    :regexp                => Regexp.new("dtk\.module\.(yml|yaml)"),
    :canonical_path_lambda => lambda { |_params| 'dtk.module.yaml' }, 
    :print_name            => 'module DSL file'
  },
  ServiceInstance::DSLFile::Top => {
    :regexp                => Regexp.new("dtk\.service\.(yml|yaml)"),
    :canonical_path_lambda => lambda { |_params| 'dtk.service.yaml' }, 
    :print_name            => 'service DSL file'
  },
  ServiceInstance::NestedModule => {
    :instance_match_lambda => lambda { |path| path =~ Regexp.new("^#{SERVICE_INSTANCE_NESTED_MODULE_DIR}/([^/]+)/.+$") && { :module_name => $1 } },    
    :base_dir_lambda       => lambda { |params| "#{SERVICE_INSTANCE_NESTED_MODULE_DIR}/#{params[:module_name]}" },
    :print_name            => 'nested module file'
  },
  ServiceInstance::NestedModule::DSLFile::Top => {
    :regexp                => Regexp.new("#{SERVICE_INSTANCE_NESTED_MODULE_DIR}/[^/]+/dtk\.nested_module\.(yml|yaml)"),
    :canonical_path_lambda => lambda { |params| "#{SERVICE_INSTANCE_NESTED_MODULE_DIR}/#{params[:module_name]}/dtk.nested_module.yaml" },
    :print_name            => 'nested module DSL file'
  }
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.backup_pathObject



80
81
82
# File 'lib/dsl/file_type.rb', line 80

def self.backup_path
  backup_path_from_canonical_path(canonical_path)
end

.base_dirObject



87
88
89
# File 'lib/dsl/file_type.rb', line 87

def self.base_dir
  nil
end

.canonical_pathObject



72
73
74
# File 'lib/dsl/file_type.rb', line 72

def self.canonical_path
  canonical_path_lambda.call({})
end

.file_type_instance_if_match?(file_path) ⇒ Boolean

If match it retuns a hash with params that can be used to create a File Type instance

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
# File 'lib/dsl/file_type.rb', line 97

def self.file_type_instance_if_match?(file_path)
  # just want to match 'this' and dont want to match parent so not using matching_type_def
  if instance_match_lambda = TYPES[self][:instance_match_lambda]
    if hash_params_for_new = instance_match_lambda.call(file_path)
      new(hash_params_for_new)
    end
  end
end

.matches?(file_path, opts = {}) ⇒ Boolean

opts can have keys:

:exact - Booelan (default: false) - meaning regexp completely matches file_path

Returns:

  • (Boolean)


27
28
29
# File 'lib/dsl/file_type.rb', line 27

def self.matches?(file_path, opts = {})
  Match.matches?(self, file_path, opts)
end

.matching_files_array?(file_type_classes, file_paths) ⇒ Boolean

Returns array of MatchingFiles or nil

Returns:

  • (Boolean)


35
36
37
# File 'lib/dsl/file_type.rb', line 35

def self.matching_files_array?(file_type_classes, file_paths)
  MatchingFiles.matching_files_array?(file_type_classes, file_paths)
end

regexps, except for one in :instance_match_lambda, purposely do not have ^ or $ so calling function can insert these depending on context



64
65
66
# File 'lib/dsl/file_type.rb', line 64

def self.print_name
  matching_type_def(:print_name)
end

.regexpObject



68
69
70
# File 'lib/dsl/file_type.rb', line 68

def self.regexp
  matching_type_def(:regexp)
end

.type_level_typeObject



106
107
108
# File 'lib/dsl/file_type.rb', line 106

def self.type_level_type
  raise Error::NoMethodForConcreteClass.new(self)
end

Instance Method Details

#backup_pathObject



83
84
85
# File 'lib/dsl/file_type.rb', line 83

def backup_path
  self.class.backup_path_from_canonical_path(canonical_path)
end

#base_dirObject

This can be over-written



91
92
93
# File 'lib/dsl/file_type.rb', line 91

def base_dir
  self.class.base_dir
end

#canonical_pathObject

This can be over-written



76
77
78
# File 'lib/dsl/file_type.rb', line 76

def canonical_path
  self.class.canonical_path
end

#indexObject

This can be over-written



111
112
113
# File 'lib/dsl/file_type.rb', line 111

def index
  self.class.to_s
end

#matches?(file_path, opts = {}) ⇒ Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/dsl/file_type.rb', line 30

def matches?(file_path, opts = {})
  Match.matches?(self, file_path, opts)
end