Module: LowCardTables::LowCardTable::Base

Extended by:
ActiveSupport::Concern
Includes:
CacheExpiration::HasCacheExpiration
Defined in:
lib/low_card_tables/low_card_table/base.rb

Overview

LowCardTables::LowCardTable::Base is the module that’s included into any ActiveRecord model that you declare is_low_card_table on. As such, it defines the API that’s available on low-card tables. (The standard ActiveRecord API does, of course, still remain available, so you can also use that if you want.)

Be careful of the distinction between the ClassMethods and instance methods here. ClassMethods are available on the low-card table as a whole; instance methods apply, of course, to each row individually.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#_low_card_column_matches?(key, value) ⇒ Boolean

This is called by _low_card_row_matches_hash?, in a loop; it asks whether the given column (key) matches the given value (value). See #_low_card_row_matches_any_hash? for more details. You can override this method instead of #_low_card_row_matches_any_hash? or #_low_card_row_matches_hash?, if its semantics work better for your purposes, since its behavior will affect those methods as well.

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/low_card_tables/low_card_table/base.rb', line 60

def _low_card_column_matches?(key, value)
  my_value = self[key.to_s]

  if value.kind_of?(Array)
    if my_value && my_value.kind_of?(Symbol)
      my_value = my_value.to_s
    end

    value = value.map { |m| if m.kind_of?(Symbol) then m.to_s else m end }
    value.include?(my_value)
  else
    value_sym = value.kind_of?(Symbol)
    my_value_sym = my_value.kind_of?(Symbol)

    if (value_sym != my_value_sym) && value && my_value
      my_value.to_s.eql?(value.to_s)
    else
      my_value.eql?(value)
    end
  end
end

#_low_card_row_matches_any_hash?(hashes) ⇒ Boolean

This method is a critical entry point from the rest of the low-card system. For example, given our usual User/UserStatus example – when you save a User object, the low-card system grabs the associated UserStatus object and creates a hash, mapping all of the columns in UserStatus to their corresponding values. Next, it iterates through its in-memory cache, using this method to determine which of the rows in the cache matches the hash it extracted – and, when it finds one, that’s the row it uses to get the low-card ID to assign in the associated table.

IMPORTANT: this is not the only context in which this method is used, but merely one example.

It’s possible to override this method to alter behavior; for example, you could use this to translate symbols to integers, pin values, or otherwise transform data. But be extremely careful when you do this, as you’re playing with a very low-level part of the low-card system.

Note that the hashes supplied can be partial or complete; that is, they may specify any subset of the values in this table, or all of them. This method must work accordingly – if the hashes are partial, then, if this row’s values for the keys that are specified match, then it should return true.

This is the highest-level, most ‘bulk’ method – it asks whether this row matches any of the hashes in the supplied array.

Returns:

  • (Boolean)


44
45
46
# File 'lib/low_card_tables/low_card_table/base.rb', line 44

def _low_card_row_matches_any_hash?(hashes)
  hashes.detect { |hash| _low_card_row_matches_hash?(hash) }
end

#_low_card_row_matches_block?(block) ⇒ Boolean

This method is called from methods like #low_card_rows_matching, when passed a block – its job is simply to see if this row is matched by the given block. It’s hard to imagine a different implementation than this one, but it’s here in case you want to override it.

Returns:

  • (Boolean)


85
86
87
# File 'lib/low_card_tables/low_card_table/base.rb', line 85

def _low_card_row_matches_block?(block)
  block.call(self)
end

#_low_card_row_matches_hash?(hash) ⇒ Boolean

This is called by #_low_card_row_matches_any_hash?, in a loop; it asks whether this row matches the hash provided. See #_low_card_row_matches_any_hash? for more details. You can override this method instead of that one, if its semantics work better for your purposes, since its behavior will affect that of #_low_card_row_matches_any_hash?.

Returns:

  • (Boolean)


52
53
54
# File 'lib/low_card_tables/low_card_table/base.rb', line 52

def _low_card_row_matches_hash?(hash)
  hash.keys.all? { |key| _low_card_column_matches?(key, hash[key]) }
end