class Findable::Seed

Public Class Methods

from_seed_dir(seed_dir, seed_file) click to toggle source
# File lib/findable/seed.rb, line 42
def from_seed_dir(seed_dir, seed_file)
  pathname(seed_file).relative_path_from(seed_dir)
end
model_name(seed_dir, seed_file) click to toggle source
# File lib/findable/seed.rb, line 34
def model_name(seed_dir, seed_file)
  if seed_dir != seed_file.dirname && seed_file.basename.to_s.match(/^data/)
    from_seed_dir(seed_dir, seed_file.dirname).to_s.classify
  else
    from_seed_dir(seed_dir, without_ext(seed_file)).to_s.classify
  end
end
pathname(path) click to toggle source
# File lib/findable/seed.rb, line 50
def pathname(path)
  case path
  when Pathname then path
  when String then Pathname.new(path)
  else nil
  end
end
target_files(seed_dir: nil, seed_files: nil) click to toggle source
# File lib/findable/seed.rb, line 17
def target_files(seed_dir: nil, seed_files: nil)
  target_dir = pathname(seed_dir || Findable.config.seed_dir)
  raise UnknownSeedDir.new(target_dir) unless target_dir.try(:exist?)

  seed_files = seed_files.map!(&:to_s) if seed_files
  _model_name = method(:model_name).to_proc.curry.call(target_dir)
  _selected = Proc.new do |seed|
    seed_files.present? ? seed.table_name.in?(seed_files) : true
  end

  Pathname.glob(target_dir.join("**", "*"))
    .select(&:file?)
    .group_by(&_model_name)
    .map {|name, files| new(files, name) }
    .select(&_selected)
end
without_ext(seed_file) click to toggle source
# File lib/findable/seed.rb, line 46
def without_ext(seed_file)
  pathname(seed_file).dirname.join(pathname(seed_file).basename(".*"))
end

Public Instance Methods

bootstrap!() click to toggle source
# File lib/findable/seed.rb, line 74
def bootstrap!
  model.query.lock do
    model.delete_all
    model.query.import load_files
  end
end
load_files() click to toggle source
# File lib/findable/seed.rb, line 63
def load_files
  seed_files.sort_by(&:to_s).inject([]) do |data, file|
    data | case file.extname
      when ".yml" then load_yaml(file)
      when ".csv" then load_csv(file)
      else
        raise UnknownFormat
      end
  end
end
model() click to toggle source
# File lib/findable/seed.rb, line 59
def model
  @_model_class ||= model_name.constantize
end

Private Instance Methods

load_csv(seed_file) click to toggle source
# File lib/findable/seed.rb, line 86
def load_csv(seed_file)
  CSV.table(seed_file).map(&:to_h)
end
load_yaml(seed_file) click to toggle source
# File lib/findable/seed.rb, line 82
def load_yaml(seed_file)
  YAML.load_file(seed_file).values
end