class BigSister::Reporter

Public Class Methods

new(schema, id) click to toggle source
# File lib/bigsister/reporter.rb, line 3
def initialize(schema, id)
  @id = id
  @schema = schema
  @files = []
  @directories = []
  if !@schema.key?("format")
    raise BigSister::InvalidConfiguration.new("Reporter format is required")
  elsif !%w(summary detail).include?(@schema.fetch("format"))
    raise BigSister::InvalidConfiguration.new("Reporter format must be one of: summary, detail")
  else
    @format = @schema.fetch("format")
  end
  @columns = schema.fetch("columns", [])
  @columns.each { |column|
    validate_column!(column)
  }
end

Public Instance Methods

detail?() click to toggle source
# File lib/bigsister/reporter.rb, line 33
def detail?
  return !summary?
end
log_directories?() click to toggle source
# File lib/bigsister/reporter.rb, line 41
def log_directories?
  return @schema.fetch("include", []).include?("directories")
end
log_directory(path) click to toggle source
# File lib/bigsister/reporter.rb, line 25
def log_directory(path)
  @directories.push(path)
end
log_file(path) click to toggle source
# File lib/bigsister/reporter.rb, line 21
def log_file(path)
  @files.push(path)
end
log_files?() click to toggle source
# File lib/bigsister/reporter.rb, line 37
def log_files?
  return @schema.fetch("include", []).include?("files")
end
summary?() click to toggle source
# File lib/bigsister/reporter.rb, line 29
def summary?
  return @format == "summary"
end

Protected Instance Methods

current_timestamp() click to toggle source
# File lib/bigsister/reporter.rb, line 110
def current_timestamp
  DateTime.now.strftime("%FT%T%z")
end
detail() click to toggle source
# File lib/bigsister/reporter.rb, line 122
def detail
  rows = []
  rows += file_rows if log_files?
  rows += directory_rows if log_directories?
  rows
end
directory_rows() click to toggle source
# File lib/bigsister/reporter.rb, line 102
def directory_rows
  @directories.map { |dir|
    @columns.map { |column|
      transform_directory(dir, column)
    }
  }
end
file_rows() click to toggle source
# File lib/bigsister/reporter.rb, line 94
def file_rows
  @files.map { |file|
    @columns.map { |column|
      transform_file(file, column)
    }
  }
end
rows() click to toggle source
# File lib/bigsister/reporter.rb, line 114
def rows
  if summary?
    summary
  else
    detail
  end
end
summary() click to toggle source
# File lib/bigsister/reporter.rb, line 129
def summary
  file_count = file_rows.size
  directory_count = directory_rows.size
  @columns.map { |column|
    type = column["type"]
    if type == "timestamp"
      current_timestamp
    elsif type == "file_count"
      file_count
    elsif type == "directory_count"
      directory_count
    elsif type == "literal"
      column.fetch("value", nil)
    end
  }
end
transform_directory(directory, column) click to toggle source
# File lib/bigsister/reporter.rb, line 77
def transform_directory(directory, column)
  type = column["type"]
  if type == "timestamp"
    current_timestamp
  elsif type == "name"
    directory.name
  elsif type == "type"
    directory.type
  elsif type == "file_count"
    directory.file_count
  elsif type == "directory_count"
    directory.directory_count
  elsif type == "literal"
    column.fetch("value", nil)
  end
end
transform_file(file, column) click to toggle source
# File lib/bigsister/reporter.rb, line 62
def transform_file(file, column)
  type = column["type"]
  if type == "timestamp"
    DateTime.now.strftime("%FT%T%:z")
  elsif type == "name"
    file.name
  elsif type == "type"
    file.type
  elsif type == "file_size"
    file.file_size
  elsif type == "literal"
    column.fetch("value", nil)
  end
end
validate_column!(column) click to toggle source
# File lib/bigsister/reporter.rb, line 47
def validate_column!(column)
  if !column.key?("type")
    raise BigSister::InvalidConfiguration.new("Column type is required.")
  end
  allowed_types = %w(timestamp literal file_count directory_count)
  if detail?
    allowed_types += %w(file_size type name)
  end
  type = column.fetch("type")
  if !allowed_types.include?(type)
    list = allowed_types.join(", ")
    raise BigSister::InvalidConfiguration.new("Column type for #{@format} must be one of: #{list}")
  end
end