lib/seed_gimmick/inflector.rb in seed_gimmick-0.0.1 vs lib/seed_gimmick/inflector.rb in seed_gimmick-0.0.2

- old
+ new

@@ -1,35 +1,50 @@ module SeedGimmick class Inflector attr_reader :seed_dir - def initialize(seed_dir) - @seed_dir = seed_dir + class << self + def build(options = nil) + new(options || Options.new) + end + + def model_class(name) + name.try(:safe_constantize) + end + + def pathname(path) + case path + when Pathname then path + when String then Pathname.new(path) + else nil + end + end end + def initialize(options) + @seed_dir = options.seed_dir + end + def model_for(seed_file) - relative_path(without_ext(seed_file)).to_s.classify.constantize + class_name = relative_path(without_ext(seed_file)).to_s.classify + self.class.model_class(class_name) end def seed_for(model, format = :yml) - seed_dir + _pathname(model.model_name.collection).sub_ext(".#{format}") + seed_dir + pathname(model.model_name.collection).sub_ext(".#{format}") end private - def without_ext(pathname) - pathname.dirname + pathname.basename(".*") + def without_ext(path) + path.dirname + path.basename(".*") end - def relative_path(pathname) - pathname.relative? ? pathname : pathname.relative_path_from(seed_dir) + def relative_path(path) + path.relative? ? path : path.relative_path_from(seed_dir) end - def _pathname(path) - case path - when Pathname then path - when String then Pathname.new(path) - else nil - end + def pathname(path) + self.class.pathname(path) end end end