Sha256: 47da68250dc572a1cfd12e2ac2894672cd12b80eb62825c2e537a5e8ec0e12bb

Contents?: true

Size: 1.33 KB

Versions: 7

Compression:

Stored size: 1.33 KB

Contents

# == Schema Information
#
# Table name: easy_ml_event_contexts
#
#  id         :bigint           not null, primary key
#  event_id   :bigint           not null
#  context    :jsonb            not null
#  format     :string
#  created_at :datetime         not null
#  updated_at :datetime         not null
#
module EasyML
  class EventContext < ActiveRecord::Base
    include EasyML::DataframeSerialization

    self.table_name = "easy_ml_event_contexts"

    belongs_to :event

    validates :context, presence: true
    validates :event, presence: true

    def context=(new_context)
      write_attribute(:context, serialize_context(new_context))
      @context = new_context
    end

    def context
      @context ||= deserialize_context(read_attribute(:context))
    end

    private

    def serialize_context(new_context)
      case new_context
      when Hash
        self.format = :json
        new_context.to_json
      when YAML
        self.format = :yaml
        new_context.to_yaml
      when Polars::DataFrame
        self.format = :dataframe
        serialize_dataframe(new_context)
      end
    end

    def deserialize_context(context)
      case format.to_sym
      when :json
        JSON.parse(context)
      when :yaml
        YAML.safe_load(context)
      when :dataframe
        deserialize_dataframe(context)
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
easy_ml-0.2.0.pre.rc52 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc51 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc50 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc49 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc48 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc47 app/models/easy_ml/event_context.rb
easy_ml-0.2.0.pre.rc46 app/models/easy_ml/event_context.rb