Function bodies 314 total
registered_filter_object function · ruby · L46-L52 (7 LOC)lib/pack_api/querying/filter_factory.rb
def registered_filter_object(filter_name, options)
options.is_a?(Hash) ?
filter_classes[filter_name].new(**options) :
filter_classes[filter_name].new(options)
rescue ArgumentError => e
raise ArgumentError, "Invalid filter options for #{filter_name}: #{options} (#{e})"
endSortHash class · ruby · L4-L26 (23 LOC)lib/pack_api/querying/sort_hash.rb
class SortHash < Hash
###
# Normalize a hash object to be used to control the sorting of a collection of objects
#
# @param [Hash|Symbol|String] sort_arg When provided with a string or a symbol, the sort hash will treat that
# as the name of the attribute to sort by, and will sort in ascending order. When provided with a hash, the keys
# of the hash should be the names of the attributes to sort by, and the values should be either `:asc` or `:desc`
def initialize(sort_arg)
super()
case sort_arg
when Arel::Nodes::SqlLiteral, nil
hash_entries = []
when Hash
hash_entries = sort_arg.to_a
when Symbol
hash_entries = [[sort_arg, :asc]]
when String
hash_entries = sort_arg.split(',').map do |sort_term|
sort_term_parts = sort_term.split
[sort_term_parts[0].to_sym, sort_term_parts[1]&.to_sym || :asc]
end
endinitialize method · ruby · L12-L25 (14 LOC)lib/pack_api/querying/sort_hash.rb
def initialize(sort_arg)
super()
case sort_arg
when Arel::Nodes::SqlLiteral, nil
hash_entries = []
when Hash
hash_entries = sort_arg.to_a
when Symbol
hash_entries = [[sort_arg, :asc]]
when String
hash_entries = sort_arg.split(',').map do |sort_term|
sort_term_parts = sort_term.split
[sort_term_parts[0].to_sym, sort_term_parts[1]&.to_sym || :asc]
endAggregateType class · ruby · L45-L94 (50 LOC)lib/pack_api/types/aggregate_type.rb
class AggregateType < BaseType
@attribute_sources = {}
@attribute_sources = {}
@next_attribute_list = nil
class << self
def inherited(subclass)
subclass.instance_variable_set(:@attribute_sources, {})
subclass.instance_variable_set(:@next_attribute_list, nil)
super
end
def attribute(name, type = Undefined, &block)
super
@next_attribute_list << name if @next_attribute_list
end
def combine_attributes(from:, &block)
@next_attribute_list = []
block.call
@attribute_sources[from] = @next_attribute_list
@next_attribute_list = nil
end
def query(**params)
attribute_blender.query(**params).map { new(it) }
end
def get(id)
new(attribute_blender.get(id))
end
def update(id, params)
new(attribute_blender.update(id, params))
end
def create(params)
new(attribute_blender.create(params))
end
inherited method · ruby · L51-L55 (5 LOC)lib/pack_api/types/aggregate_type.rb
def inherited(subclass)
subclass.instance_variable_set(:@attribute_sources, {})
subclass.instance_variable_set(:@next_attribute_list, nil)
super
endattribute method · ruby · L57-L60 (4 LOC)lib/pack_api/types/aggregate_type.rb
def attribute(name, type = Undefined, &block)
super
@next_attribute_list << name if @next_attribute_list
endcombine_attributes method · ruby · L62-L67 (6 LOC)lib/pack_api/types/aggregate_type.rb
def combine_attributes(from:, &block)
@next_attribute_list = []
block.call
@attribute_sources[from] = @next_attribute_list
@next_attribute_list = nil
endWant fix-PRs on findings? Install Repobility's GitHub App · github.com/apps/repobility-bot
query method · ruby · L69-L71 (3 LOC)lib/pack_api/types/aggregate_type.rb
def query(**params)
attribute_blender.query(**params).map { new(it) }
endget method · ruby · L73-L75 (3 LOC)lib/pack_api/types/aggregate_type.rb
def get(id)
new(attribute_blender.get(id))
endupdate method · ruby · L77-L79 (3 LOC)lib/pack_api/types/aggregate_type.rb
def update(id, params)
new(attribute_blender.update(id, params))
endcreate method · ruby · L81-L83 (3 LOC)lib/pack_api/types/aggregate_type.rb
def create(params)
new(attribute_blender.create(params))
enddelete method · ruby · L85-L87 (3 LOC)lib/pack_api/types/aggregate_type.rb
def delete(id)
attribute_blender.delete(id)
endattribute_blender method · ruby · L91-L93 (3 LOC)lib/pack_api/types/aggregate_type.rb
def attribute_blender
@attribute_blender ||= AttributeBlender.new(@attribute_sources, self)
endAttributeBlender class · ruby · L96-L119 (24 LOC)lib/pack_api/types/aggregate_type.rb
class AttributeBlender
attr_reader :attribute_sources, :combined_attributes, :aggregate_type
def initialize(attribute_sources, aggregate_type)
@attribute_sources = attribute_sources
@aggregate_type = aggregate_type
@combined_attributes = {}
end
def query(**params)
primary_source, primary_resource_attributes = attribute_sources.first
resource_method = :"query_#{primary_source}s"
primary_resource_results = aggregate_type.send(resource_method, **params)
combined_attributes = primary_resource_results.transform_values do |primary_resource|
primary_resource.to_h.slice(*primary_resource_attributes)
end
primary_result_ids = primary_resource_results.keys
attribute_sources.drop(1).each do |resource_name, resource_attributes|
resource_method = :"query_#{resource_name}s"
aggregate_type.send(resource_method, id: primary_result_ids)
.eachinitialize method · ruby · L99-L103 (5 LOC)lib/pack_api/types/aggregate_type.rb
def initialize(attribute_sources, aggregate_type)
@attribute_sources = attribute_sources
@aggregate_type = aggregate_type
@combined_attributes = {}
endSource: Repobility analyzer · https://repobility.com
query method · ruby · L105-L111 (7 LOC)lib/pack_api/types/aggregate_type.rb
def query(**params)
primary_source, primary_resource_attributes = attribute_sources.first
resource_method = :"query_#{primary_source}s"
primary_resource_results = aggregate_type.send(resource_method, **params)
combined_attributes = primary_resource_results.transform_values do |primary_resource|
primary_resource.to_h.slice(*primary_resource_attributes)
endget function · ruby · L125-L130 (6 LOC)lib/pack_api/types/aggregate_type.rb
def get(id)
attribute_sources.each do |resource_name, attribute_names|
resource_method = :"get_#{resource_name}"
resource = aggregate_type.send(resource_method, id)
combined_attributes.merge!(resource.to_h.slice(*attribute_names))
endupdate function · ruby · L134-L145 (12 LOC)lib/pack_api/types/aggregate_type.rb
def update(id, params)
initial_state = get(id)
@combined_attributes = initial_state.to_h
attribute_sources_updated = []
attribute_sources.each do |resource_name, resource_attributes|
resource_params = resource_params(params, resource_attributes)
next unless resource_params.any?
resource_method = :"update_#{resource_name}"
resource = aggregate_type.send(resource_method, id, resource_params)
combined_attributes.merge!(resource.to_h.slice(*resource_attributes))
endcreate function · ruby · L152-L162 (11 LOC)lib/pack_api/types/aggregate_type.rb
def create(params)
attribute_sources_created = []
primary_resource_id = nil
attribute_sources.each do |resource_name, resource_attributes|
resource_method = :"create_#{resource_name}"
args = [resource_params(params, resource_attributes)]
args << primary_resource_id if primary_resource_id
resource = aggregate_type.send(resource_method, *args)
primary_resource_id ||= resource.id
combined_attributes.merge!(resource.to_h.slice(*resource_attributes))
enddelete function · ruby · L169-L176 (8 LOC)lib/pack_api/types/aggregate_type.rb
def delete(id)
attribute_sources.each_key do |resource_name|
resource_method = :"delete_#{resource_name}"
aggregate_type.send(resource_method, id)
rescue PackAPI::InternalError => e
Rails.logger.error("Failed to delete #{resource_name} with id: #{id}")
Rails.logger.error(e.backtrace.join("\n"))
endresource_params function · ruby · L181-L184 (4 LOC)lib/pack_api/types/aggregate_type.rb
def resource_params(params, resource_attributes)
normalizer = PackAPI::Mapping::NormalizedAPIAttribute.new(resource_attributes)
params.filter { |key| resource_attributes.include?(normalizer.normalize(key)) }
endrollback_update function · ruby · L186-L191 (6 LOC)lib/pack_api/types/aggregate_type.rb
def rollback_update(attribute_sources_updated, id, initial_state)
attribute_sources_updated.reverse_each do |resource_name|
resource_method = :"update_#{resource_name}"
attribute_names = attribute_sources[resource_name]
aggregate_type.send(resource_method, id, initial_state.to_h.slice(*attribute_names))
endrollback_create function · ruby · L194-L198 (5 LOC)lib/pack_api/types/aggregate_type.rb
def rollback_create(attribute_sources_created, primary_resource_id)
attribute_sources_created.reverse_each do |resource_name|
resource_method = :"delete_#{resource_name}"
aggregate_type.send(resource_method, primary_resource_id)
endRepobility — the code-quality scanner for AI-generated software · https://repobility.com
BaseType class · ruby · L4-L43 (40 LOC)lib/pack_api/types/base_type.rb
class BaseType < Dry::Struct
@optional_attributes = []
def self.inherited(subclass)
subclass.instance_variable_set(:@optional_attributes, @optional_attributes.dup)
super
end
def self.optional_attribute(name, type = Undefined, &block)
attribute?(name, type.optional, &block)
@optional_attributes << name
end
def self.optional_attributes
@optional_attributes.to_a
end
def to_data(**other_attributes)
merged_attributes = to_h.merge(other_attributes)
Data.define(*merged_attributes.keys).new(**merged_attributes)
end
def merge(**other_attributes)
self.class.new(to_h.merge(other_attributes))
end
##
# This method returns a mapping of attribute names to `Dry::Type::*` objects that describe the attributes that have
# been designated as filterable, as in the following example:
# attribute :id, Types::String.meta(filterable: true)
#
# NOTE: According to the documentation, `Dry:inherited method · ruby · L7-L10 (4 LOC)lib/pack_api/types/base_type.rb
def self.inherited(subclass)
subclass.instance_variable_set(:@optional_attributes, @optional_attributes.dup)
super
endoptional_attribute method · ruby · L12-L15 (4 LOC)lib/pack_api/types/base_type.rb
def self.optional_attribute(name, type = Undefined, &block)
attribute?(name, type.optional, &block)
@optional_attributes << name
endoptional_attributes method · ruby · L17-L19 (3 LOC)lib/pack_api/types/base_type.rb
def self.optional_attributes
@optional_attributes.to_a
endto_data method · ruby · L21-L24 (4 LOC)lib/pack_api/types/base_type.rb
def to_data(**other_attributes)
merged_attributes = to_h.merge(other_attributes)
Data.define(*merged_attributes.keys).new(**merged_attributes)
endmerge method · ruby · L26-L28 (3 LOC)lib/pack_api/types/base_type.rb
def merge(**other_attributes)
self.class.new(to_h.merge(other_attributes))
endfilterable_attributes method · ruby · L39-L42 (4 LOC)lib/pack_api/types/base_type.rb
def self.filterable_attributes
schema.keys.each_with_object({}) do |schema_key, result|
result[schema_key.name] = schema_key.type if schema_key.meta[:filterable]
endBooleanFilterDefinition class · ruby · L4-L8 (5 LOC)lib/pack_api/types/boolean_filter_definition.rb
class BooleanFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:tri_state_boolean)
attribute :options, Types::Array.of(FilterOption)
endRepobility · code-quality intelligence · https://repobility.com
CollectionResultMetadata class · ruby · L4-L54 (51 LOC)lib/pack_api/types/collection_result_metadata.rb
class CollectionResultMetadata < Dry::Struct
attribute :first_item, Types::Integer
attribute :last_item, Types::Integer
attribute :total_items, Types::Integer
# the page size (input) -- can be different than actual number of items in the result for 2 reasons:
# - single page scenario: per_page = :all (symbol)
# - last page scenario: per_page = N (integer), but there are fewer than N items remaining in the result set
attribute :per_page, Types::Integer | Types::Symbol
###
# Cursors for the current record set (see PackAPI::Paginator)
attribute? :next_page_cursor, Types::String.optional
attribute? :previous_page_cursor, Types::String.optional
attribute? :first_page_cursor, Types::String.optional
attribute? :last_page_cursor, Types::String.optional
attribute? :current_page_cursor, Types::String.optional
attribute? :recordset_cursor, Types::String.optional
###
# A cursor representing a separate query that will alwaysdefault method · ruby · L29-L36 (8 LOC)lib/pack_api/types/collection_result_metadata.rb
def self.default
new(
first_item: 1,
last_item: 1,
total_items: 1,
per_page: 1
)
endfrom_paginator method · ruby · L38-L53 (16 LOC)lib/pack_api/types/collection_result_metadata.rb
def self.from_paginator(paginator, sort = nil, current_page_snapshot_cursor = nil)
new(
first_item: paginator.item_range.begin,
last_item: paginator.item_range.end,
total_items: paginator.total_items,
per_page: paginator.per_page,
next_page_cursor: paginator.next_page_cursor,
previous_page_cursor: paginator.previous_page_cursor,
current_page_cursor: paginator.current_page_cursor,
first_page_cursor: paginator.first_page_cursor,
last_page_cursor: paginator.last_page_cursor,
recordset_cursor: paginator.recordset_cursor,
sort: sort || paginator.sort,
current_page_snapshot_cursor:
)
endCustomFilterDefinition class · ruby · L4-L7 (4 LOC)lib/pack_api/types/custom_filter_definition.rb
class CustomFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:custom)
endEnumFilterDefinition class · ruby · L4-L9 (6 LOC)lib/pack_api/types/enum_filter_definition.rb
class EnumFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:enum)
attribute :options, Types::Array.of(FilterOption)
attribute :can_exclude, Types::Bool.default(false)
endFilterOption class · ruby · L4-L7 (4 LOC)lib/pack_api/types/filter_option.rb
class FilterOption < BaseType
attribute :label, Types::String
attribute :value, Types::String
endmake_gid method · ruby · L8-L10 (3 LOC)lib/pack_api/types/globally_identifiable.rb
def make_gid(model_id)
GlobalID.new(URI::GID.build(app: GlobalID.app, model_name: name, model_id: model_id))
endgid method · ruby · L14-L16 (3 LOC)lib/pack_api/types/globally_identifiable.rb
def gid
@gid ||= self.class.make_gid(id)
endWant fix-PRs on findings? Install Repobility's GitHub App · github.com/apps/repobility-bot
NumericFilterDefinition class · ruby · L4-L8 (5 LOC)lib/pack_api/types/numeric_filter_definition.rb
class NumericFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:numeric)
attribute :operators, Types::Array.of(FilterOption)
endRangeFilterDefinition class · ruby · L4-L9 (6 LOC)lib/pack_api/types/range_filter_definition.rb
class RangeFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:range)
attribute :range_start_constraints, Types::Hash.schema(min: Types::Any.optional, max: Types::Any.optional).optional
attribute :range_end_constraints, Types::Hash.schema(min: Types::Any.optional, max: Types::Any.optional).optional
endResult class · ruby · L4-L30 (27 LOC)lib/pack_api/types/result.rb
class Result < Dry::Struct
attribute :success, Types::Bool
attribute? :value, Types::Any.optional
attribute? :errors, Types::Hash.optional
attribute? :collection_metadata, CollectionResultMetadata.optional
def self.from_request_error(message, model: nil)
new(success: false, errors: { request: [message] }, value: model)
end
def self.from_collection(models:,
value_object_factory:,
optional_attributes: nil,
paginator: nil,
sort: nil,
current_page_snapshot_cursor: nil)
value = value_object_factory.create_collection(models:, optional_attributes:)
if paginator.present?
collection_metadata = CollectionResultMetadata.from_paginator(paginator, sort, current_page_snapshot_cursor)
end
new(
success: true,
value:,
collection_metadata:
)
endfrom_request_error method · ruby · L10-L12 (3 LOC)lib/pack_api/types/result.rb
def self.from_request_error(message, model: nil)
new(success: false, errors: { request: [message] }, value: model)
endfrom_collection method · ruby · L14-L23 (10 LOC)lib/pack_api/types/result.rb
def self.from_collection(models:,
value_object_factory:,
optional_attributes: nil,
paginator: nil,
sort: nil,
current_page_snapshot_cursor: nil)
value = value_object_factory.create_collection(models:, optional_attributes:)
if paginator.present?
collection_metadata = CollectionResultMetadata.from_paginator(paginator, sort, current_page_snapshot_cursor)
endfrom_model method · ruby · L32-L45 (14 LOC)lib/pack_api/types/result.rb
def self.from_model(model:, value_object_factory:, optional_attributes: nil)
if model.errors.present?
errors = value_object_factory.create_errors(model:)
errors[:request] = model.errors[:base] if model.errors[:base].present?
new(
success: false,
errors:
)
else
new(
success: true,
value: value_object_factory.create_object(model:, optional_attributes:)
)
endattribute_error_string function · ruby · L48-L52 (5 LOC)lib/pack_api/types/result.rb
def attribute_error_string(attribute)
return '' if errors.nil? || errors[attribute].blank?
errors[attribute]&.join(', ')
endrequest_error_string function · ruby · L54-L58 (5 LOC)lib/pack_api/types/result.rb
def request_error_string
return '' if errors.nil? || errors[:request].blank?
"#{errors[:request]&.join(', ')}."
endSource: Repobility analyzer · https://repobility.com
error_string function · ruby · L60-L65 (6 LOC)lib/pack_api/types/result.rb
def error_string
return '' if errors.nil?
one_message_per_error = errors.map do |attribute, errors|
"#{attribute.to_s.titleize}: #{Array.wrap(errors).join(', ')}"
endSimpleFilterDefinition class · ruby · L4-L8 (5 LOC)lib/pack_api/types/simple_filter_definition.rb
class SimpleFilterDefinition < BaseType
attribute :name, Types::Symbol
attribute :type, Types::Symbol.default(:simple)
attribute :data_type, Types::String
endValuesInBackgroundBatches class · ruby · L4-L40 (37 LOC)lib/pack_api/values_in_background_batches.rb
class ValuesInBackgroundBatches < ValuesInBatches
include Enumerable
attr_reader :low_watermark, :background_worker
def initialize(method:, batch_size:, optional_attributes: nil, **kwargs, &block)
super
@low_watermark = 1
end
def each
@background_worker = Rails.env.test? ?
Concurrent::ImmediateExecutor.new :
Concurrent::SingleThreadExecutor.new
super
background_worker.shutdown
@background_worker = nil
end
private
def process_item(index, item, block)
cache_next_batch if index == low_watermark
block.call(item)
end
def cache_key
@cache_key ||= "api:batches:#{SecureRandom.uuid}"
end
def cache_next_batch
background_worker.post do
Rails.application.executor.wrap do
Rails.logger.debug { "Storing next batch in cache #{cache_key}" }
Rails.cache.write(cache_key, fetch_batch(cursor: next_batch_cursor), expires_in: 1.minute)
end