Class: ToFactory::Parsing::File

Inherits:
Object
  • Object
show all
Defined in:
lib/to_factory/parsing/file.rb

Constant Summary collapse

EmptyFileException =
Class.new ArgumentError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents) ⇒ File

Returns a new instance of File.



25
26
27
# File 'lib/to_factory/parsing/file.rb', line 25

def initialize(contents)
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



12
13
14
# File 'lib/to_factory/parsing/file.rb', line 12

def contents
  @contents
end

Class Method Details

.from_file(filename) ⇒ Object



18
19
20
21
22
23
# File 'lib/to_factory/parsing/file.rb', line 18

def self.from_file(filename)
  contents = ::File.read filename rescue nil
  raise EmptyFileException.new "Invalid file #{filename}"  if contents.to_s.strip.length == 0

  new(contents)
end

.parse(filename) ⇒ Object



14
15
16
# File 'lib/to_factory/parsing/file.rb', line 14

def self.parse(filename)
  from_file(filename).parse
end

Instance Method Details

#parserObject



29
30
31
# File 'lib/to_factory/parsing/file.rb', line 29

def parser
  @parser ||= Syntax.new(@contents)
end