Class: FiasReader::Scope

Inherits:
Object
  • Object
show all
Defined in:
lib/fias_reader/scope.rb

Overview

Инерфейс для работы XML с базой ФИАС

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(table) ⇒ Scope

Returns a new instance of Scope.



6
7
8
9
10
# File 'lib/fias_reader/scope.rb', line 6

def initialize(table)
  @table = table
  @options = {}
  @logics = @table.class::LOGICS.dup
end

Instance Attribute Details

#logicsObject (readonly)

Returns the value of attribute logics.



4
5
6
# File 'lib/fias_reader/scope.rb', line 4

def logics
  @logics
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/fias_reader/scope.rb', line 4

def options
  @options
end

Instance Method Details

#allObject



12
13
14
15
# File 'lib/fias_reader/scope.rb', line 12

def all
  @options[:rows] = -> (row) { yield row }
  parse!
end

#default_parserObject



40
41
42
# File 'lib/fias_reader/scope.rb', line 40

def default_parser
  FiasReader::Parser.new(logics, @options)
end

#firstObject



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/fias_reader/scope.rb', line 17

def first
  result = nil
  catch :fias_parse_stop do
    @options[:rows] = -> (row) do
      result = row
      throw :fias_parse_stop
    end
    parse!
  end
  result
end

#parse!(parser = default_parser) ⇒ Object



36
37
38
# File 'lib/fias_reader/scope.rb', line 36

def parse!(parser = default_parser)
  Ox.sax_parse parser, @table.file, symbolize: true
end

#select(*args) ⇒ Object



29
30
31
32
33
34
# File 'lib/fias_reader/scope.rb', line 29

def select(*args)
  index = @logics.index(FiasReader::ParseLogic::Attributes)
  @logics[index] = FiasReader::ParseLogic::AttributesSelect if index
  @options[:select] = args
  self
end