Class: Hocon::Impl::Parseable::ParseableFile

Inherits:
Hocon::Impl::Parseable show all
Defined in:
lib/hocon/impl/parseable.rb

Overview

NOTE: Skipping ‘ParseableURL` for now as we probably won’t support this right away

Constant Summary

Constants inherited from Hocon::Impl::Parseable

MAX_INCLUDE_DEPTH

Instance Method Summary collapse

Methods inherited from Hocon::Impl::Parseable

#content_type, #fixup_options, force_parsed_to_object, #include_context, new_file, new_not_found, new_resources, new_string, #options, #origin, #parse, #parse_config_document, #parse_document, #parse_document_from_origin, #parse_value, #parse_value_from_origin, #post_construct, #raw_parse_document, #raw_parse_document_from_io, #raw_parse_value, #raw_parse_value_from_io, #reader, relative_to, syntax_from_extension, trace

Methods included from ConfigParseable

#options, #origin, #parse

Constructor Details

#initialize(input, options) ⇒ ParseableFile

Returns a new instance of ParseableFile.



397
398
399
400
401
# File 'lib/hocon/impl/parseable.rb', line 397

def initialize(input, options)
  super()
  @input = input
  post_construct(options)
end

Instance Method Details

#create_originObject



453
454
455
# File 'lib/hocon/impl/parseable.rb', line 453

def create_origin
  Hocon::Impl::SimpleConfigOrigin.new_file(@input)
end

#custom_readerObject



403
404
405
406
407
408
409
410
# File 'lib/hocon/impl/parseable.rb', line 403

def custom_reader
  if Hocon::Impl::ConfigImpl.trace_loads_enabled
    self.class.trace("Loading config from a String: #{@input}")
  end
  # we return self here, which will cause `open` to be called on us, so
  # we can provide an implementation of that.
  self
end

#guess_syntaxObject



430
431
432
# File 'lib/hocon/impl/parseable.rb', line 430

def guess_syntax
  Hocon::Impl::Parseable.syntax_from_extension(File.basename(@input))
end

#openObject



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/hocon/impl/parseable.rb', line 412

def open
  begin
    if block_given?
      File.open(@input, :encoding => 'bom|utf-8') do |f|
        yield f
      end
    else
      File.open(@input, :encoding => 'bom|utf-8')
    end
  rescue Errno::ENOENT
    if @initial_options.allow_missing?
      return Hocon::Impl::SimpleConfigObject.empty
    end

    raise Hocon::ConfigError::ConfigIOError.new(nil, "File not found. No file called #{@input}")
  end
end

#relative_to(filename) ⇒ Object



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
# File 'lib/hocon/impl/parseable.rb', line 434

def relative_to(filename)
  sibling = nil
  if Pathname.new(filename).absolute?
    sibling = File.new(filename)
  else
    # this may return nil
    sibling = Hocon::Impl::Parseable.relative_to(@input, filename)
  end
  if sibling.nil?
    nil
  elsif File.exists?(sibling)
    self.class.trace("#{sibling} exists, so loading it as a file")
    Hocon::Impl::Parseable.new_file(sibling, options.set_origin_description(nil))
  else
    self.class.trace("#{sibling} does not exist, so trying it as a resource")
    super(filename)
  end
end

#to_sObject



457
458
459
# File 'lib/hocon/impl/parseable.rb', line 457

def to_s
  "#{self.class.name.split('::').last} (#{@input})"
end