Class: DropboxApi::Metadata::Field

Inherits:
Object
  • Object
show all
Defined in:
lib/dropbox_api/metadata/field.rb

Instance Method Summary collapse

Constructor Details

#initialize(type, options = []) ⇒ Field

Returns a new instance of Field.



3
4
5
6
# File 'lib/dropbox_api/metadata/field.rb', line 3

def initialize(type, options = [])
  @type = type
  @options = options
end

Instance Method Details

#cast(object) ⇒ Object



8
9
10
11
12
13
14
15
# File 'lib/dropbox_api/metadata/field.rb', line 8

def cast(object)
  if object.nil?
    raise ArgumentError unless @options.include? :optional
    nil
  else
    force_cast object
  end
end

#force_cast(object) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/dropbox_api/metadata/field.rb', line 17

def force_cast(object)
  if @type == String
    object.to_s
  elsif @type == Time
    Time.parse(object)
  elsif @type == Integer
    object.to_i
  elsif @type == Float
    object.to_f
  elsif @type == Symbol
    object[".tag"].to_sym
  elsif @type == :boolean
    object.to_s == "true"
  elsif @type.ancestors.include? DropboxApi::Metadata::Base
    @type.new(object)
  else
    raise NotImplementedError, "Can't cast `#{@type}`"
  end
end