Sha256: b03d1333ac00ebfebaaed02ee58908937ce1b49cc9b7058b9fc5690fdaa96e21

Contents?: true

Size: 730 Bytes

Versions: 10

Compression:

Stored size: 730 Bytes

Contents

class SqlQuery
  attr_reader :opts
  attr_reader :sql
  attr_reader :record

  def initialize(**opts)
    @opts   = opts
    @sql    = self.class.sql
    @record = self.class.record
  end

  def query(sql_query)
    result = connection.execute(sql_query)
    result.map { |row| record.new(**row) }
  end

  class << self
    attr_reader :sql
    attr_reader :record

    def query(**opts)
      new(**opts).call
    end

    def sql_resource(filename)
      sql_file = File.expand_path(filename, File.dirname(__FILE__))
      @sql = File.read(sql_file)
    end

    def fields(fields)
      @record = Struct.new(*fields, keyword_init: true)
    end
  end

  protected

  def connection
    ActiveRecord::Base.connection
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
rails_app_generator-0.3.6 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.5 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.4 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.3 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.2 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.1 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.3.0 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.2.43 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.2.42 after_templates/application/klueless/app/queries/sql_query.rb
rails_app_generator-0.2.41 after_templates/application/klueless/app/queries/sql_query.rb