Sha256: f49ab9319fa4097b729919ccbe71ec9ded85737c74c0c4c80aeaf8625abe76a2

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

#!/usr/bin/env ruby
# -*- encoding: utf-8 -*-
# Copyright muflax <mail@muflax.com>, 2013
# License: GNU GPL 3 <http://www.gnu.org/copyleft/gpl.html>

# make it possible to track data in attributes
class Class
  alias_method :attr_reader_without_tracking, :attr_reader
  def attr_reader(*names)
    attr_readers.concat(names)
    attr_reader_without_tracking(*names)
  end

  def attr_readers
    @attr_readers ||= [ ]
  end

  alias_method :attr_writer_without_tracking, :attr_writer
  def attr_writer(*names)
    attr_writers.concat(names)
    attr_writer_without_tracking(*names)
  end

  def attr_writers
    @attr_writers ||= [ ]
  end

  alias_method :attr_accessor_without_tracking, :attr_accessor
  def attr_accessor(*names)
    attr_readers.concat(names)
    attr_writers.concat(names)
    attr_accessor_without_tracking(*names)
  end
end

class Object
  def differences_with(other)
    # get list of all attributes
    attrs = (self.class.attr_readers + self.class.attr_writers).uniq.sort

    # pick all attributes that they disagree about
    attrs.reject do |attr|
      self.respond_to? attr and other.respond_to? attr and self.send(attr) == other.send(attr)
    end
  end

  # consistent string length
  def str_length
    HighLine.uncolor(self.to_s).length
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
muflax-0.1.35 lib/muflax/objects.rb
muflax-0.1.34 lib/muflax/objects.rb