class Ehpt::CreateStories

Attributes

csv_file[R]

Public Class Methods

new(csv_file) click to toggle source
Calls superclass method Ehpt::Base::new
# File lib/ehpt/create_stories.rb, line 5
def initialize(csv_file)
  @csv_file = csv_file
  super
end

Public Instance Methods

call() click to toggle source
# File lib/ehpt/create_stories.rb, line 10
def call
  validate_csv_file!
  create_stories unless error?
rescue StandardError => e
  add_error(e.message)
end

Private Instance Methods

create_stories() click to toggle source
# File lib/ehpt/create_stories.rb, line 26
def create_stories
  CSV.foreach(csv_file, headers: true) do |row|
    story_attrs = create_story_attributes(row)

    next if error?

    story_creator = Ehpt::CreateStory.new(story_attrs)
    story_creator.call

    if story_creator.success?
      puts "Created story: #{story_creator.data.name}"
    else
      add_error({
        row: row.to_h,
        errors: story_creator.errors
      })
    end
  end
end
create_story_attributes(row) click to toggle source
# File lib/ehpt/create_stories.rb, line 46
def create_story_attributes(row)
  attr_creator = Ehpt::CreateStoryAttributes.new(row)
  attr_creator.call

  add_warning(attr_creator.warnings) if attr_creator.warning?

  if attr_creator.error?
    add_error(attr_creator.errors)
    return
  end

  attr_creator.data
end
validate_csv_file!() click to toggle source
# File lib/ehpt/create_stories.rb, line 19
def validate_csv_file!
  if File.extname(csv_file) != '.csv'
    add_error('Input file must be a csv file' )
  end
  add_error('CSV content is empty') if File.zero?(csv_file)
end