Sha256: ce36fa6f4b9889c2aa25c4494dba1cef5fe8a7a2df4d933d13aa7a46a5e641c7

Contents?: true

Size: 1.83 KB

Versions: 1

Compression:

Stored size: 1.83 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).dup
  end

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

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

end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_mocker-1.3 lib/active_mocker/mock_instance_methods.rb