Module: Redcord::Attribute::ClassMethods

Extended by:
T::Sig
Defined in:
lib/redcord/attribute.rb

Instance Method Summary collapse

Instance Method Details

#_script_arg_custom_index_attrsObject



136
137
138
# File 'lib/redcord/attribute.rb', line 136

def _script_arg_custom_index_attrs
  class_variable_get(:@@custom_index_attributes)
end

#_script_arg_index_attrsObject



126
127
128
# File 'lib/redcord/attribute.rb', line 126

def _script_arg_index_attrs
  class_variable_get(:@@index_attributes).to_a
end

#_script_arg_range_index_attrsObject



131
132
133
# File 'lib/redcord/attribute.rb', line 131

def _script_arg_range_index_attrs
  class_variable_get(:@@range_index_attributes).to_a
end

#_script_arg_ttlObject



121
122
123
# File 'lib/redcord/attribute.rb', line 121

def _script_arg_ttl
  class_variable_get(:@@ttl)&.to_i || -1
end

#attribute(name, type, options = {}) ⇒ Object



55
56
57
58
59
60
61
# 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) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# 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) ⇒ Object



91
92
93
# File 'lib/redcord/attribute.rb', line 91

def ttl(duration)
  class_variable_set(:@@ttl, duration)
end