Sha256: b42409fc70e2d35c4c2d37fb945aff12a557b72ee040b394711b60182f39f5df

Contents?: true

Size: 1.73 KB

Versions: 8

Compression:

Stored size: 1.73 KB

Contents

module Ecoportal
  module API
    class V1
      class Person < Common::BaseModel
        passthrough :id, :external_id, :name, :email, :supervisor_id, to: :doc

        def supervisor(client)
          return @supervisor if defined?(@supervisor)
          return @supervisor = nil if supervisor_id.nil?
          @supervisor = client.people.get(supervisor_id).result
        end

        def supervisor=(person)
          self.supervisor_id = person.id
        end

        def as_json
          super.merge "details" => details&.as_json
        end

        def details
          return @details if defined?(@details)
          return @details = nil if doc["details"].nil?
          @details = person_details_class.new(doc["details"])
        end

        def details=(value)
          case value
          when NilClass
            doc["details"] = nil
          when person_details_class
            doc["details"] = value.as_json
          when Hash
            doc["details"] = value.slice("schema_id", "fields")
          else
            raise "Invalid set on details: Need nil, PersonDetails or Hash; got #{value.class}"
          end
          remove_instance_variable("@details") if defined?(@details)
          return details
        end

        def add_details(schema_or_id)
          self.details = person_details_class.new
          case schema_or_id
          when person_schema_class
            schema_or_id.initialize_details(self.details)
          when String
            self.details.schema_id = schema_id
          end
          self.details
        end

        private

        def person_schema_class
          V1::PersonSchema
        end

        def person_details_class
          V1::PersonDetails
        end
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
ecoportal-api-0.1.11 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.10 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.9 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.8 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.7 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.6 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.5 lib/ecoportal/api/v1/person.rb
ecoportal-api-0.1.4 lib/ecoportal/api/v1/person.rb