Sha256: 9d7f3242cd674370345f639c507f2fbc71e1100d30b3bf02413e9ca6994bbb1c

Contents?: true

Size: 995 Bytes

Versions: 3

Compression:

Stored size: 995 Bytes

Contents

class Spree::Report::Observation

  def initialize
    set_defaults
  end

  class << self
    def observation_fields(records)
      case records
      when Hash
        build_from_hash(records)
      else
        build_from_list(records)
      end
    end

    def build_from_hash(records)
      build_from_list(records.keys)

      define_method :set_defaults do
        records.keys.each do |key|
          self.send("#{ key }=", records[key])
        end
      end
    end

    def build_from_list(records)
      attr_accessor *records

      define_method :populate do |result|
        records.each do |record|
          record_name = record.to_s
          self.send("#{ record }=", result[record_name]) if result[record_name]
        end
      end

      define_method :set_defaults do
      end

      define_method :observations_to_h do
        records.inject({}) { |acc, record| acc[record] = self.send(record); acc }
      end
    end
  end

  def to_h
    observations_to_h
  end
end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
spree_admin_insights-3.2.0 app/reports/spree/report/observation.rb
solidus_admin_insights-2.1.1 app/reports/spree/report/observation.rb
solidus_admin_insights-2.1.0 app/reports/spree/report/observation.rb