Sha256: a7654064e4b06d73581c88d1766cfcf2e6f1db50425f42d1d25b3136cab595dc

Contents?: true

Size: 1.37 KB

Versions: 4

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'yaml'

class DataDump < Aid::Script
  def self.description
    'Helpers to get data out of the application.'
  end

  def self.help
    <<~HELP
      Usage: $ aid data [data_type]
      Available data types are: #{available_data_types.join(', ')}
    HELP
  end

  def self.available_data_types
    %w[questions]
  end

  def run
    exit_with('Please include a data type.') if argv.empty?
    exit_with('Please include a single data type.') if argv.length != 1

    unless self.class.available_data_types.include?(data_type)
      message = <<~HELP
        #{data_type} is not a valid data type.
        Available ones are: #{self.class.available_data_types.join(', ')}
      HELP
      exit_with(message)
    end

    puts dump_data
  end

  private

  def data_type
    argv.first
  end

  def exit_with(message)
    puts message
    exit 1
  end

  def dump_data
    send(data_type.to_sym)
  end

  def questions
    [
      section_yml('setup'),
      section_yml('petition')
    ].map do |section|
      section['chapters'].map do |chapter|
        chapter['panels'].map { |p| p['name'] }
      end
    end.flatten
  end

  def section_yml(section_name)
    YAML.safe_load(File.read(section_yml_file_path(section_name)))
  end

  def section_yml_file_path(section_name)
    File.expand_path("./app/views/applications/sections/#{section_name}.yml")
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
aid-0.2.2 examples/data_dump.rb
abtion-aid-0.3.2 examples/data_dump.rb
abtion-aid-0.3.1 examples/data_dump.rb
abtion-aid-0.2.0 examples/data_dump.rb