class SelfData

Constants

Error
VERSION

Attributes

default_formats[RW]
default_options[RW]
file[R]

Public Class Methods

add_converter(name, block) click to toggle source
# File lib/self_data.rb, line 27
def add_converter(name, block)
  converters[name] = block
end
add_filter(&block) click to toggle source
# File lib/self_data.rb, line 19
def add_filter(&block)
  filters << block
end
converters() click to toggle source
# File lib/self_data.rb, line 23
def converters
  @converters ||= {}
end
filters() click to toggle source
# File lib/self_data.rb, line 15
def filters
  @filters ||= []
end
load(*args, **kargs) click to toggle source
# File lib/self_data.rb, line 11
def load(*args, **kargs)
  new.load(*args, **kargs)
end
new(file = caller_file) click to toggle source
# File lib/self_data.rb, line 34
def initialize(file = caller_file)
  @file = file
end
read(*args, **kargs) click to toggle source
# File lib/self_data.rb, line 7
def read(*args, **kargs)
  new.read(*args, **kargs)
end

Public Instance Methods

load(*formats, **options) click to toggle source
# File lib/self_data.rb, line 38
def load(*formats, **options)
  formats = self.class.default_formats if formats.empty?
  options = self.class.default_options if options.empty?

  formats.reduce(read) do |data, format|
    raise ConverterNotFound, format unless self.class.converters[format]
    begin
      self.class.converters[format].call(data, options)
    rescue => error
      raise ConversionError.new(format, error)
    end
  end
end
read() click to toggle source
# File lib/self_data.rb, line 52
def read
  IO.read(file).scan(/\n__END__\n(.*)/m).flatten.first or raise NoDataFound, file
end

Private Instance Methods

caller_file() click to toggle source
# File lib/self_data.rb, line 58
def caller_file
  calls = caller.lazy
    .map { |call_string| call_string.split(":").first }
    .reject { |file| file == __FILE__ }
    .select(&File.method(:exist?))

  self.class.filters.each do |filter|
    calls = calls.select(&filter.method(:call))
  end

  calls.first
end