Sha256: a307c3e1f400b23c152078e1d5b86589c042d08c67516c636fbfa8000744f131

Contents?: true

Size: 1.82 KB

Versions: 22

Compression:

Stored size: 1.82 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

      def unique_trustee_id(authority_slug, trustee_name)
        "#{authority_slug}.#{trustee_name}"
      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

22 entries across 22 versions & 1 rubygems

Version Path
decidim-bulletin_board-0.24.4 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.24.3 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.24.2 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.24.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.24.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.23.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.22.3 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.22.2 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.22.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.22.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.21.2 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.21.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.21.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.20.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.19.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.18.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.17.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.17.0 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.16.1 lib/decidim/bulletin_board/message_identifier.rb
decidim-bulletin_board-0.16.0 lib/decidim/bulletin_board/message_identifier.rb