Class: Ecoportal::API::V1::SchemaFieldValue

Inherits:
Common::BaseModel show all
Defined in:
lib/ecoportal/api/v1/schema_field_value.rb

Direct Known Subclasses

Internal::SchemaFieldValue

Instance Attribute Summary

Attributes inherited from Common::BaseModel

#_key, #_parent

Instance Method Summary collapse

Methods inherited from Common::BaseModel

#as_json, #as_update, #consolidate!, #dirty?, #doc, embeds_one, #initial_doc, #initialize, #original_doc, passthrough, #print_pretty, #reset!, #to_json

Methods included from Common::BaseClass

#class_resolver, #resolve_class

Constructor Details

This class inherits a constructor from Ecoportal::API::Common::BaseModel

Instance Method Details

#maybe_multiple(value) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/ecoportal/api/v1/schema_field_value.rb', line 52

def maybe_multiple(value)
  if multiple
    unless value.is_a?(Array)
      raise "Expected Array, got #{value.class}"
    end
    value.map {|v| yield v }
  else
    yield value
  end
end

#valueObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ecoportal/api/v1/schema_field_value.rb', line 8

def value
  case type
  when "text", "phone_number", "number", "boolean", "select"
   doc["value"]
  when "date"
   if doc["value"]
     maybe_multiple(doc["value"]) do |v|
       Date.iso8601(v)
     end
   end
  else
   raise "Unknown type #{type}"
  end
end

#value=(value) ⇒ Object



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
# File 'lib/ecoportal/api/v1/schema_field_value.rb', line 23

def value=(value)
  case type
  when "text", "phone_number", "select"
    doc["value"] = maybe_multiple(value) do |v|
      v&.to_s
    end
  when "number"
    maybe_multiple(value) do |v|
      unless v.nil? || v.is_a?(Numeric)
        raise "Invalid number type #{v.class}"
      end
    end
    doc["value"] = value
  when "boolean"
    doc["value"] = !!value
  when "date"
    maybe_multiple(value) do |v|
      unless v.nil? || v.respond_to?(:to_date)
        raise "Invalid date type #{v.class}"
      end
    end
    doc["value"] = maybe_multiple(value) do |v|
      v&.to_date&.to_s
    end
  else
    raise "Unknown type #{type}"
  end
end