Class: AirtableBaseBuilder::Airtable::Field

Inherits:
Client
  • Object
show all
Includes:
Thor::Shell
Defined in:
lib/airtable_base_builder/airtable/field.rb

Constant Summary collapse

FIELD_TYPES =
%w[
  singleLineText email url multilineText number percent currency singleSelect multipleSelects
  singleCollaborator multipleCollaborators date dateTime phoneNumber
  multipleAttachments checkbox barcode rating richText duration
].freeze

Instance Method Summary collapse

Methods inherited from Client

#post

Constructor Details

#initialize(data) ⇒ Field

Returns a new instance of Field.



16
17
18
# File 'lib/airtable_base_builder/airtable/field.rb', line 16

def initialize(data)
  @data = data
end

Instance Method Details

#buildObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/airtable_base_builder/airtable/field.rb', line 20

def build
  validate_field_type

  field = {
    name: @data[:name],
    type: @data[:type],
  }

  field[:options] = options if options

  field
end

#optionsObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/airtable_base_builder/airtable/field.rb', line 33

def options
  case @data[:type]
  when 'singleSelect', 'multipleSelects'
    { choices: [{ name: 'Option 1' }, { name: 'Option 2' }] }
  when 'number', 'percent'
    { precision: 0 }
  when 'currency'
    { precision: 0, symbol: '$' }
  when 'date'
    { dateFormat: { name: "local" } }
  when 'dateTime'
    { timeZone: "utc", dateFormat: { name: "local" }, timeFormat: { name: "12hour" } }
  # when 'multipleAttachments'
  #   { isReversed: true }
  when 'checkbox'
    { color: "greenBright", icon: "check" }
  when 'rating'
    { color: "yellowBright", icon: "star", max: 5 }
  when 'duration'
    { durationFormat: "h:mm" }
  else
    nil
  end
end

#validate_field_typeObject



58
59
60
61
62
63
64
65
66
# File 'lib/airtable_base_builder/airtable/field.rb', line 58

def validate_field_type
  return if FIELD_TYPES.include?(@data[:type])

  valid_field_types = FIELD_TYPES.join("\n  - ")
  say_error("Invalid field type provided: #{@data[:type]}", color = :red)
  say("Valid field types are:\n  - #{valid_field_types}", color = :green)

  exit
end