Module: DataMapper::Persevere::JSONSupport::Resource

Defined in:
lib/persevere_adapter/json_support/resource.rb

Instance Method Summary collapse

Instance Method Details

#to_json_hashObject

Convert a DataMapper Resource to a JSON.

Parameters:

  • query (Query)

    The DataMapper query object passed in



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/persevere_adapter/json_support/resource.rb', line 13

def to_json_hash
  json_rsrc = Hash.new
  

  attributes(:property).each do |property, value|
    next if value.nil? #|| (value.is_a?(Array) && value.empty?) || relations.include?(property.name.to_s)

    json_rsrc[property.field] = case value
      when DateTime then value.new_offset(0).strftime("%Y-%m-%dT%H:%M:%SZ")
      when Date then value.to_s
      when Time then value.strftime("%H:%M:%S")
      when Float then value.to_f
      when BigDecimal then value.to_f
      when Integer then value.to_i
      else # when String, TrueClass, FalseClass then
        self[property.name]
    end
  end

  json_rsrc
end