Sha256: 243cb763c3aa61079539592ebb5a1fbaa8366be17b750950fec44740e171a38f

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

module TableHelper
  def remove_dynamically_defined_all_method
    klasses = [Facemock::Database::Table,
               Facemock::Application,
               Facemock::User,
               Facemock::Permission,
               Facemock::AuthorizationCode ]
    klasses.each do |klass|
      remove_dynamically_defined_class_method(klass)
      remove_dynamically_defined_instance_method(klass)
    end
  end

  # テストで動的に定義したクラスメソッドを削除
  def remove_dynamically_defined_class_method(klass)
    klass.methods.each do |method_name|
      if method_name.to_s =~ /^find_by_/ || method_name.to_s =~ /^find_all_by_/
        klass.class_eval do
          class_variable_set(:@@target_method_name, method_name)
          class << self
            remove_method class_variable_get(:@@target_method_name)
          end
          remove_class_variable(:@@target_method_name)
        end
      end
    end
  end

  # テストで動的に定義したインスタンスメソッドを削除
  def remove_dynamically_defined_instance_method(klass)
    klass.column_names.each do |column_name|
      getter = column_name
      if klass.instance_methods.include?(getter)
        klass.class_eval { remove_method getter }
      end

      setter = (column_name.to_s + "=").to_sym
      if klass.instance_methods.include?(setter)
        klass.class_eval { remove_method setter }
      end
    end
  end

  # Tableクラスでテストするために、一時的にDB Tableを作成する
  def create_tables_table_for_test
    db = Facemock::Database.new
    db.connection.execute <<-SQL
      CREATE TABLE TABLES (
        id          INTEGER   PRIMARY KEY AUTOINCREMENT,
        text        TEXT      NOT NULL,
        active      BOOLEAN,
        number      INTEGER   NOT NULL,
        created_at  DATETIME  NOT NULL
      );
    SQL
    db.disconnect!
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
facemock-0.0.11 spec/support/tables_helper.rb
facemock-0.0.10 spec/support/tables_helper.rb
facemock-0.0.9 spec/support/tables_helper.rb
facemock-0.0.8 spec/support/tables_helper.rb