Sha256: 6199d28be265d26407efa4fa278505fa9b4cd433acd60bcefce2c84f01124843

Contents?: true

Size: 1.71 KB

Versions: 13

Compression:

Stored size: 1.71 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)
    hash == obj.attributes.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

13 entries across 13 versions & 1 rubygems

Version Path
active_mocker-1.2 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.11 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.10 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.9 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.8 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.7 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.6 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.5 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.4 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.3 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.2 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre.1 lib/active_mocker/mock_instance_methods.rb
active_mocker-1.2.pre lib/active_mocker/mock_instance_methods.rb