Module: Rubyists::Linear::BaseModel::ClassMethods

Included in:
Rubyists::Linear::BaseModel
Defined in:
lib/linear/models/base_model/class_methods.rb

Overview

Class methods for Linear models.

Instance Method Summary collapse

Instance Method Details

#all(after: nil, filter: nil, max: 100) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/linear/models/base_model/class_methods.rb', line 117

def all(after: nil, filter: nil, max: 100)
  edges = []
  moar = true
  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

#all_query(args, subject, base_fragment) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/linear/models/base_model/class_methods.rb', line 77

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

#allq(filter: nil, limit: 50, after: nil) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/linear/models/base_model/class_methods.rb', line 69

def allq(filter: nil, limit: 50, after: nil)
  args = { first: limit }
  args[:filter] = filter ? basic_filter.merge(filter) : basic_filter
  args.delete(:filter) if args[:filter].empty?
  args[:after] = after if after
  all_query args, plural.to_s, base_fragment
end

#base_fragmentObject



93
94
95
# File 'lib/linear/models/base_model/class_methods.rb', line 93

def base_fragment
  const_get(:Base)
end

#basic_filterObject



101
102
103
104
105
# File 'lib/linear/models/base_model/class_methods.rb', line 101

def basic_filter
  return const_get(:BASIC_FILTER) if const_defined?(:BASIC_FILTER)

  {}
end

#const_added(const) ⇒ Object



63
64
65
66
67
# File 'lib/linear/models/base_model/class_methods.rb', line 63

def const_added(const)
  return unless const == :Base

  include MethodMagic
end

#find(id_val) ⇒ Object

Raises:



53
54
55
56
57
58
59
60
61
# File 'lib/linear/models/base_model/class_methods.rb', line 53

def find(id_val)
  camel_name = just_name.camelize :lower
  sym = camel_name.to_sym
  ff = full_fragment
  query_data = Api.query(query { __node(camel_name, id: id_val) { ___ ff } })
  raise NotFoundError, "No #{just_name} found with id #{id_val}" if query_data.nil? || query_data[sym].nil?

  new query_data[sym]
end

#full_fragmentObject



97
98
99
# File 'lib/linear/models/base_model/class_methods.rb', line 97

def full_fragment
  base_fragment
end

#getter!(relation) ⇒ Object



16
17
18
19
20
21
22
23
24
# File 'lib/linear/models/base_model/class_methods.rb', line 16

def getter!(relation)
  define_method relation do
    return instance_variable_get("@#{relation}") if instance_variable_defined?("@#{relation}")

    return unless (val = updated_data[relation])

    send("#{relation}=", val)
  end
end

#gql_query(filter: nil, after: nil) ⇒ Object



113
114
115
# File 'lib/linear/models/base_model/class_methods.rb', line 113

def gql_query(filter: nil, after: nil)
  Api.query(allq(filter:, after:))
end

#just_nameObject



89
90
91
# File 'lib/linear/models/base_model/class_methods.rb', line 89

def just_name
  name.split('::').last
end

#many_setter!(relation, klass) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/linear/models/base_model/class_methods.rb', line 34

def many_setter!(relation, klass)
  define_method "#{relation}=" do |val|
    vals = if val&.key?(:nodes)
             val[:nodes]
           else
             Array(val)
           end
    updated_data[relation] = vals.map { |v| v.is_a?(Hash) ? v : v.updated_data }
    new_relations = vals.map { |v| v.is_a?(Hash) ? Rubyists::Linear.const_get(klass).new(v) : v }
    instance_variable_set("@#{relation}", new_relations)
  end
end

#many_to_one(relation, klass = nil) ⇒ Object Also known as: one_to_one



26
27
28
29
30
# File 'lib/linear/models/base_model/class_methods.rb', line 26

def many_to_one(relation, klass = nil)
  klass ||= relation.to_s.camelize.to_sym
  getter! relation
  setter! relation, klass
end

#one_to_many(relation, klass = nil) ⇒ Object



47
48
49
50
51
# File 'lib/linear/models/base_model/class_methods.rb', line 47

def one_to_many(relation, klass = nil)
  klass ||= relation.to_s.singularize.camelize.to_sym
  getter! relation
  many_setter! relation, klass
end

#pluralObject



107
108
109
110
111
# File 'lib/linear/models/base_model/class_methods.rb', line 107

def plural
  return const_get(:PLURAL) if const_defined?(:PLURAL)

  just_name.downcase.pluralize.to_sym
end

#setter!(relation, klass) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/linear/models/base_model/class_methods.rb', line 8

def setter!(relation, klass)
  define_method "#{relation}=" do |val|
    hash = val.is_a?(Hash) ? val : val.updated_data
    updated_data[relation] = hash
    instance_variable_set("@#{relation}", Rubyists::Linear.const_get(klass).new(hash))
  end
end