Function bodies 314 total
AttributeMapRegistry class · ruby · L4-L13 (10 LOC)lib/pack_api/mapping/attribute_map_registry.rb
class AttributeMapRegistry
class << self
attr_reader :attribute_maps
def register_attribute_map(attribute_map_class)
@attribute_maps ||= {}
@attribute_maps[attribute_map_class.model_type] = attribute_map_class
end
endregister_attribute_map method · ruby · L9-L12 (4 LOC)lib/pack_api/mapping/attribute_map_registry.rb
def register_attribute_map(attribute_map_class)
@attribute_maps ||= {}
@attribute_maps[attribute_map_class.model_type] = attribute_map_class
endattribute_map_class method · ruby · L15-L19 (5 LOC)lib/pack_api/mapping/attribute_map_registry.rb
def attribute_map_class(model_class)
raise "No attribute map defined for #{model_class}" unless self.class.attribute_maps.key?(model_class)
self.class.attribute_maps[model_class]
endErrorHashToAPIAttributesTransformer class · ruby · L7-L26 (20 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
class ErrorHashToAPIAttributesTransformer < AbstractTransformer
NESTED_ATTRIBUTE_ERROR_KEY = /\A(?<parent>[\w_]+)\[(?<index>\d+)\]\.(?<child>[\w_]+)\z/
def initialize(config)
super
@transform_nested_attributes_with = config[:transform_nested_attributes_with]
end
def execute
api_attributes = api_attribute_names.flat_map do |api_attribute|
model_attribute = model_attribute(api_attribute)
next unless error_present_for?(model_attribute)
error_keys_for(model_attribute).map do |error_key|
converted_error_key = nested_attribute_error_key?(error_key) ?
convert_nested_attribute_error_key(error_key, api_attribute) :
normalize_association_reference(api_attribute, model_attribute)
[converted_error_key, data_source[error_key]]
end
endinitialize method · ruby · L10-L13 (4 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def initialize(config)
super
@transform_nested_attributes_with = config[:transform_nested_attributes_with]
endexecute method · ruby · L15-L25 (11 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def execute
api_attributes = api_attribute_names.flat_map do |api_attribute|
model_attribute = model_attribute(api_attribute)
next unless error_present_for?(model_attribute)
error_keys_for(model_attribute).map do |error_key|
converted_error_key = nested_attribute_error_key?(error_key) ?
convert_nested_attribute_error_key(error_key, api_attribute) :
normalize_association_reference(api_attribute, model_attribute)
[converted_error_key, data_source[error_key]]
endconvert_nested_attribute_error_key function · ruby · L32-L40 (9 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def convert_nested_attribute_error_key(error_key, parent_api_attribute)
NESTED_ATTRIBUTE_ERROR_KEY.match(error_key) do |match_data|
child_attribute_map_class = @transform_nested_attributes_with[parent_api_attribute]
child_model_attribute = match_data[:child].to_sym
child_api_attribute = child_model_attribute
if child_attribute_map_class
mapping = child_attribute_map_class.config[:mappings].find { |_k, v| v == child_model_attribute }
child_api_attribute = mapping.first if mapping
endRepobility · MCP-ready · https://repobility.com
nested_attribute_error_key? function · ruby · L45-L47 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def nested_attribute_error_key?(error_key)
NESTED_ATTRIBUTE_ERROR_KEY.match?(error_key.to_s)
enderror_keys_for function · ruby · L49-L53 (5 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def error_keys_for(model_attribute)
data_source.include?(model_attribute) ?
[model_attribute] :
nested_attributes_with_errors(model_attribute)
enderror_present_for? function · ruby · L55-L57 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def error_present_for?(model_attribute)
data_source.include?(model_attribute) || nested_attributes_with_errors(model_attribute).any?
endnested_attributes_with_errors function · ruby · L59-L61 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def nested_attributes_with_errors(parent_attribute)
data_source.attribute_names.select { |attribute| attribute.to_s.start_with?("#{parent_attribute}[") }
endresource_association? function · ruby · L63-L65 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def resource_association?(model_attribute)
resource_associations.include?(model_attribute)
endcollection_association? function · ruby · L67-L69 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def collection_association?(model_attribute)
collection_associations.include?(model_attribute)
endaccepts_nested_attributes_for? function · ruby · L71-L73 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def accepts_nested_attributes_for?(association_name)
model_type.nested_attributes_options.key?(association_name)
endresource_associations function · ruby · L75-L79 (5 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def resource_associations
@resource_associations ||= model_type.reflect_on_all_associations
.reject(&:collection?)
.map(&:name)
endRepobility · severity-and-effort ranking · https://repobility.com
collection_associations function · ruby · L81-L85 (5 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def collection_associations
@collection_associations ||= model_type.reflect_on_all_associations
.select(&:collection?)
.map(&:name)
endnormalize_association_reference function · ruby · L87-L96 (10 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def normalize_association_reference(api_attribute, model_attribute)
if accepts_nested_attributes_for?(model_attribute)
api_attribute
elsif resource_association?(model_attribute) && !model_attribute.to_s.end_with?('_id')
normalize_resource_association_reference(api_attribute)
elsif collection_association?(model_attribute) && !model_attribute.to_s.end_with?('_ids')
normalize_collection_association_reference(api_attribute)
else
api_attribute
endnormalize_collection_association_reference function · ruby · L99-L101 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def normalize_collection_association_reference(api_attribute)
:"#{api_attribute.to_s.singularize}_ids"
endnormalize_resource_association_reference function · ruby · L103-L105 (3 LOC)lib/pack_api/mapping/error_hash_to_api_attributes_transformer.rb
def normalize_resource_association_reference(api_attribute)
:"#{api_attribute.to_s.singularize}_id"
endFilterMap class · ruby · L7-L28 (22 LOC)lib/pack_api/mapping/filter_map.rb
class FilterMap
attr_reader :filter_factory, :attribute_map_class
def initialize(filter_factory:, attribute_map_class: nil)
@filter_factory = filter_factory
@attribute_map_class = attribute_map_class
end
def from_api_filters(api_filters)
validate(api_filters)
transform_api_attribute_filters(api_filters).merge(transform_api_custom_filters(api_filters))
end
def filter_definitions(filter_names: nil, **)
supported_filters.filter_map do |filter_name, filter_class|
next if filter_names&.exclude?(filter_name)
api_attribute_filter_name_map.key?(filter_name) ?
attribute_filter_definition(filter_name, filter_class) :
other_filter_definition(filter_name, filter_class, **)
end
endinitialize method · ruby · L10-L13 (4 LOC)lib/pack_api/mapping/filter_map.rb
def initialize(filter_factory:, attribute_map_class: nil)
@filter_factory = filter_factory
@attribute_map_class = attribute_map_class
endfrom_api_filters method · ruby · L15-L18 (4 LOC)lib/pack_api/mapping/filter_map.rb
def from_api_filters(api_filters)
validate(api_filters)
transform_api_attribute_filters(api_filters).merge(transform_api_custom_filters(api_filters))
endfilter_definitions method · ruby · L20-L27 (8 LOC)lib/pack_api/mapping/filter_map.rb
def filter_definitions(filter_names: nil, **)
supported_filters.filter_map do |filter_name, filter_class|
next if filter_names&.exclude?(filter_name)
api_attribute_filter_name_map.key?(filter_name) ?
attribute_filter_definition(filter_name, filter_class) :
other_filter_definition(filter_name, filter_class, **)
endRepobility — same analyzer, your code, free for public repos · /scan/
attribute_filter_definition method · ruby · L32-L35 (4 LOC)lib/pack_api/mapping/filter_map.rb
def attribute_filter_definition(filter_name, filter_class)
api_filter_name = api_attribute_filter_name_map[filter_name]
filter_class.definition.merge(name: api_filter_name)
endother_filter_definition method · ruby · L37-L39 (3 LOC)lib/pack_api/mapping/filter_map.rb
def other_filter_definition(_filter_name, filter_class, **)
filter_class.definition(**)
endapi_attribute_filter_names method · ruby · L41-L45 (5 LOC)lib/pack_api/mapping/filter_map.rb
def api_attribute_filter_names
@api_attribute_filter_names ||= attribute_map_class.nil? ?
PackAPI::FrozenEmpty::ARRAY :
attribute_map_class.api_type.filterable_attributes.keys
endapi_attribute_filter_name_map method · ruby · L49-L56 (8 LOC)lib/pack_api/mapping/filter_map.rb
def api_attribute_filter_name_map
return @api_attribute_filter_name_map if defined?(@api_attribute_filter_name_map)
return PackAPI::FrozenEmpty::HASH if attribute_map_class.nil?
names = {}
api_attribute_filter_names.each { |name| names[name] = name }
@api_attribute_filter_name_map = attribute_map_class.model_attribute_keys(names)
endvalidate method · ruby · L58-L61 (4 LOC)lib/pack_api/mapping/filter_map.rb
def validate(filters)
invalid_filter_names = filters.keys.reject { |filter_name| valid?(filter_name) }
raise PackAPI::InternalError, validation_error_message(invalid_filter_names) if invalid_filter_names.any?
endvalid? method · ruby · L63-L65 (3 LOC)lib/pack_api/mapping/filter_map.rb
def valid?(filter_name)
api_filter_names.include?(filter_name)
endapi_filter_names method · ruby · L67-L70 (4 LOC)lib/pack_api/mapping/filter_map.rb
def api_filter_names
@api_filter_names ||= supported_filters.keys.map do |filter_name|
api_attribute_filter_name_map.fetch(filter_name, filter_name)
endvalidation_error_message function · ruby · L73-L77 (5 LOC)lib/pack_api/mapping/filter_map.rb
def validation_error_message(unsupported_filter_names)
filter_names_string = unsupported_filter_names.map { |filter_name| "'#{filter_name}'" }.join(', ')
api_type_name = attribute_map_class.api_type.name
"unsupported #{'filter'.pluralize(unsupported_filter_names.size)} #{filter_names_string} for #{api_type_name}."
endRepobility — the code-quality scanner for AI-generated software · https://repobility.com
transform_api_attribute_filters function · ruby · L79-L84 (6 LOC)lib/pack_api/mapping/filter_map.rb
def transform_api_attribute_filters(api_filters)
return api_filters if attribute_map_class.nil?
api_attribute_filters = api_filters.select { |filter_name, _| api_attribute_filter_names.include?(filter_name) }
attribute_map_class.model_attribute_keys(api_attribute_filters)
endtransform_api_custom_filters function · ruby · L86-L88 (3 LOC)lib/pack_api/mapping/filter_map.rb
def transform_api_custom_filters(api_filters)
api_filters.except(*api_attribute_filter_names)
endsupported_filters function · ruby · L90-L92 (3 LOC)lib/pack_api/mapping/filter_map.rb
def supported_filters
@supported_filters ||= filter_factory.filter_classes
endModelToAPIAttributesTransformer class · ruby · L7-L23 (17 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
class ModelToAPIAttributesTransformer < AbstractTransformer
def options=(options)
super
@optional_attributes_to_include = nil
@model_attributes_of_interest = nil
@api_attributes_of_interest = nil
end
def execute
api_attribute_names.each_with_object({}) do |api_attribute, result|
model_attribute = model_attribute(api_attribute)
next unless include_model_attribute?(model_attribute)
api_value = transform_value(api_attribute, model_value(model_attribute)) if include_api_attribute?(api_attribute)
result[api_attribute] = api_value
end
endoptions= method · ruby · L8-L13 (6 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def options=(options)
super
@optional_attributes_to_include = nil
@model_attributes_of_interest = nil
@api_attributes_of_interest = nil
endexecute method · ruby · L15-L22 (8 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def execute
api_attribute_names.each_with_object({}) do |api_attribute, result|
model_attribute = model_attribute(api_attribute)
next unless include_model_attribute?(model_attribute)
api_value = transform_value(api_attribute, model_value(model_attribute)) if include_api_attribute?(api_attribute)
result[api_attribute] = api_value
endapi_attribute_names method · ruby · L27-L29 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def api_attribute_names
api_attributes_of_interest || api_type.attribute_names
endinclude_api_attribute? method · ruby · L31-L33 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def include_api_attribute?(api_attribute)
!optional_api_attribute?(api_attribute) || include_optional_api_attribute?(api_attribute)
endRepobility · MCP-ready · https://repobility.com
model_attributes_of_interest method · ruby · L35-L37 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def model_attributes_of_interest
@model_attributes_of_interest ||= options[:model_attributes_of_interest]
endapi_attributes_of_interest method · ruby · L39-L41 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def api_attributes_of_interest
@api_attributes_of_interest ||= options[:api_attributes_of_interest]
endoptional_attributes_to_include method · ruby · L43-L45 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def optional_attributes_to_include
@optional_attributes_to_include ||= options[:optional_attributes]
endoptional_api_attribute? method · ruby · L47-L49 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def optional_api_attribute?(api_attribute_name)
api_type_optional_attributes.include?(api_attribute_name)
endinclude_optional_api_attribute? method · ruby · L51-L58 (8 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def include_optional_api_attribute?(api_attribute_name)
return true if api_attributes_of_interest && api_attributes_of_interest.include?(api_attribute_name)
return false if optional_attributes_to_include.nil?
return false if optional_attributes_to_include.respond_to?(:exclude?) &&
optional_attributes_to_include.exclude?(api_attribute_name)
true
endinclude_model_attribute? method · ruby · L60-L64 (5 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def include_model_attribute?(model_attribute)
return true if model_attributes_of_interest.nil?
model_attributes_of_interest.include?(model_attribute)
endapi_type_optional_attributes method · ruby · L66-L68 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def api_type_optional_attributes
@api_type_optional_attributes ||= api_type.optional_attributes || PackAPI::FrozenEmpty::ARRAY
endmodel_value method · ruby · L70-L72 (3 LOC)lib/pack_api/mapping/model_to_api_attributes_transformer.rb
def model_value(model_attribute)
data_source.public_send(model_attribute)
endRepobility · severity-and-effort ranking · https://repobility.com
NormalizedAPIAttribute class · ruby · L4-L22 (19 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
class NormalizedAPIAttribute
PLURAL_IDS = '_ids'
SINGULAR_ID = '_id'
attr_reader :api_attribute_names
def initialize(api_attribute_names)
@api_attribute_names = api_attribute_names
end
def normalize(attribute_name)
if id_for_resource_association?(attribute_name)
normalize_resource_association_reference(attribute_name)
elsif id_for_collection_association?(attribute_name)
normalize_collection_association_reference(attribute_name)
else
attribute_name
end
endinitialize method · ruby · L10-L12 (3 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def initialize(api_attribute_names)
@api_attribute_names = api_attribute_names
endnormalize method · ruby · L14-L21 (8 LOC)lib/pack_api/mapping/normalized_api_attribute.rb
def normalize(attribute_name)
if id_for_resource_association?(attribute_name)
normalize_resource_association_reference(attribute_name)
elsif id_for_collection_association?(attribute_name)
normalize_collection_association_reference(attribute_name)
else
attribute_name
end