Class: FlatKit::Stats

Inherits:
Object
  • Object
show all
Includes:
EventEmitter
Defined in:
lib/flat_kit/stats.rb

Constant Summary collapse

AllFields =
Class.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from EventEmitter

#_listeners, #add_listener, #count_listeners, #notify_listeners, #remove_listener, #remove_listeners

Constructor Details

#initialize(input:, input_fallback: "auto", output:, output_fallback: "auto", fields_to_stat: AllFields, stats_to_collect: FieldStats::CORE_STATS) ⇒ Stats

Returns a new instance of Stats.



13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/flat_kit/stats.rb', line 13

def initialize(input:, input_fallback: "auto",
               output:, output_fallback: "auto",
               fields_to_stat: AllFields, stats_to_collect: FieldStats::CORE_STATS)

  @fields_to_stat   = fields_to_stat
  @stats_to_collect = stats_to_collect
  @stats_by_field   = Hash.new
  @record_count     = 0

  @reader = ::FlatKit::Reader.create_reader_from_path(path: input, fallback: input_fallback)
  @writer = ::FlatKit::Writer.create_writer_from_path(path: output, fallback: output_fallback,
                                                      reader_format: @reader.format_name)
end

Instance Attribute Details

#fields_to_statObject (readonly)

Returns the value of attribute fields_to_stat.



9
10
11
# File 'lib/flat_kit/stats.rb', line 9

def fields_to_stat
  @fields_to_stat
end

#readerObject (readonly)

Returns the value of attribute reader.



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

def reader
  @reader
end

#stats_by_fieldObject (readonly)

Returns the value of attribute stats_by_field.



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

def stats_by_field
  @stats_by_field
end

#stats_to_collectObject (readonly)

Returns the value of attribute stats_to_collect.



10
11
12
# File 'lib/flat_kit/stats.rb', line 10

def stats_to_collect
  @stats_to_collect
end

#writerObject (readonly)

Returns the value of attribute writer.



8
9
10
# File 'lib/flat_kit/stats.rb', line 8

def writer
  @writer
end

Instance Method Details

#callObject



27
28
29
30
31
# File 'lib/flat_kit/stats.rb', line 27

def call
  calculate_stats
  write_stat_records
  @writer.close
end

#collecting_stats_on_field?(name) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
# File 'lib/flat_kit/stats.rb', line 33

def collecting_stats_on_field?(name)
  return true if @fields_to_stat == AllFields
  return @fields_to_stat.include?(name)
end