Module: AppQuery::Mappable

Extended by:
ActiveSupport::Concern
Defined in:
lib/app_query/mappable.rb

Overview

Maps query results to Ruby objects (e.g., Data classes, Structs).

By default, looks for an Item constant in the query class. Use map_to to specify a different class.

Examples:

With default Item class

class ArticlesQuery < ApplicationQuery
  include AppQuery::Mappable

  class Item < Data.define(:title, :url, :published_on)
  end
end

articles = ArticlesQuery.new.entries
articles.first.title  # => "Hello World"

With explicit map_to

class ArticlesQuery < ApplicationQuery
  include AppQuery::Mappable
  map_to :article

  class Article < Data.define(:title, :url)
  end
end

Skip mapping with raw

articles = ArticlesQuery.new.raw.entries
articles.first  # => {"title" => "Hello", "url" => "..."}

Instance Method Summary collapse

Instance Method Details

#rawObject



43
44
45
46
# File 'lib/app_query/mappable.rb', line 43

def raw
  @raw = true
  self
end

#select_allObject



48
49
50
# File 'lib/app_query/mappable.rb', line 48

def select_all
  map_result(super)
end

#select_oneObject



52
53
54
# File 'lib/app_query/mappable.rb', line 52

def select_one
  map_one(super)
end