Sha256: 84bc6d9c5bcb1aeda7541a5ce7f5a954a4eb3a1742d5b1b39b9184c9975fb44e

Contents?: true

Size: 1.47 KB

Versions: 1

Compression:

Stored size: 1.47 KB

Contents

module RelationToStruct::ActiveRecordBaseExtension
  extend ::ActiveSupport::Concern

  module ClassMethods
    def structs_from_sql(struct_class, sql, binds=[])
      result = connection.select_all(sanitize_sql(sql, nil), "Structs SQL Load", binds)

      if result.columns.size != result.columns.uniq.size
        raise ArgumentError, 'Expected column names to be unique'
      end

      if result.columns != struct_class.members.collect(&:to_s)
        raise ArgumentError, 'Expected column names (and their order) to match struct attribute names'
      end

      if result.columns.size == 1
        result.cast_values().map do |tuple|
          struct_class.new(tuple)
        end
      else
        result.cast_values().map do |tuple|
          struct_class.new(*tuple)
        end
      end
    end

    def pluck_from_sql(sql, binds=[])
      result = connection.select_all(sanitize_sql(sql, nil), "Pluck SQL Load", binds)
      result.cast_values()
    end

    def value_from_sql(sql, binds=[])
      result = connection.select_all(sanitize_sql(sql, nil), "Value SQL Load", binds)
      raise ArgumentError, 'Expected exactly one column to be selected' unless result.columns.size == 1

      values = result.cast_values()
      case values.size
      when 0
        nil
      when 1
        values[0]
      else
        raise ArgumentError, 'Expected only a single result to be returned'
      end
    end
  end
end

::ActiveRecord::Base.send(:include, RelationToStruct::ActiveRecordBaseExtension)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
relation_to_struct-1.1.0 lib/relation_to_struct/active_record_base_extension.rb