Class: Litter::Parsing::LitterFile

Inherits:
Object
  • Object
show all
Defined in:
lib/litter/parsing/litter_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, file: nil, config: {}) ⇒ LitterFile

Returns a new instance of LitterFile.

Parameters:

  • path (String) (defaults to: nil)

    the path of the file to be parsed; if nil, uses file instead.

  • path (File) (defaults to: nil)

    the file to be parsed.

  • config (Hash) (defaults to: {})

    custom config that overrides defaults in config.rb.



12
13
14
15
16
17
# File 'lib/litter/parsing/litter_file.rb', line 12

def initialize(path = nil, file: nil, config: {})
  validate_path_or_file(path, file)

  @string = path ? File.read(path) : file.read
  @config = Config.new(config || {}).hash
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/litter/parsing/litter_file.rb', line 7

def config
  @config
end

#stringObject (readonly)

Returns the value of attribute string.



7
8
9
# File 'lib/litter/parsing/litter_file.rb', line 7

def string
  @string
end

Instance Method Details

#parseHash

Parses the file.

Returns:

  • (Hash)

    a hash of items and their finds; see parse_test.rb for an example.



21
22
23
24
25
26
# File 'lib/litter/parsing/litter_file.rb', line 21

def parse
  parsed = Parser.new(config[:parser]).parse(string)
  hashes = Transform.new.apply(parsed)

  hashes
end