Sha256: 1fdef00931ac875fd7f5ea2eaef1c359c9b35da3c2794e84182fbfe1a6429818

Contents?: true

Size: 691 Bytes

Versions: 29

Compression:

Stored size: 691 Bytes

Contents

module Onoma
  class Variety < Onoma::Record::Base
    TOPLEVEL_VARIETIES = %w[bioproduct immatter matter product product_group].freeze

    class << self
      def parent_variety(variety)
        v = find(variety)

        if v.nil?
          nil
        elsif (toplevel = toplevel_parent(v)).present?
          toplevel.name
        else
          v.name
        end
      end

      def toplevel_parent(variety)
        ancestors(variety).last
      end

      def ancestors(variety)
        if variety.parent.nil? || TOPLEVEL_VARIETIES.include?(variety.parent.name)
          []
        else
          [variety.parent, *ancestors(variety.parent)]
        end
      end
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
onoma-0.9.3 lib/onoma/models/variety.rb
onoma-0.9.2 lib/onoma/models/variety.rb
onoma-0.9.1 lib/onoma/models/variety.rb
onoma-0.9.0 lib/onoma/models/variety.rb
onoma-0.8.9 lib/onoma/models/variety.rb
onoma-0.8.8 lib/onoma/models/variety.rb
onoma-0.8.7 lib/onoma/models/variety.rb
onoma-0.8.6 lib/onoma/models/variety.rb
onoma-0.8.5 lib/onoma/models/variety.rb
onoma-0.8.4 lib/onoma/models/variety.rb
onoma-0.8.3 lib/onoma/models/variety.rb
onoma-0.8.2 lib/onoma/models/variety.rb
onoma-0.8.1 lib/onoma/models/variety.rb
onoma-0.8.0 lib/onoma/models/variety.rb
onoma-0.7.0 lib/onoma/models/variety.rb
onoma-0.6.4 lib/onoma/models/variety.rb
onoma-0.6.3 lib/onoma/models/variety.rb
onoma-0.6.2 lib/onoma/models/variety.rb
onoma-0.6.0 lib/onoma/models/variety.rb
onoma-0.5.6 lib/onoma/models/variety.rb