Sha256: 8d563cf26355420ddb3ceb9882ae138361455288abd62fb077dd6a62a0617a2c

Contents?: true

Size: 1.71 KB

Versions: 6

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Decidim
  module BulletinBoard
    class MessageIdentifier
      AUTHOR_TYPE = {
        a: :authority,
        b: :bulletin_board,
        t: :trustee,
        v: :voter
      }.freeze

      INVERTED_AUTHOR_TYPE = AUTHOR_TYPE.invert.freeze

      def initialize(message_id)
        @message_id = message_id
      end

      def from_authority?
        author_type == :authority
      end

      def from_trustee?
        author_type == :trustee
      end

      def from_voter?
        author_type == :voter
      end

      def author_type
        @author_type ||= AUTHOR_TYPE[author.first.to_sym]
      end

      def author_id
        @author_id ||= author.last
      end

      def authority_id
        @authority_id ||= elements[0]
      end

      def election_id
        @election_id ||= elements[0..1].join(".")
      end

      def type
        @type ||= elements[2]
      end

      def subtype
        @subtype ||= elements[3]
      end

      def type_subtype
        @type_subtype ||= [type, subtype].compact.join(".")
      end

      def to_s
        @message_id
      end

      class << self
        def format(unique_election_id, type_subtype, author_type, author_id)
          "#{unique_election_id}.#{type_subtype}+#{INVERTED_AUTHOR_TYPE[author_type]}.#{author_id}"
        end

        def unique_election_id(authority_slug, election_id)
          "#{authority_slug}.#{election_id}"
        end
      end

      private

      attr_accessor :message_id

      def elements
        @elements ||= parts.first.split(".", 4)
      end

      def author
        @author ||= parts.last.split(".", 2)
      end

      def parts
        @parts ||= message_id.split("+")
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-bulletin_board-0.14.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.13.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.13.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.12.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.12.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.11.0 lib/decidim/bulletin_board/message_identifier.rb