Sha256: 6602f42a6b09294199be129654f191c1a59273dbe0b4fa19ee028bf3f9a72985

Contents?: true

Size: 1.72 KB

Versions: 4

Compression:

Stored size: 1.72 KB

Contents

module Ecoportal
  module API
    class Internal
      class Account < Common::BaseModel
        passthrough :policy_group_ids, :landing_page_id, :permissions_preset, :permissions_custom, :preferences, :prefilter, :filter_tags, :login_provider_ids, :starred_ids, to: :doc

        VALID_TAG_REGEX = /^[A-Za-z0-9 &_'\/-]+$/

        def preset=(value)
          self.permissions_preset = value == "custom" ? nil : value
        end

        def filter_tags=(value)
          unless value.is_a?(Array)
            raise "filter_tags= needs to be passed an Array, got #{value.class}"
          end
          doc["filter_tags"] = value.map do |tag|
            unless tag.match(VALID_TAG_REGEX)
              raise "Invalid filter tag #{tag.inspect}"
            end
            tag.upcase
          end
        end

        def preset
          self.permissions_preset.nil? ? "custom" : self.permissions_preset
        end

        def as_json
          super.tap do |hash|
            if preset == "custom"
              hash["permissions_custom"] = permissions.as_json
            else
              hash.delete "permissions_custom"
            end
            hash["preferences"] = preferences.as_json
          end
        end

        def permissions
          return @permissions if defined?(@permissions)
          doc["permissions_custom"] ||= {}
          @permissions = Internal::Permissions.new(doc["permissions_custom"])
        end

        def preferences
          return @preferences if defined?(@preferences)
          doc["preferences"] ||= {}
          @preferences = Internal::Preferences.new(doc["preferences"])
        end
      end
    end
  end
end
require 'ecoportal/api/internal/permissions'
require 'ecoportal/api/internal/preferences'

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
ecoportal-api-0.3.0 lib/ecoportal/api/internal/account.rb
ecoportal-api-0.3.0.pre1 lib/ecoportal/api/internal/account.rb
ecoportal-api-0.2.2 lib/ecoportal/api/internal/account.rb
ecoportal-api-0.2.1 lib/ecoportal/api/internal/account.rb