Sha256: 066227d18d38e996b3f4f5c93f3dd0eca4d246ea0fcc3f2968d9477495f45995

Contents?: true

Size: 940 Bytes

Versions: 1

Compression:

Stored size: 940 Bytes

Contents

module Qcontent
  class Dimension
    class InvalidDimension < ::RuntimeError; end;
    include Comparable
    
    attr_accessor :width, :height

    def initialize(*args)
      first = args.shift
      case first
      when Array
        self.width, self.height = first
      when Hash
        self.width  = first['width'] || first[:width]
        self.height = first['height'] || first[:height]
      when Dimension
        return first
      else
        self.width, self.height = (first.is_a?(String) ? first.split('x') : first)
        self.height ||= args.shift
      end
    end

    def ==(other)
      self.width == other.width && self.height == other.height
    end
    
    def width=(w)
      @width = w.nil? ? nil : w.to_i
    end
    
    def height=(h)
      @height = h.nil? ? nil : h.to_i
    end
    
    def to_s(join = 'x')
      "#{width}#{join}#{height}"
    end

    def to_a
      [width, height]
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
qcontent-0.1.0 lib/qcontent/dimension.rb