Class: SelfData

Inherits:
Object
  • Object
show all
Defined in:
lib/self_data.rb,
lib/self_data/errors.rb,
lib/self_data/version.rb

Defined Under Namespace

Classes: ConversionError, ConverterNotFound, NoDataFound

Constant Summary collapse

Error =
Class.new(StandardError)
VERSION =
"1.1.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file = caller_file) ⇒ SelfData

Returns a new instance of SelfData.



26
27
28
# File 'lib/self_data.rb', line 26

def initialize(file = caller_file)
  @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



24
25
26
# File 'lib/self_data.rb', line 24

def file
  @file
end

Class Method Details

.add_converter(name, block) ⇒ Object



18
19
20
21
# File 'lib/self_data.rb', line 18

def add_converter(name, block)
  self.converters ||= {}
  self.converters[name] = block
end

.add_filter(&block) ⇒ Object



14
15
16
# File 'lib/self_data.rb', line 14

def add_filter(&block)
  filters << block
end

Instance Method Details

#load(*formats, **options) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/self_data.rb', line 30

def load(*formats, **options)
  formats = default_formats if formats.empty?
  options = default_options if options.empty?

  formats.reduce(read) do |data, format|
    raise ConverterNotFound, format unless converters[format]
    begin
      converters[format].call(data, options)
    rescue => e
      raise ConversionError.new(format, e)
    end
  end
end

#readObject



44
45
46
# File 'lib/self_data.rb', line 44

def read
  IO.read(file).scan(/\n__END__\n(.*)/m).flatten.first or raise NoDataFound, file
end