Sha256: 850b5d775844e279fa714aeccab4ccf116e512439083b1d90eab1e7c7d510988

Contents?: true

Size: 1.39 KB

Versions: 9

Compression:

Stored size: 1.39 KB

Contents

module Ecoportal
  module API
    class V1
      class PersonSchema < Common::BaseModel
        passthrough :id, :name
        passthrough :enable_tags, :tags

        class_resolver :schema_field_class, "Ecoportal::API::V1::SchemaField"

        def fields
          @fields_by_id or index_fields
          @fields_by_id.values
        end

        def fields_by_id
          @fields_by_id or index_fields
          @fields_by_id
        end

        def fields_by_alt_id
          @fields_by_alt_id or index_fields
          @fields_by_alt_id
        end

        def [](id)
          @fields_by_alt_id or index_fields
          @fields_by_id[id] || @fields_by_alt_id[id]
        end

        def index_fields
          @fields_by_id     = {}
          @fields_by_alt_id = {}

          doc["fields"].each do |field|
            wrapped = schema_field_class.new(field)
            @fields_by_id[wrapped.id] = wrapped
            @fields_by_alt_id[wrapped.alt_id] = wrapped
          end
        end

        def initialize_details(details)
          details.schema_id = id
          details.doc["fields"] = fields.map do |field|
            field.doc.slice(*%w[id alt_id name multiple type shared]).merge(
              "value" => field.multiple ? [] : nil
            )
          end
        end
      end
    end
  end
end
require 'ecoportal/api/v1/schema_field'

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
ecoportal-api-0.10.8 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.7 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.6 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.5 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.4 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.3 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.2 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.1 lib/ecoportal/api/v1/person_schema.rb
ecoportal-api-0.10.0 lib/ecoportal/api/v1/person_schema.rb