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.2.1"

Class Attribute Summary collapse

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.



34
35
36
# File 'lib/self_data.rb', line 34

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

Class Attribute Details

.default_formatsObject

Returns the value of attribute default_formats.



5
6
7
# File 'lib/self_data.rb', line 5

def default_formats
  @default_formats
end

.default_optionsObject

Returns the value of attribute default_options.



5
6
7
# File 'lib/self_data.rb', line 5

def default_options
  @default_options
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



32
33
34
# File 'lib/self_data.rb', line 32

def file
  @file
end

Class Method Details

.add_converter(name, block) ⇒ Object



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

def add_converter(name, block)
  converters[name] = block
end

.add_filter(&block) ⇒ Object



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

def add_filter(&block)
  filters << block
end

.convertersObject



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

def converters
  @converters ||= {}
end

.filtersObject



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

def filters
  @filters ||= []
end

.load(*args, **kargs) ⇒ Object



11
12
13
# File 'lib/self_data.rb', line 11

def load(*args, **kargs)
  new.load(*args, **kargs)
end

.read(*args, **kargs) ⇒ Object



7
8
9
# File 'lib/self_data.rb', line 7

def read(*args, **kargs)
  new.read(*args, **kargs)
end

Instance Method Details

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



38
39
40
41
42
43
44
45
46
47
48
49
50
# 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

#readObject



52
53
54
# File 'lib/self_data.rb', line 52

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