module Redcord::Attribute::ClassMethods

Public Instance Methods

_script_arg_custom_index_attrs() click to toggle source
# File lib/redcord/attribute.rb, line 136
def _script_arg_custom_index_attrs
  class_variable_get(:@@custom_index_attributes)
end
_script_arg_index_attrs() click to toggle source
# File lib/redcord/attribute.rb, line 126
def _script_arg_index_attrs
  class_variable_get(:@@index_attributes).to_a
end
_script_arg_range_index_attrs() click to toggle source
# File lib/redcord/attribute.rb, line 131
def _script_arg_range_index_attrs
  class_variable_get(:@@range_index_attributes).to_a
end
_script_arg_ttl() click to toggle source
# File lib/redcord/attribute.rb, line 121
def _script_arg_ttl
  class_variable_get(:@@ttl)&.to_i || -1
end
attribute(name, type, options = {}) click to toggle source
# File lib/redcord/attribute.rb, line 55
def attribute(name, type, options = {})
  # TODO: support uniq options
  # TODO: validate types
  prop(name, type)

  index_attribute(name, type) if options[:index]
end
custom_index(index_name, attrs) click to toggle source
# File lib/redcord/attribute.rb, line 73
def custom_index(index_name, attrs)
  attrs.each do |attr|
    type = props[attr][:type]
    if !can_custom_index?(type)
      raise(Redcord::WrongAttributeType, "Custom index doesn't support '#{type}' attributes.")
    end
  end
  shard_by_attr = class_variable_get(:@@shard_by_attribute)
  if shard_by_attr and shard_by_attr != attrs.first
    raise(
      Redcord::CustomIndexInvalidDesign,
      "shard_by attribute '#{shard_by_attr}' must be placed first in '#{index_name}' index"
    )
  end
  class_variable_get(:@@custom_index_attributes)[index_name] = attrs
end
index_attribute(attr, type) click to toggle source
# File lib/redcord/attribute.rb, line 64
def index_attribute(attr, type)
  if should_range_index?(type)
    class_variable_get(:@@range_index_attributes) << attr
  else
    class_variable_get(:@@index_attributes) << attr
  end
end
shard_by_attribute(attr=nil) click to toggle source
# File lib/redcord/attribute.rb, line 95
def shard_by_attribute(attr=nil)
  return class_variable_get(:@@shard_by_attribute) if attr.nil?

  # attr must be an non-index attribute (index: false)
  if class_variable_get(:@@index_attributes).include?(attr) ||
      class_variable_get(:@@range_index_attributes).include?(attr)
    raise Redcord::InvalidAttribute, "Cannot shard by an index attribute '#{attr}'"
  end

  class_variable_get(:@@custom_index_attributes).each do |index_name, attrs|
    if attr != attrs.first
      raise(
        Redcord::CustomIndexInvalidDesign,
        "shard_by attribute '#{attr}' must be placed first in '#{index_name}' index"
      )
    end

    # Delete the shard_by_attribute since it would be a constant in the
    # custom index set
    attrs.shift
  end

  class_variable_set(:@@shard_by_attribute, attr)
end
ttl(duration) click to toggle source
# File lib/redcord/attribute.rb, line 91
def ttl(duration)
  class_variable_set(:@@ttl, duration)
end

Private Instance Methods

can_custom_index?(type) click to toggle source
# File lib/redcord/attribute.rb, line 151
def can_custom_index?(type)
  # Change Ruby raw type to Sorbet type in order to call subtype_of?
  type = T::Types::Simple.new(type) if type.is_a?(Class)
  type.subtype_of?(CustomIndexType)
end
should_range_index?(type) click to toggle source
# File lib/redcord/attribute.rb, line 143
def should_range_index?(type)
  # Change Ruby raw type to Sorbet type in order to call subtype_of?
  type = T::Types::Simple.new(type) if type.is_a?(Class)

  type.subtype_of?(RangeIndexType)
end