Sha256: 966b44e71a3ff143e4a4eea6acf185ad73b2fab267a46bfc509dfc59e68218d0

Contents?: true

Size: 1.42 KB

Versions: 149

Compression:

Stored size: 1.42 KB

Contents

module Flydata
  module Heroku
    module InstanceMethods
      # @return [Hash]
      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

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

Version data entries

149 entries across 149 versions & 1 rubygems

Version Path
flydata-0.8.10.2 lib/flydata/heroku/instance_methods.rb
flydata-0.8.10.1 lib/flydata/heroku/instance_methods.rb
flydata-0.8.9.11 lib/flydata/heroku/instance_methods.rb
flydata-0.8.10 lib/flydata/heroku/instance_methods.rb
flydata-0.8.9 lib/flydata/heroku/instance_methods.rb
flydata-0.8.8 lib/flydata/heroku/instance_methods.rb
flydata-0.8.7 lib/flydata/heroku/instance_methods.rb
flydata-0.8.6 lib/flydata/heroku/instance_methods.rb
flydata-0.8.5 lib/flydata/heroku/instance_methods.rb
flydata-0.8.4 lib/flydata/heroku/instance_methods.rb
flydata-0.8.3 lib/flydata/heroku/instance_methods.rb
flydata-0.8.2 lib/flydata/heroku/instance_methods.rb
flydata-0.8.1 lib/flydata/heroku/instance_methods.rb
flydata-0.8.0 lib/flydata/heroku/instance_methods.rb
flydata-0.7.19 lib/flydata/heroku/instance_methods.rb
flydata-0.7.18 lib/flydata/heroku/instance_methods.rb
flydata-0.7.17 lib/flydata/heroku/instance_methods.rb
flydata-0.7.16 lib/flydata/heroku/instance_methods.rb
flydata-0.7.15 lib/flydata/heroku/instance_methods.rb
flydata-0.7.14 lib/flydata/heroku/instance_methods.rb