Class: DropboxApi::Metadata::Field
- Inherits:
-
Object
- Object
- DropboxApi::Metadata::Field
- Defined in:
- lib/dropbox_api/metadata/field.rb
Instance Method Summary collapse
- #cast(object) ⇒ Object
- #force_cast(object) ⇒ Object
-
#initialize(type, options = []) ⇒ Field
constructor
A new instance of Field.
Constructor Details
#initialize(type, options = []) ⇒ Field
Returns a new instance of Field.
4 5 6 7 |
# File 'lib/dropbox_api/metadata/field.rb', line 4 def initialize(type, = []) @type = type @options = end |
Instance Method Details
#cast(object) ⇒ Object
9 10 11 12 13 14 15 16 |
# File 'lib/dropbox_api/metadata/field.rb', line 9 def cast(object) if object.nil? raise ArgumentError unless @options.include? :optional nil else force_cast object end end |
#force_cast(object) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dropbox_api/metadata/field.rb', line 18 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.ancestors.include?(Array) || @type == DropboxApi::Metadata::Resource @type.new(object) else raise NotImplementedError, "Can't cast #{object} to `#{@type}`" end end |