Module: Flydata::Heroku::InstanceMethods

Defined in:
lib/flydata/heroku/instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#as_flydataHash

Returns:

  • (Hash)

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
31
32
33
34
35
# File 'lib/flydata/heroku/instance_methods.rb', line 5

def as_flydata
  # Logic borrowed from activemodel/lib/active_model/serialization.rb
  options = { :root => nil }
  attribute_names = self.class._flydata_attributes || attributes.keys.sort
  hash = {}
  attribute_names.each do |n|
    value = read_attribute_for_serialization(n)
    hash[n] = case value
              when Time
                Flydata.format_time_for_redshift(value)
              when DateTime
                Flydata.format_time_for_redshift(value)
              when Date
                Flydata.format_date_for_redshift(value)
              else
                value
              end
  end
  method_names = Array.wrap(options[:methods]).select { |n| respond_to?(n) }
  method_names.each { |n| hash[n] = send(n) }

  serializable_add_includes(options) do |association, records, opts|
    hash[association] = if records.is_a?(Enumerable)
                          records.map { |a| a.serializable_hash(opts) }
                        else
                          records.serializable_hash(opts)
                        end
  end

  hash
end

#send_to_flydataObject


37
38
39
# File 'lib/flydata/heroku/instance_methods.rb', line 37

def send_to_flydata
  $stdout.puts({ self.class.flydata_table_name => as_flydata }.to_json)
end