Sha256: f7ffe5aa1c17aef6ca2c58b01371ced2439847ba6a0fb1ba156ffeafac95c5e3

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

module Must
  module SameStruct
    def self.check(src, dst)
      case src
      when Hash
        return true if dst == Hash
        dst.must(Hash)
        return true if src.empty?
        return true if dst.empty?
        key1, val1 = src.first
        key2, val2 = dst.first
        key1.must(key2)
        val1.must(val2)

      when Array
        return true if dst == Array
        dst.must(Array)
        return true if src.empty?
        return true if dst.empty?
        src.first.must(dst.first)

      else
        # 1.must.struct(2)
        # 1.must.struct(Integer)
        dst_class = classify(dst)
        return true if classify(src).ancestors.include?(dst_class)

        # Fixnum.must.struct(2)
        return true if class?(src) and src.ancestors.include?(dst_class)

        return false
      end
      return true

    rescue Must::Invalid
      return false
    end

    def self.classify(obj)
      class?(obj) ? obj : obj.class
    end

    def self.class?(obj)
      obj.class.to_s =~ /\A(Class|Module)\Z/o
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
must-0.2.4 lib/must/same_struct.rb