Sha256: 134aba763fa7902a82f529706ab78201fab6c4d2aa55d498f8cf284f19abd2fc

Contents?: true

Size: 897 Bytes

Versions: 5

Compression:

Stored size: 897 Bytes

Contents

require 'active_support'

module Influxer
  module Model
    extend ActiveSupport::Concern
  
    module ClassMethods
      def has_metrics(*args, **params)
        metrics_name = args.empty? ? "metrics" : args.first.to_s

        klass = params[:class_name].present? ? params[:class_name] : "#{self}Metrics" 
        klass = klass.constantize

        attrs = nil

        if params[:inherits].present? 
          attrs = params[:inherits]
        end

        _foreign_key = params.key?(:foreign_key) ? params[:foreign_key] : self.to_s.foreign_key

        define_method(metrics_name) do
          rel_attrs = _foreign_key ? {_foreign_key => self.id} : {}
          
          unless attrs.nil?
            attrs.each do |key|
              rel_attrs[key] = self.send(key)
            end 
          end
          Relation.new klass, attributes: rel_attrs
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
influxer-0.2.2 lib/influxer/model.rb
influxer-0.2.0 lib/influxer/model.rb
influxer-0.1.2 lib/influxer/model.rb
influxer-0.1.1 lib/influxer/model.rb
influxer-0.1.0 lib/influxer/model.rb