Class: Redcord::Relation

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/redcord/relation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, regular_index_query_conditions = {}, custom_index_query_conditions = {}, select_attrs = Set.new, custom_index_name: nil) ⇒ Relation

Returns a new instance of Relation.



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/redcord/relation.rb', line 39

def initialize(
  model,
  regular_index_query_conditions = {},
  custom_index_query_conditions = {},
  select_attrs = Set.new,
  custom_index_name: nil
)
  @model = model
  @regular_index_query_conditions = regular_index_query_conditions
  @custom_index_query_conditions = custom_index_query_conditions
  @select_attrs = select_attrs
  @custom_index_name = custom_index_name
end

Instance Attribute Details

#custom_index_nameObject (readonly)

Returns the value of attribute custom_index_name.



22
23
24
# File 'lib/redcord/relation.rb', line 22

def custom_index_name
  @custom_index_name
end

#custom_index_query_conditionsObject (readonly)

Returns the value of attribute custom_index_query_conditions.



28
29
30
# File 'lib/redcord/relation.rb', line 28

def custom_index_query_conditions
  @custom_index_query_conditions
end

#modelObject (readonly)

Returns the value of attribute model.



16
17
18
# File 'lib/redcord/relation.rb', line 16

def model
  @model
end

#regular_index_query_conditionsObject (readonly)

Returns the value of attribute regular_index_query_conditions.



25
26
27
# File 'lib/redcord/relation.rb', line 25

def regular_index_query_conditions
  @regular_index_query_conditions
end

#select_attrsObject (readonly)

Returns the value of attribute select_attrs.



19
20
21
# File 'lib/redcord/relation.rb', line 19

def select_attrs
  @select_attrs
end

Instance Method Details

#countObject



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/redcord/relation.rb', line 90

def count
  Redcord::Base.trace(
   'redcord_relation_count',
   model_name: model.name,
  ) do
    model.validate_index_attributes(query_conditions.keys, custom_index_name: custom_index_name)
    redis.find_by_attr_count(
      model.model_key,
      extract_query_conditions!,
      index_attrs: model._script_arg_index_attrs,
      range_index_attrs: model._script_arg_range_index_attrs,
      custom_index_attrs: model._script_arg_custom_index_attrs[custom_index_name],
      hash_tag: extract_hash_tag!,
      custom_index_name: custom_index_name
    )
  end
end

#select(*args, &blk) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/redcord/relation.rb', line 73

def select(*args, &blk)
  Redcord::Base.trace(
   'redcord_relation_select',
   model_name: model.name,
  ) do
    if block_given?
      return execute_query.select do |*item|
        blk.call(*item)
      end
    end

    select_attrs.merge(args)
    self
  end
end

#where(args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/redcord/relation.rb', line 54

def where(args)
  encoded_args = args.map do |attr_key, attr_val|
    encoded_val = model.validate_types_and_encode_query(attr_key, attr_val)
    [attr_key, encoded_val]
  end

  regular_index_query_conditions.merge!(encoded_args.to_h)
  if custom_index_name
    with_index(custom_index_name)
  end
  self
end

#with_index(index_name) ⇒ Object



109
110
111
112
113
114
# File 'lib/redcord/relation.rb', line 109

def with_index(index_name)
  @custom_index_name = index_name
  adjusted_query_conditions = model.validate_and_adjust_custom_index_query_conditions(regular_index_query_conditions)
  custom_index_query_conditions.merge!(adjusted_query_conditions)
  self
end