Sha256: 3f735c71b93b3a4c4dbac3e3d187fb660d810841c731710c4c4880d642acd73e

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

module ActiveMocker
module MockInstanceMethods


  def mock_instance_method(method, &block)
    model_instance_methods[method] = block
  end

  def inspect
    inspection =  self.class.column_names.map { |name|
      "#{name}: #{attribute_for_inspect(name)}"
    }.compact.join(", ")

    "#<#{self.class} #{inspection}>"
  end

  def attribute_for_inspect(attr_name)
    value = self.attributes[attr_name]
    if value.is_a?(String) && value.length > 50
      "#{value[0, 50]}...".inspect
    elsif value.is_a?(Date) || value.is_a?(Time)
      %("#{value.to_s(:db)}")
    elsif value.is_a?(Array) && value.size > 10
      inspected = value.first(10).inspect
      %(#{inspected[0...-1]}, ...])
    else
      value.inspect
    end
  end

  def hash
    attributes.hash
  end

  def ==(obj)
    return false if obj.nil?
    return hash == obj.attributes.hash if obj.respond_to?(:attributes)
    hash == obj.hash if obj.respond_to?(:hash)
  end

  private

  def read_attribute(attr)
    @attributes[attr]
  end

  def write_attribute(attr, value)
    @attributes[attr] = value
  end

  def read_association(attr)
    @associations[attr]
  end

  def write_association(attr, value)
    @associations[attr] = value
  end

  def attribute_to_string
    attributes.map {|k, v| "#{k.to_s}: #{v.inspect}"}.join(', ')
  end

  def delegate_to_model_instance(method, *args)
    self.class.send(:delegate_to_model_instance, method, *args)
  end

  def delegate_to_model_class(method, *args)
    self.class.send(:delegate_to_model_class, method, *args)
  end

  def model_instance_methods
    @model_instance_methods ||= self.class.send(:model_instance_methods)
  end

  def model_class_methods
    @model_class_methods ||= self.class.send(:model_class_methods)
  end

  def schema_attributes
    @schema_attributes ||= self.class.send(:attribute_template).dup
  end

end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
active_mocker-1.3.2 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.3.1 lib/active_mocker/mock_instance_methods.rb