class SeedGimmick::Inflector

Attributes

seed_dir[R]

Public Class Methods

build(options = nil) click to toggle source
# File lib/seed_gimmick/inflector.rb, line 6
def build(options = nil)
  new(options || Options.new)
end
ext_type(path) click to toggle source

Extension type. @return [Symbol]

# File lib/seed_gimmick/inflector.rb, line 35
def ext_type(path)
  pathname(path).extname.sub(/\A./, '').to_sym
end
model_class(name) click to toggle source

Convert String to model class. @param name [String]

# File lib/seed_gimmick/inflector.rb, line 12
def model_class(name)
  name.try(:safe_constantize)
end
new(options) click to toggle source
# File lib/seed_gimmick/inflector.rb, line 40
def initialize(options)
  @seed_dir = pathname(options.seed_dir)
end
pathname(path) click to toggle source

Convert String to Pathname. @return [Pathname] @return [nil]

# File lib/seed_gimmick/inflector.rb, line 19
def pathname(path)
  case path
  when Pathname then path
  when String then Pathname.new(path)
  else nil
  end
end
without_ext(path) click to toggle source

Path without extension. @return [Pathname]

# File lib/seed_gimmick/inflector.rb, line 29
def without_ext(path)
  pathname(path).dirname.join(pathname(path).basename(".*"))
end

Public Instance Methods

model_for(seed_file) click to toggle source

Convert seed file path to model class. @param seed_file [Pathname] @return [ActiveRecord::Base]

# File lib/seed_gimmick/inflector.rb, line 47
def model_for(seed_file)
  name = from_seed_dir(self.class.without_ext(seed_file)).to_s.classify
  self.class.model_class(name)
end
seed_for(model, format = :yml) click to toggle source

Convert model class to seed file path. @param model [ActiveRecord::Base] @param format [Symbol] Extension type. @return [Pathname]

# File lib/seed_gimmick/inflector.rb, line 56
def seed_for(model, format = :yml)
  seed_dir.join(pathname(model.model_name.collection).sub_ext(".#{format}"))
end

Private Instance Methods

from_seed_dir(path) click to toggle source

Convert lerative path from seed_dir.

# File lib/seed_gimmick/inflector.rb, line 62
def from_seed_dir(path)
  path = pathname(path)
  path.relative? ? path : path.relative_path_from(seed_dir)
end
pathname(path) click to toggle source
# File lib/seed_gimmick/inflector.rb, line 67
def pathname(path)
  self.class.pathname(path)
end