Class: Sbuilder::SnippetLoaderSimple

Inherits:
SnippetLoaderPlugin show all
Extended by:
Forwardable, SingleForwardable
Defined in:
lib/sbuilder/snippet_loader_simple.rb

Constant Summary collapse

@@src_dir =

default directory where files are read

"./src"

Constants inherited from LoaderPluginRoot

LoaderPluginRoot::PROGNAME

Constants included from Utils::MyLogger

Utils::MyLogger::LOGFILE

Constructor and configuration collapse

Register snippets to sbuilder collapse

Simple plugin implementation collapse

Methods included from SnippetLoaderPluginMixer

#registerMetatype, #registerSnippets, #setSnippetFacade, #snippetFacade, validateProperties

Methods inherited from LoaderPluginRoot

#configure, #logger, #oneOf, #validateProperties, validateProperties

Methods included from Utils::MyLogger

#getLogger, #logfile

Constructor Details

#initialize(options = {}) ⇒ SnippetLoaderSimple




22
23
24
# File 'lib/sbuilder/snippet_loader_simple.rb', line 22

def initialize( options = {} )
  super( options )
end

Class Method Details

.configure(configuration) ⇒ Object

Class method to configure class ‘SnippetLoaderPlugin’

Parameters:

  • configuration (Hash)

    properties to configure



30
31
32
33
34
35
36
37
# File 'lib/sbuilder/snippet_loader_simple.rb', line 30

def self.configure( configuration )
  return if configuration.nil?
  Sbuilder::Utils::Validate.validateProperties(
    configuration,
    Constants::VALIDATION[:snippet_loader_plugin][:class_config_required],
    Constants::VALIDATION[:snippet_loader_plugin][:class_config_allowed] )
  @@src_dir = configuration['src_dir']
end

Instance Method Details

#doConfigure(configuration) ⇒ Object

base class should implement ‘doConfigure’



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/sbuilder/snippet_loader_simple.rb', line 41

def doConfigure( configuration )

  if configuration.nil?
    logger.warn "#{__method__}: no cofiguration for #{self.class}"
    return
  end
  Sbuilder::Utils::Validate.validateProperties(
    configuration,
    Constants::VALIDATION[:snippet_loader_plugin][:object_config_required],
    Constants::VALIDATION[:snippet_loader_plugin][:object_config_allowed] )

  # hash with metatypes as keys
  configuration['metatypes'] && configuration['metatypes'].each do |metatype,desc|
    registerMetatype( metatype, desc )
  end

end

#doRegisterSnippets(snippetDefitions) ⇒ Object

Override abstract method in parent class for the implementation.

Parameters:

  • snippetDefitions (Hash:Array)

    given in sbuilder.yaml



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/sbuilder/snippet_loader_simple.rb', line 69

def doRegisterSnippets( snippetDefitions )
  logger.info "#{__method__}, loading snippetDefitions=#{snippetDefitions}"
  
  snippetDefitions && snippetDefitions.each do  |snippetDefition|

    # gets actually delegated
    # Sbuilder::Utils::Validate.validateProperties( snippetDefition, PROPS_REQUIRED, PROPS_ALLOWED )
    Utils::Validate.validateProperties( snippetDefition, Constants::VALIDATION[:snippet_loader_plugin][:required], Constants::VALIDATION[:snippet_loader_plugin][:allowed] )
    # 0 allowed, max one allowed from url/file
    Utils::Validate.oneOf( snippetDefition, Constants::VALIDATION[:snippet_loader_plugin][:one_of_file_or_url], 0, 1 )

    # read content of snippetDefition['file'] into a [String}
    snippetBody = readFile( src_path( snippetDefition ) ) if snippetDefition['file'] || snippetDefition['url']

    # pass 'snippetBody' together with metatypes to facade
    snippetFacade.handOver( snippetDefition['metatype'], snippetDefition['appName'], snippetBody, snippetDefition['name'], )
    
  end
end

#readFile(location) ⇒ String

Read file pointed by ‘location’

Parameters:

  • location (String)

    to read

Returns:

  • (String)

    lines in file in ‘location’



99
100
101
102
# File 'lib/sbuilder/snippet_loader_simple.rb', line 99

def readFile( location )
  @logger.info "#{__method__}: location=#{location}"
  line = Sbuilder::Utils::NetIo.read_lines( location )
end

#src_dirString

Returns path to directory, where src files are.

Returns:

  • (String)

    path to directory, where src files are



115
116
117
# File 'lib/sbuilder/snippet_loader_simple.rb', line 115

def src_dir
  @@src_dir || "."
end

#src_path(snippetDefition) ⇒ String

Returns path to file to read ‘url’ or src_dir + file.

Parameters:

  • snippetDefition (Hash)

    configuration for location

Options Hash (snippetDefition):

  • url (String)

    read from url (=e.g. path)

  • file (String)

    read from file in src -repository

Returns:

  • (String)

    path to file to read ‘url’ or src_dir + file



109
110
111
112
# File 'lib/sbuilder/snippet_loader_simple.rb', line 109

def src_path( snippetDefition )
  return snippetDefition['url'] if snippetDefition['url']
  return File.join( src_dir, snippetDefition['file'])
end