Sha256: 59719f3f1c2a8f9410d455fba52aee6ef7c27548d0719aa166d18f8b7e23f5aa

Contents?: true

Size: 913 Bytes

Versions: 2

Compression:

Stored size: 913 Bytes

Contents

module DagLinkCalculator
  ANCESTOR_KEYS = [:ancestor_id, :ancestor, :parent_id, :parent].freeze
  DESCENDANT_KEYS = [:descendant_id, :descendant, :child_id, :child].freeze

  DirectLink = Struct.new(:ancestor_id, :descendant_id) do
    def self.from_hash(hash)
      ancestor_id = fetch_from hash, ANCESTOR_KEYS
      descendant_id = fetch_from hash, DESCENDANT_KEYS
      new(ancestor_id, descendant_id)
    end

    def self.fetch_from(hash, keys)
      keys.each do |k|
        return hash[k] if hash[k]
      end

      raise KeyError, "none of these keys found in the given hash: #{keys}"
    end

    def direct
      true
    end

    def direct?
      direct
    end

    def count
      1
    end

    def to_link
      NodeLink.new(ancestor_id, descendant_id, direct?, count)
    end

    def <=>(other)
      [ancestor_id, descendant_id] <=> [other.ancestor_id, other.descendant_id]
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
dag_link_calculator-1.0.0 lib/dag_link_calculator/direct_link.rb
dag_link_calculator-0.1.0 lib/dag_link_calculator/direct_link.rb