Class: WCC::Contentful::IndexedRepresentation::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/indexed_representation.rb

Constant Summary collapse

ATTRIBUTES =
%i[
  name
  type
  array
  required
  link_types
].freeze
TYPES =
%i[
  String
  Int
  Float
  DateTime
  Boolean
  Json
  Coordinates
  RichText
  Link
  Asset
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(hash_or_id = nil) ⇒ Field

Returns a new instance of Field.



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/wcc/contentful/indexed_representation.rb', line 117

def initialize(hash_or_id = nil)
  return unless hash_or_id

  if hash_or_id.is_a?(String)
    @name = hash_or_id
    return
  end

  unless hash_or_id.is_a?(Hash)
    ATTRIBUTES.each { |att| public_send("#{att}=", hash_or_id.public_send(att)) }
    return
  end

  if raw_type = hash_or_id.delete('type')
    raw_type = raw_type.to_sym
    raise ArgumentError, "Unknown type #{raw_type}, expected one of: #{TYPES}" unless TYPES.include?(raw_type)

    @type = raw_type
  end

  hash_or_id.each { |k, v| public_send("#{k}=", v) }
end

Instance Method Details

#==(other) ⇒ Object



140
141
142
# File 'lib/wcc/contentful/indexed_representation.rb', line 140

def ==(other)
  ATTRIBUTES.all? { |att| public_send(att) == other.public_send(att) }
end

#type=(raw_type) ⇒ Object

Raises:

  • (ArgumentError)


111
112
113
114
115
# File 'lib/wcc/contentful/indexed_representation.rb', line 111

def type=(raw_type)
  raise ArgumentError, "Unknown type #{raw_type}, expected one of: #{TYPES}" unless TYPES.include?(raw_type)

  @type = raw_type
end