ecoportal-api 0.8.5 → 0.9.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +20 -20
- data/.rspec +3 -3
- data/.rubocop.yml +55 -55
- data/.travis.yml +5 -5
- data/.yardopts +10 -10
- data/CHANGELOG.md +257 -236
- data/Gemfile +6 -6
- data/LICENSE +21 -21
- data/README.md +34 -34
- data/Rakefile +27 -27
- data/bin/console +14 -14
- data/bin/setup +8 -8
- data/ecoportal-api.gemspec +36 -36
- data/lib/ecoportal/api/common/base_class.rb +33 -29
- data/lib/ecoportal/api/common/base_model.rb +195 -177
- data/lib/ecoportal/api/common/batch_operation.rb +119 -119
- data/lib/ecoportal/api/common/batch_response.rb +34 -34
- data/lib/ecoportal/api/common/client.rb +198 -196
- data/lib/ecoportal/api/common/doc_helpers.rb +29 -29
- data/lib/ecoportal/api/common/elastic_apm_integration.rb +112 -112
- data/lib/ecoportal/api/common/hash_diff.rb +41 -41
- data/lib/ecoportal/api/common/logging.rb +12 -12
- data/lib/ecoportal/api/common/response.rb +31 -31
- data/lib/ecoportal/api/common/wrapped_response.rb +54 -54
- data/lib/ecoportal/api/common.rb +18 -18
- data/lib/ecoportal/api/errors/base.rb +8 -8
- data/lib/ecoportal/api/errors/time_out.rb +8 -8
- data/lib/ecoportal/api/errors.rb +9 -9
- data/lib/ecoportal/api/internal/account.rb +99 -100
- data/lib/ecoportal/api/internal/login_provider.rb +9 -9
- data/lib/ecoportal/api/internal/login_providers.rb +33 -33
- data/lib/ecoportal/api/internal/people.rb +14 -14
- data/lib/ecoportal/api/internal/permissions.rb +14 -13
- data/lib/ecoportal/api/internal/person.rb +101 -53
- data/lib/ecoportal/api/internal/person_details.rb +9 -9
- data/lib/ecoportal/api/internal/person_schema.rb +10 -10
- data/lib/ecoportal/api/internal/person_schemas.rb +11 -11
- data/lib/ecoportal/api/internal/policy_group.rb +9 -9
- data/lib/ecoportal/api/internal/policy_groups.rb +32 -32
- data/lib/ecoportal/api/internal/preferences.rb +31 -31
- data/lib/ecoportal/api/internal/schema_field.rb +8 -8
- data/lib/ecoportal/api/internal/schema_field_value.rb +8 -8
- data/lib/ecoportal/api/internal.rb +31 -31
- data/lib/ecoportal/api/logger.rb +62 -62
- data/lib/ecoportal/api/v1/people.rb +218 -218
- data/lib/ecoportal/api/v1/person.rb +138 -135
- data/lib/ecoportal/api/v1/person_details.rb +94 -82
- data/lib/ecoportal/api/v1/person_schema.rb +53 -53
- data/lib/ecoportal/api/v1/person_schemas.rb +48 -48
- data/lib/ecoportal/api/v1/schema_field.rb +34 -34
- data/lib/ecoportal/api/v1/schema_field_value.rb +65 -65
- data/lib/ecoportal/api/v1.rb +49 -49
- data/lib/ecoportal/api/version.rb +5 -5
- data/lib/ecoportal/api.rb +16 -16
- metadata +3 -3
@@ -1,135 +1,138 @@
|
|
1
|
-
module Ecoportal
|
2
|
-
module API
|
3
|
-
class V1
|
4
|
-
# @attr id [String] the internal unique id of this person (unique in all the system).
|
5
|
-
# @attr external_id [String] the alternative unique id of this person (unique in one organization).
|
6
|
-
# @attr name [String] the name of the person.
|
7
|
-
# @attr supervisor_id [String] internal or external id of the supervisor of this person.
|
8
|
-
# @
|
9
|
-
# @
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
#
|
24
|
-
#
|
25
|
-
#
|
26
|
-
#
|
27
|
-
#
|
28
|
-
#
|
29
|
-
#
|
30
|
-
#
|
31
|
-
#
|
32
|
-
#
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
#
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
tag.
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
#
|
88
|
-
#
|
89
|
-
#
|
90
|
-
#
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
# @
|
113
|
-
#
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
end
|
1
|
+
module Ecoportal
|
2
|
+
module API
|
3
|
+
class V1
|
4
|
+
# @attr id [String] the internal unique id of this person (unique in all the system).
|
5
|
+
# @attr external_id [String] the alternative unique id of this person (unique in one organization).
|
6
|
+
# @attr name [String] the name of the person.
|
7
|
+
# @attr supervisor_id [String] internal or external id of the supervisor of this person.
|
8
|
+
# @attr contractor_organization_id [String] internal id of the contractor entity of this person.
|
9
|
+
# @attr_reader subordinates [Integer] the number of people this person is supervisor of.
|
10
|
+
# @attr details [PersonDetails, nil] the details of the person or `nil` if missing.
|
11
|
+
class Person < Common::BaseModel
|
12
|
+
passthrough :id, :external_id, :name, :email, :filter_tags
|
13
|
+
passthrough :supervisor_id, :subordinates, :contractor_organization_id
|
14
|
+
passthrough :freemium
|
15
|
+
|
16
|
+
class_resolver :person_schema_class, "Ecoportal::API::V1::PersonSchema"
|
17
|
+
class_resolver :person_details_class, "Ecoportal::API::V1::PersonDetails"
|
18
|
+
embeds_one :details, nullable: true, klass: :person_details_class
|
19
|
+
|
20
|
+
VALID_TAG_REGEX = /^[A-Za-z0-9 &_'\/.-]+$/
|
21
|
+
VALID_EMAIL_REGEX = /^[^@\s]+@[^@\s]+\.[^@\s]+$/
|
22
|
+
|
23
|
+
# Gets the supervisor (`Person`) of this person, with given his `supervisor_id`.
|
24
|
+
#
|
25
|
+
# **Example Usage**:
|
26
|
+
# ```ruby
|
27
|
+
# API_KEY = 'some-private-api-key-version'
|
28
|
+
# HOST = "live.ecoportal.com"
|
29
|
+
# api = Ecoportal::API::Internal.new(API_KEY, host: HOST)
|
30
|
+
# person = api.people.get({"id": "my-dummy-user"})
|
31
|
+
# super = person.supervisor(api.client)
|
32
|
+
# pp "#{person.name}'s supervisor is #{super.name}."
|
33
|
+
# ```
|
34
|
+
#
|
35
|
+
# @param client [Common::Client] the api client to make the request.
|
36
|
+
def supervisor(client)
|
37
|
+
return @supervisor if defined?(@supervisor)
|
38
|
+
return @supervisor = nil if supervisor_id.nil?
|
39
|
+
@supervisor = client.people.get(supervisor_id).result
|
40
|
+
end
|
41
|
+
|
42
|
+
# Sets the supervisor of a person.
|
43
|
+
# @param person [Person, nil] the supervisor of this person.
|
44
|
+
def supervisor=(person)
|
45
|
+
self.supervisor_id = person&.id || person&.external_id
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sets the email of a person.
|
49
|
+
# @param value [String, nil] the email of this person.
|
50
|
+
def email=(value)
|
51
|
+
unless !value || value.match(VALID_EMAIL_REGEX)
|
52
|
+
raise "Invalid email #{value.inspect}"
|
53
|
+
end
|
54
|
+
doc["email"] = value&.downcase
|
55
|
+
end
|
56
|
+
|
57
|
+
# Validates the string tags of the array, and sets the `filter_tags` property of the account.
|
58
|
+
# @note all is set in upper case and preserves the original order.
|
59
|
+
# @raise [Exception] if there was any invalid string tag.
|
60
|
+
# @param value [Array<String>] array of tags.
|
61
|
+
def filter_tags=(value)
|
62
|
+
unless value.is_a?(Array)
|
63
|
+
raise "filter_tags= needs to be passed an Array, got #{value.class}"
|
64
|
+
end
|
65
|
+
end_tags = value.compact.map do |tag|
|
66
|
+
unless tag.match(VALID_TAG_REGEX)
|
67
|
+
raise "Invalid filter tag #{tag.inspect}"
|
68
|
+
end
|
69
|
+
tag.upcase
|
70
|
+
end
|
71
|
+
set_uniq_array_keep_order("filter_tags", end_tags)
|
72
|
+
end
|
73
|
+
|
74
|
+
# @return [Array<String>] the filter tags of this person.
|
75
|
+
def filter_tags
|
76
|
+
doc["filter_tags"] ||= []
|
77
|
+
end
|
78
|
+
|
79
|
+
def as_json
|
80
|
+
super.merge "details" => details&.as_json
|
81
|
+
end
|
82
|
+
|
83
|
+
def as_update(ref = :last, ignore: [])
|
84
|
+
super(ignore: ignore | ["subordinates"])
|
85
|
+
end
|
86
|
+
|
87
|
+
# Sets the PersonDetails to the person, depending on the paramter received:
|
88
|
+
# - `nil`: blanks the details.
|
89
|
+
# - `PersonDetails`: sets a copy of the object param as details.
|
90
|
+
# - `Hash`: sets respectivelly the `schema_id` and the `fields` keys of the Hash to the person details.
|
91
|
+
# @note unique point of access to update the PersonDetails.
|
92
|
+
# @param value [nil, PersonDetails, Hash] value to be set.
|
93
|
+
# @return [nil, PersonDetails] the resulting `PersonDetails` set to the person.
|
94
|
+
def details=(value)
|
95
|
+
case value
|
96
|
+
when NilClass
|
97
|
+
doc["details"] = nil
|
98
|
+
when person_details_class
|
99
|
+
doc["details"] = value.as_json
|
100
|
+
when Hash
|
101
|
+
doc["details"] = value.slice("schema_id", "fields")
|
102
|
+
else
|
103
|
+
raise "Invalid type set on details. Required nil, PersonDetails or Hash; got #{value.class}"
|
104
|
+
end
|
105
|
+
remove_instance_variable("@details") if defined?(@details)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Sets the PersonDetails to the person, depending on the parameter received:
|
109
|
+
# - `PersonSchema`: initializes the `PersonDetails` as per the schema specified (`schema_id` and `fields`).
|
110
|
+
# - `String`: it just sets the `schema_id` on the `PersonDetails` (as `fields` is not include, `details[key]=` will throw error).
|
111
|
+
# (see #details=)
|
112
|
+
# @note
|
113
|
+
# - this method alone only sets the internal structure of the details.
|
114
|
+
# - you will not be able to `reset!` after using this method.
|
115
|
+
# @param schema_or_id [PersonSchema, String, nil, PersonDetails, Hash] value to be set.
|
116
|
+
# @return [nil, PersonDetails] the resulting `PersonDetails` that set to the person.
|
117
|
+
def add_details(schema_or_id)
|
118
|
+
person_details_class.new.tap do |new_details|
|
119
|
+
case schema_or_id
|
120
|
+
when person_schema_class
|
121
|
+
schema_or_id.initialize_details(new_details)
|
122
|
+
when String
|
123
|
+
new_details.schema_id = schema_or_id
|
124
|
+
else
|
125
|
+
raise "Invalid set on details: Requierd PersonSchema or String; got #{schema_or_id.class}"
|
126
|
+
end
|
127
|
+
self.details = new_details
|
128
|
+
# Patch out static data from as_update
|
129
|
+
original_doc["details"] = {
|
130
|
+
"fields" => JSON.parse(doc["details"]["fields"].to_json)
|
131
|
+
}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
@@ -1,82 +1,94 @@
|
|
1
|
-
module Ecoportal
|
2
|
-
module API
|
3
|
-
class V1
|
4
|
-
class PersonDetails < Common::BaseModel
|
5
|
-
class MissingId < StandardError
|
6
|
-
end
|
7
|
-
|
8
|
-
passthrough :schema_id
|
9
|
-
|
10
|
-
class_resolver :schema_field_value_class, "Ecoportal::API::V1::SchemaFieldValue"
|
11
|
-
|
12
|
-
def as_json
|
13
|
-
super.merge "fields" => fields.map(&:as_json)
|
14
|
-
end
|
15
|
-
|
16
|
-
# Sets the `id` of the PersonDetails.
|
17
|
-
# @note unless the new `id` is `nil`, this does not reset the `fields`.
|
18
|
-
# @param value [nil, String] the id of the schema.
|
19
|
-
def schema_id=(value)
|
20
|
-
@fields = [] if value.nil?
|
21
|
-
doc["schema_id"] = value
|
22
|
-
end
|
23
|
-
|
24
|
-
# Gets all the fields of the PersonDetails.
|
25
|
-
# @return [Array<SchemaFieldValue>] the array of fields of the schema.
|
26
|
-
def fields
|
27
|
-
return @fields if defined?(@fields)
|
28
|
-
@fields = (doc["fields"] || []).each_with_index.map do |field, i|
|
29
|
-
schema_field_value_class.new(field, parent: self, key: ["fields", i])
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# Gets one specific field of the PersonDetails.
|
34
|
-
# @param id [String] the `id` or the `alt_id` of the target field.
|
35
|
-
# @return [nil, SchemaFieldValue] the field or `nil` if missing.
|
36
|
-
def get_field(id)
|
37
|
-
@fields_by_id or index_fields
|
38
|
-
@fields_by_id[id] || @fields_by_alt_id[id]
|
39
|
-
end
|
40
|
-
|
41
|
-
# Gets the value of one specific field of the PersonDetails.
|
42
|
-
# @param id [String] the `id` or the `alt_id` of the target field.
|
43
|
-
# @return [String, Array<String>, Boolean, Array<Boolean>, Date, Array<Date>, Numberic, Array<Numeric>] the value of field or `nil` if missing.
|
44
|
-
def [](id)
|
45
|
-
get_field(id)&.value
|
46
|
-
end
|
47
|
-
|
48
|
-
# Sets the value to one specific field of the PersonDetails.
|
49
|
-
# @raise MisssingId if the `id` or `alt_id` is missing.
|
50
|
-
# @param id [String] the `id` or the `alt_id` of the target field.
|
51
|
-
# @return [void]
|
52
|
-
def []=(id, value)
|
53
|
-
if field = get_field(id)
|
54
|
-
field.value = value
|
55
|
-
else
|
56
|
-
raise MissingId.new("details[#{id.inspect}] is missing. Did you forget to load the schema?")
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
# Checks if an `id` or `alt_id` exists
|
61
|
-
# @param id [String] the `id` or the `alt_id` of the target field.
|
62
|
-
# @return [Boolean] `true` if it exists, `false` otherwise
|
63
|
-
def key?(id)
|
64
|
-
@fields_by_id
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
end
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
1
|
+
module Ecoportal
|
2
|
+
module API
|
3
|
+
class V1
|
4
|
+
class PersonDetails < Common::BaseModel
|
5
|
+
class MissingId < StandardError
|
6
|
+
end
|
7
|
+
|
8
|
+
passthrough :schema_id
|
9
|
+
|
10
|
+
class_resolver :schema_field_value_class, "Ecoportal::API::V1::SchemaFieldValue"
|
11
|
+
|
12
|
+
def as_json
|
13
|
+
super.merge "fields" => fields.map(&:as_json)
|
14
|
+
end
|
15
|
+
|
16
|
+
# Sets the `id` of the PersonDetails.
|
17
|
+
# @note unless the new `id` is `nil`, this does not reset the `fields`.
|
18
|
+
# @param value [nil, String] the id of the schema.
|
19
|
+
def schema_id=(value)
|
20
|
+
@fields = [] if value.nil?
|
21
|
+
doc["schema_id"] = value
|
22
|
+
end
|
23
|
+
|
24
|
+
# Gets all the fields of the PersonDetails.
|
25
|
+
# @return [Array<SchemaFieldValue>] the array of fields of the schema.
|
26
|
+
def fields
|
27
|
+
return @fields if defined?(@fields)
|
28
|
+
@fields = (doc["fields"] || []).each_with_index.map do |field, i|
|
29
|
+
schema_field_value_class.new(field, parent: self, key: ["fields", i])
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Gets one specific field of the PersonDetails.
|
34
|
+
# @param id [String] the `id` or the `alt_id` of the target field.
|
35
|
+
# @return [nil, SchemaFieldValue] the field or `nil` if missing.
|
36
|
+
def get_field(id)
|
37
|
+
@fields_by_id or index_fields
|
38
|
+
@fields_by_id[id] || @fields_by_alt_id[id]
|
39
|
+
end
|
40
|
+
|
41
|
+
# Gets the value of one specific field of the PersonDetails.
|
42
|
+
# @param id [String] the `id` or the `alt_id` of the target field.
|
43
|
+
# @return [String, Array<String>, Boolean, Array<Boolean>, Date, Array<Date>, Numberic, Array<Numeric>] the value of field or `nil` if missing.
|
44
|
+
def [](id)
|
45
|
+
get_field(id)&.value
|
46
|
+
end
|
47
|
+
|
48
|
+
# Sets the value to one specific field of the PersonDetails.
|
49
|
+
# @raise MisssingId if the `id` or `alt_id` is missing.
|
50
|
+
# @param id [String] the `id` or the `alt_id` of the target field.
|
51
|
+
# @return [void]
|
52
|
+
def []=(id, value)
|
53
|
+
if field = get_field(id)
|
54
|
+
field.value = value
|
55
|
+
else
|
56
|
+
raise MissingId.new("details[#{id.inspect}] is missing. Did you forget to load the schema?")
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Checks if an `id` or `alt_id` exists
|
61
|
+
# @param id [String] the `id` or the `alt_id` of the target field.
|
62
|
+
# @return [Boolean] `true` if it exists, `false` otherwise
|
63
|
+
def key?(id)
|
64
|
+
@fields_by_id or index_fields
|
65
|
+
@fields_by_id.key?(id) || @fields_by_alt_id.key?(id)
|
66
|
+
end
|
67
|
+
|
68
|
+
# @return [Boolean] `true` if `id` exists and `value` has changed, `false` otherwise
|
69
|
+
def changed?(id, doc = :original)
|
70
|
+
return false unless field = get_field(id)
|
71
|
+
field.as_update.key?("value")
|
72
|
+
end
|
73
|
+
|
74
|
+
def original_value(id)
|
75
|
+
return nil unless field = get_field(id)
|
76
|
+
field.original_doc["value"]
|
77
|
+
end
|
78
|
+
|
79
|
+
protected
|
80
|
+
|
81
|
+
# Rebuilds the internal `id` and `alt_id` references to the fields.
|
82
|
+
def index_fields
|
83
|
+
@fields_by_id = {}
|
84
|
+
@fields_by_alt_id = {}
|
85
|
+
fields.each do |wrapped|
|
86
|
+
@fields_by_id[wrapped.id] = wrapped
|
87
|
+
@fields_by_alt_id[wrapped.alt_id] = wrapped
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
@@ -1,53 +1,53 @@
|
|
1
|
-
module Ecoportal
|
2
|
-
module API
|
3
|
-
class V1
|
4
|
-
class PersonSchema < Common::BaseModel
|
5
|
-
passthrough :id, :name
|
6
|
-
passthrough :enable_tags, :tags
|
7
|
-
|
8
|
-
class_resolver :schema_field_class, "Ecoportal::API::V1::SchemaField"
|
9
|
-
|
10
|
-
def fields
|
11
|
-
@fields_by_id or index_fields
|
12
|
-
@fields_by_id.values
|
13
|
-
end
|
14
|
-
|
15
|
-
def fields_by_id
|
16
|
-
@fields_by_id or index_fields
|
17
|
-
@fields_by_id
|
18
|
-
end
|
19
|
-
|
20
|
-
def fields_by_alt_id
|
21
|
-
@fields_by_alt_id or index_fields
|
22
|
-
@fields_by_alt_id
|
23
|
-
end
|
24
|
-
|
25
|
-
def [](id)
|
26
|
-
@fields_by_alt_id or index_fields
|
27
|
-
@fields_by_id[id] || @fields_by_alt_id[id]
|
28
|
-
end
|
29
|
-
|
30
|
-
def index_fields
|
31
|
-
@fields_by_id = {}
|
32
|
-
@fields_by_alt_id = {}
|
33
|
-
doc["fields"].each do |field|
|
34
|
-
wrapped = schema_field_class.new(field)
|
35
|
-
@fields_by_id[wrapped.id] = wrapped
|
36
|
-
@fields_by_alt_id[wrapped.alt_id] = wrapped
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
def initialize_details(details)
|
41
|
-
details.schema_id = id
|
42
|
-
details.doc["fields"] = fields.map do |field|
|
43
|
-
field.doc.slice(*%w[id alt_id name multiple type shared]).merge(
|
44
|
-
"value" => field.multiple ? [] : nil
|
45
|
-
)
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
require 'ecoportal/api/v1/schema_field'
|
1
|
+
module Ecoportal
|
2
|
+
module API
|
3
|
+
class V1
|
4
|
+
class PersonSchema < Common::BaseModel
|
5
|
+
passthrough :id, :name
|
6
|
+
passthrough :enable_tags, :tags
|
7
|
+
|
8
|
+
class_resolver :schema_field_class, "Ecoportal::API::V1::SchemaField"
|
9
|
+
|
10
|
+
def fields
|
11
|
+
@fields_by_id or index_fields
|
12
|
+
@fields_by_id.values
|
13
|
+
end
|
14
|
+
|
15
|
+
def fields_by_id
|
16
|
+
@fields_by_id or index_fields
|
17
|
+
@fields_by_id
|
18
|
+
end
|
19
|
+
|
20
|
+
def fields_by_alt_id
|
21
|
+
@fields_by_alt_id or index_fields
|
22
|
+
@fields_by_alt_id
|
23
|
+
end
|
24
|
+
|
25
|
+
def [](id)
|
26
|
+
@fields_by_alt_id or index_fields
|
27
|
+
@fields_by_id[id] || @fields_by_alt_id[id]
|
28
|
+
end
|
29
|
+
|
30
|
+
def index_fields
|
31
|
+
@fields_by_id = {}
|
32
|
+
@fields_by_alt_id = {}
|
33
|
+
doc["fields"].each do |field|
|
34
|
+
wrapped = schema_field_class.new(field)
|
35
|
+
@fields_by_id[wrapped.id] = wrapped
|
36
|
+
@fields_by_alt_id[wrapped.alt_id] = wrapped
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def initialize_details(details)
|
41
|
+
details.schema_id = id
|
42
|
+
details.doc["fields"] = fields.map do |field|
|
43
|
+
field.doc.slice(*%w[id alt_id name multiple type shared]).merge(
|
44
|
+
"value" => field.multiple ? [] : nil
|
45
|
+
)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
require 'ecoportal/api/v1/schema_field'
|