Class: Hocon::Impl::Parseable::ParseableResources

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

Overview

NOTE: this is not a faithful port of the ‘ParseableResources` class from the upstream, because at least for now we’re not going to try to do anything crazy like look for files on the ruby load path. However, there is a decent chunk of logic elsewhere in the codebase that is written with the assumption that this class will provide the ‘last resort’ attempt to find a config file before giving up, so we’re basically port just enough to have it provide that last resort behavior

Constant Summary

Constants inherited from Hocon::Impl::Parseable

MAX_INCLUDE_DEPTH

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Hocon::Impl::Parseable

#content_type, #custom_reader, #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_stack, #parse_value, #parse_value_from_origin, #post_construct, #raw_parse_document, #raw_parse_document_from_io, #raw_parse_value_from_io, relative_to, syntax_from_extension, trace

Methods included from ConfigParseable

#options, #origin, #parse

Constructor Details

#initialize(resource, options) ⇒ ParseableResources

Returns a new instance of ParseableResources.



481
482
483
484
485
# File 'lib/hocon/impl/parseable.rb', line 481

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

Class Method Details

.parent(resource) ⇒ Object



503
504
505
506
507
508
509
510
511
512
513
514
# File 'lib/hocon/impl/parseable.rb', line 503

def self.parent(resource)
  # the "resource" is not supposed to begin with a "/"
  # because it's supposed to be the raw resource
  # (ClassLoader#getResource), not the
  # resource "syntax" (Class#getResource)
  i = resource.rindex("/")
  if i < 0
    nil
  else
    resource.slice(0..i)
  end
end

Instance Method Details

#create_originObject



536
537
538
# File 'lib/hocon/impl/parseable.rb', line 536

def create_origin
  Hocon::Impl::SimpleConfigOrigin.new_resource(@resource)
end

#guess_syntaxObject



499
500
501
# File 'lib/hocon/impl/parseable.rb', line 499

def guess_syntax
  Hocon::Impl::Parseable.syntax_from_extension(@resource)
end

#raw_parse_value(origin, final_options) ⇒ Object

Raises:

  • (IOError)


491
492
493
494
495
496
497
# File 'lib/hocon/impl/parseable.rb', line 491

def raw_parse_value(origin, final_options)
  # this is where the upstream code would go out and look for a file on the
  # classpath.  We're not going to do that, and instead we're just going to
  # raise the same exception that the upstream code would raise if it failed
  # to find the file.
  raise IOError, "resource not found: #{@resource}"
end

#readerObject



487
488
489
# File 'lib/hocon/impl/parseable.rb', line 487

def reader
  raise Hocon::ConfigError::ConfigBugOrBrokenError, "reader() should not be called on resources"
end

#relative_to(sibling) ⇒ Object



516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/hocon/impl/parseable.rb', line 516

def relative_to(sibling)
  if sibling.start_with?("/")
    # if it starts with "/" then don't make it relative to the
    # including resource
    Hocon::Impl::Parseable.new_resources(sibling.slice(1), options.set_origin_description(nil))
  else
    # here we want to build a new resource name and let
    # the class loader have it, rather than getting the
    # url with getResource() and relativizing to that url.
    # This is needed in case the class loader is going to
    # search a classpath.
    parent = self.class.parent(@resource)
    if parent.nil?
      Hocon::Impl::Parseable.new_resources(sibling, options.set_origin_description(nil))
    else
      Hocon::Impl::Parseable.new_resources("#{parent}/sibling", options.set_origin_description(nil))
    end
  end
end

#to_sObject



540
541
542
# File 'lib/hocon/impl/parseable.rb', line 540

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