Class: SelfData
- Inherits:
-
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 =
"0.1.1"
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.
23
24
25
|
# File 'lib/self_data.rb', line 23
def initialize(file = caller_file)
@file = file
end
|
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
21
22
23
|
# File 'lib/self_data.rb', line 21
def file
@file
end
|
Class Method Details
.add_converter(name, block) ⇒ Object
15
16
17
18
|
# File 'lib/self_data.rb', line 15
def add_converter(name, block)
self.converters ||= {}
self.converters[name] = block
end
|
.add_filter(&block) ⇒ Object
11
12
13
|
# File 'lib/self_data.rb', line 11
def add_filter(&block)
filters << block
end
|
Instance Method Details
#load(*formats, **options) ⇒ Object
27
28
29
30
31
32
33
34
35
36
37
38
39
|
# File 'lib/self_data.rb', line 27
def load(*formats, **options)
formats = default_formats if formats.empty?
options = default_options if options.empty?
formats.reduce(read) do |data, format|
fail ConverterNotFound, format unless converters[format]
begin
converters[format].call(data, options)
rescue => e
raise ConversionError.new(format, e)
end
end
end
|
#read ⇒ Object
41
42
43
|
# File 'lib/self_data.rb', line 41
def read
IO.read(file).scan(/\n__END__\n(.*)/m).flatten.first or fail NoDataFound, file
end
|