Skip to content

Commit

Permalink
Base model abstracting the all query
Browse files Browse the repository at this point in the history
  • Loading branch information
bougyman committed Jan 27, 2024
1 parent c5bc5a0 commit 77c1557
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
linear-cli (0.1.8)
linear-cli (0.1.9)
base64
dry-cli (~> 1.0)
dry-cli-completion (~> 1.0)
Expand Down
6 changes: 4 additions & 2 deletions exe/lcls
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
#!/usr/bin/env bash
exec lc i ls "$@"
#!/usr/bin/env ruby
# frozen_string_literal: true

exec File.join(__dir__, 'lcls.sh'), *ARGV
2 changes: 2 additions & 0 deletions exe/lcls.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
exec lc i ls "$@"
32 changes: 22 additions & 10 deletions lib/linear/models/base_model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,47 @@ class BaseModel
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
class << self
def allq(filter: nil, limit: 50, after: nil)
args = { first: limit }
args[:filter] = filter ? BASIC_FILTER.merge(filter) : BASIC_FILTER
args[:filter] = filter ? basic_filter.merge(filter) : basic_filter
args[:after] = after if after
all_query args, plural.to_s, base_fragment
end

def all_query(args, subject, base_fragment)
query do
subject(args) do
__node(subject, args) do
edges do
node { ___ Base }
node { ___ base_fragment }
cursor
end
___ Fragments::PageInfo
end
end
end

def base_fragment
const_get(:Base)
end

def basic_filter
const_get(:BASIC_FILTER)
end

def plural
const_get(:PLURAL)
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]
subjects = data[plural]
edges += subjects[:edges]
moar = false if edges.size >= max || !subjects[:pageInfo][:hasNextPage]
after = subjects[:pageInfo][:endCursor]
Expand Down
2 changes: 1 addition & 1 deletion lib/linear/models/issue.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Issue

def display
format = "%-10s %s (%s)\n"
printf format, data[:identifier], data[:title], data[:assignee][:name]
printf format, data[:identifier], data[:title], data.dig(:assignee, :name)
end
end
end
Expand Down

0 comments on commit 77c1557

Please sign in to comment.