Class: JsonApiClient::Schema::Property

Inherits:
Struct
  • Object
show all
Defined in:
lib/json_api_client/schema.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#defaultObject

Returns the value of attribute default

Returns:

  • (Object)

    the current value of default



3
4
5
# File 'lib/json_api_client/schema.rb', line 3

def default
  @default
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



3
4
5
# File 'lib/json_api_client/schema.rb', line 3

def name
  @name
end

#typeObject

Returns the value of attribute type

Returns:

  • (Object)

    the current value of type



3
4
5
# File 'lib/json_api_client/schema.rb', line 3

def type
  @type
end

Instance Method Details

#cast(value) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/json_api_client/schema.rb', line 4

def cast(value)
  return nil if value.nil?
  return value if type.nil?

  case type.to_sym
  when :int, :integer
    value.to_i
  when :string
    value.to_s
  when :float
    value.to_f
  when :time
    value.is_a?(Time) || nil ? value : Time.parse(value)
  when :boolean
    case value
    when "false", "0", 0, false
      false
    when "true", "1", 1, true
      true
    else
      # if it's unknown, use the default value
      default
    end
  else
    value
  end
end