module DTK::DSL::Template::Parsing::Mixin

Attributes

file_obj[R]
parent_key[R]

Public Instance Methods

parse() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 47
def parse
  parse!
  @output
end
parser_output_type() click to toggle source

The method parser_output_type can be set on concrete class; it wil be set if input and output types are different

# File lib/dsl/template/parsing/mixin.rb, line 53
def parser_output_type
  nil
end
parsing_error(*args) click to toggle source

args can have form

(:ParsingErrorName,*parsing_error_params) or
(*parsing_error_params)
# File lib/dsl/template/parsing/mixin.rb, line 60
def parsing_error(*args)
  parsing_error_with_opts(args)
end

Private Instance Methods

add_found_key?(key) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 83
def add_found_key?(key)
  @found_keys << key if @found_keys
end
check_type_and_ret_input(template_class, key_and_value) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 162
def check_type_and_ret_input(template_class, key_and_value)
  input = key_and_value.values.first
  key = key_and_value.keys.first

  case template_class.elements_collection_type
  when :hash
    raise_input_error(::Hash, :input => input, :key => key) unless input_hash?(input)
  when :array
    raise_input_error(::Array, :input => input, :key => key) unless input_array?(input)
  end
  input
end
empty_parser_output(input, parent_key) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 66
def empty_parser_output(input, parent_key)
  if self.class.const_defined? 'SemanticParse'
    qualified_key = parent_key && parent_key.create_qualified_key
    self.class::SemanticParse.new(FileParser::Output, :qualified_key => qualified_key)
  elsif parser_output_type 
    FileParser::Output.create(:output_type => parser_output_type)
  else
    FileParser::Output.create(:input => input)
  end
end
file_parser_output_array() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 87
def file_parser_output_array
  self.class.file_parser_output_array
end
input_array() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 187
def input_array
  @input_array ||= input_array? ? @input : raise_input_error(::Array)
end
input_array?(input = nil) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 183
def input_array?(input = nil)
  (input || @input).kind_of?(FileParser::Input::Array)
end
input_hash() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 179
def input_hash
  @input_hash ||= input_hash? ? @input : raise_input_error(::Hash)
end
input_hash?(input = nil) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 175
def input_hash?(input = nil)
  (input || @input).kind_of?(FileParser::Input::Hash) 
end
input_key_and_value?(constant, opts = {}) click to toggle source

returns nil or {key => value} opts can have keys

:input_hash
# File lib/dsl/template/parsing/mixin.rb, line 140
def input_key_and_value?(constant, opts = {})
  input_hash = opts[:input_hash] || input_hash()
  if kv = constant_class.matching_key_and_value?(input_hash, constant)
    unless kv.values.first.nil? # check if value is nil and if so nil is returned
      add_found_key?(kv.keys.first)
      kv
    end
  end
end
input_key_value(constant, opts = {}) click to toggle source

This cannot be used for an assignment that can have with nil values opts can have key

:input_hash
# File lib/dsl/template/parsing/mixin.rb, line 122
def input_key_value(constant, opts = {})
  ret = input_key_value?(constant, opts)
  raise_missing_key_value(constant) if ret.nil?
  ret
end
input_key_value?(constant, opts = {}) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 127
def input_key_value?(constant, opts = {})
  input_hash = opts[:input_hash] || input_hash()
  set_matching_key = []
  ret = constant_class.matches?(input_hash, constant, :set_matching_key => set_matching_key)
  if matching_key = set_matching_key.first
    add_found_key?(matching_key)
  end
  ret
end
input_string() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 195
def input_string
  @input_string ||= input_string? ? @input : raise_input_error(::String)
end
input_string?() click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 191
def input_string?
  @input.kind_of?(::String)
end
parse!() click to toggle source

Main template-specific parse call; Concrete classes overwrite this

# File lib/dsl/template/parsing/mixin.rb, line 23
def parse!
  raise Error::NoMethodForConcreteClass.new(self.class)
end
parse_child(parse_template_type, input, opts = {}) click to toggle source

opts can have keys

:parent_key
# File lib/dsl/template/parsing/mixin.rb, line 111
def parse_child(parse_template_type, input, opts = {})
  if input.nil?
    nil
  else
    template_class(parse_template_type).create_for_parsing(input, opts.merge(:file_obj => @file_obj)).parse
  end
end
parse_child_elements(parse_template_type, key_constant, opts = {}) click to toggle source

opts can have key

:key_type
# File lib/dsl/template/parsing/mixin.rb, line 93
def parse_child_elements(parse_template_type, key_constant, opts = {})
  unless key_and_value = input_key_and_value?(key_constant, opts)
    nil
  else
    template_class = template_class(parse_template_type)
    input = check_type_and_ret_input(template_class, key_and_value)
    key_type = opts[:key_type] || parse_template_type
    template_class.parse_elements(input, ParentInfo.new(self, key_type))
  end
end
parse_child_elements?(parse_template_type, key_constant, opts = {}) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 104
def parse_child_elements?(parse_template_type, key_constant, opts = {})
  ret = parse_child_elements(parse_template_type, key_constant, opts)
  (ret.nil? or ret.empty?) ? nil : ret
end
parsing_add(array_element) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 158
def parsing_add(array_element)
  @output << array_element
end
parsing_error_with_opts(args, opts = {}) click to toggle source

opts can have keys

:key
# File lib/dsl/template/parsing/mixin.rb, line 215
def parsing_error_with_opts(args, opts = {})
  qualified_key = qualified_key(opts[:key]) 
  if error_class = ParsingError.error_class?(args)
    error_params = args[1..args.size-1]
    error_class.new(*error_params, :file_obj => @file_obj, :qualified_key => qualified_key)
  else
    ParsingError.new(*args, :file_obj => @file_obj, :qualified_key => qualified_key)
  end
end
parsing_initialize(opts = {}) click to toggle source

opts can have keys:

:raw_input
:file_obj
:parent_key
# File lib/dsl/template/parsing/mixin.rb, line 34
def parsing_initialize(opts = {})
  unless raw_input = opts[:raw_input]
    raise Error, "Unexpected that opts[:raw_input] is nil"
  end
  parent_key = opts[:parent_key]

  @input      = FileParser::Input.create(raw_input)
  @output     = empty_parser_output(@input, parent_key)
  @file_obj   = opts[:file_obj]
  @parent_key = parent_key
end
parsing_merge(hash) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 154
def parsing_merge(hash)
  @output.merge!(hash)
end
parsing_set(constant, val) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 150
def parsing_set(constant, val)
  @output.set(constant, val)
end
qualified_key(key = nil) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 225
def qualified_key(key = nil)
  if key.nil?
    @parent_key
  else
    # TODO: find common function that appends keys
    @parent_key.nil? ? key : "#{@parent_key}/#{key}"
  end
end
raise_input_error(correct_ruby_types, opts = {}) click to toggle source

opts can have keys

:input
:key

correct_ruby_types can also be scalar

# File lib/dsl/template/parsing/mixin.rb, line 203
def raise_input_error(correct_ruby_types, opts = {})
  input = (opts.has_key?(:input) ? opts[:input] : @input)
  raise parsing_error_with_opts([:WrongObjectType, input, correct_ruby_types], opts)
end
raise_missing_key_value(constant) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 208
def raise_missing_key_value(constant)
  key = canonical_key(constant)
  raise parsing_error(:MissingKeyValue, key)
end
remove_processed_keys_from_input_hash!(&body) click to toggle source
# File lib/dsl/template/parsing/mixin.rb, line 77
def remove_processed_keys_from_input_hash!(&body)
  @found_keys = []
  body.call
  @found_keys.each { |key| input_hash.delete(key) }
end