Sha256: fc2be405b19ec1dbeb93a5fd8280074716bd5f93a8adc27b6f52ed57a40561e7

Contents?: true

Size: 1.9 KB

Versions: 14

Compression:

Stored size: 1.9 KB

Contents

module Ecoportal
  module API
    class Internal
      # @attr account [Account, nil] the account of the person or `nil` if missing.
      class Person < V1::Person
        class_resolver :person_details_class, "Ecoportal::API::Internal::PersonDetails"
        class_resolver :person_account_class, "Ecoportal::API::Internal::Account"
        embeds_one :account, nullable: true, klass: :person_account_class

        def as_json
          super.update("account" => account&.as_json)
        end

        # Sets the Account to the person, depending on the paramter received:
        #  - `nil`: blanks the account.
        #  - `Account`: sets a copy of the object param as account.
        #  - `Hash`: slices the properties of `Account`.
        # @note this method does not make dirty the account (meaning that `as_json` will be an empty hash `{}`)
        # @param value [nil, Account, Hash] value to be set.
        # @return [nil, Account] the resulting `Account` set to the person.
        def account=(value)
          case value
          when NilClass
            doc["account"] = nil
          when Internal::Account
            doc["account"] = JSON.parse(value.to_json)
          when Hash
            doc["account"] = value.slice(*%w[policy_group_ids default_tag landing_page_id permissions_preset permissions_custom preferences prefilter login_provider_ids starred_ids])
          else
            # TODO
            raise "Invalid set on account: Need nil, Account or Hash; got #{value.class}"
          end
          remove_instance_variable("@account") if defined?(@account)
          return account
        end

        # Adds an empty account to the person.
        # @note if the person exists, and does not have an account, an this will send an invite.
        # @note this will **not** change the account properties of this person.
        def add_account
          self.account = {}
        end

      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
ecoportal-api-0.8.2 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.5 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.4 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.3 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.2 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.1 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.7.0 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.6.0 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.8 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.7 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.6 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.5 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.3 lib/ecoportal/api/internal/person.rb
ecoportal-api-0.5.1 lib/ecoportal/api/internal/person.rb