Sha256: bd1b19469040dc09b05f67ccf6140d80819fcc3a7e1cd6566ae1b36bb8fcafba

Contents?: true

Size: 1.15 KB

Versions: 2

Compression:

Stored size: 1.15 KB

Contents

require 'active_support/core_ext/string/inflections.rb'

module FixtureOverlord
  class Model

    class << self
      def init(hash, filename)
        new(hash, filename).new_model_with_values
      end

      def create(hash, filename)
        new(hash, filename).create_as_model
      end
      alias_method :create!, :create
    end

    def initialize(hash, filename)
      @hash, @filename = hash, filename
    end

    def convert_to_model
      model_name.new( sanitize_hash )
    end

    def new_model_with_values
      model_name.new( sanitize_hash )
    end

    def create_as_model
      model_name.create( sanitize_hash )
    end

    def model_name
      @model_name ||= @filename.to_s.singularize.classify.constantize
    end

    def can_convert_to_model?
      model = model_name
      model.respond_to?(:superclass) &&
        model.superclass == ::ActiveRecord::Base
    end

    def respond_to_model_methods?
      model = convert_to_model
      model.respond_to?(:create!) ||
        model.respond_to?(:create) ||
        model.respond_to?(:build)
    end

    def sanitize_hash
      @hash.delete_if { |key,value| key.to_sym == :id }
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
fixture_overlord-0.1.9 lib/fixture_overlord/model.rb
fixture_overlord-0.1.8 lib/fixture_overlord/model.rb