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.



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

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

Instance Method Details

#create_originObject



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

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

#custom_readerObject



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

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



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

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

#openObject



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

def open
  begin
    if block_given?
      File.open(@input) do |f|
        yield f
      end
    else
      File.open(@input)
    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



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

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



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

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