-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a base model for shared behavior
- Loading branch information
Showing
3 changed files
with
75 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'gqli' | ||
require 'semantic_logger' | ||
|
||
module Rubyists | ||
module Linear | ||
# Module which provides a base model for Linear models. | ||
class BaseModel | ||
extend GQLi::DSL | ||
include GQLi::DSL | ||
include SemanticLogger::Loggable | ||
|
||
def self.included(klass) | ||
klass.extend ClassMethods | ||
end | ||
|
||
# Class methods for Linear models. | ||
module ClassMethods | ||
def allq(filter: nil, limit: 50, after: nil) # rubocop:disable Metrics/MethodLength | ||
args = { first: limit } | ||
args[:filter] = filter ? BASIC_FILTER.merge(filter) : BASIC_FILTER | ||
args[:after] = after if after | ||
query do | ||
subject(args) do | ||
edges do | ||
node { ___ Base } | ||
cursor | ||
end | ||
___ Fragments::PageInfo | ||
end | ||
end | ||
end | ||
|
||
def gql_query(filter: nil, after: nil) | ||
Api.query(allq(filter:, after:)) | ||
end | ||
|
||
def all(edges: [], moar: true, after: nil, filter: nil, max: 100) | ||
while moar | ||
data = gql_query(filter:, after:) | ||
subjects = data[PLURAL] | ||
edges += subjects[:edges] | ||
moar = false if edges.size >= max || !subjects[:pageInfo][:hasNextPage] | ||
after = subjects[:pageInfo][:endCursor] | ||
end | ||
edges.map { |edge| new edge[:node] } | ||
end | ||
end | ||
|
||
attr_reader :data | ||
|
||
def initialize(data) | ||
@data = data | ||
end | ||
|
||
def to_json(*_args) | ||
data.to_json | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters