Sha256: a17995d508edb6a2538a0ceef889ec7eb532712fcfc40e0ad76f05f6333ad438

Contents?: true

Size: 1.84 KB

Versions: 22

Compression:

Stored size: 1.84 KB

Contents

class Card
  module Rule
    class Cache
      class_attribute :sql, :cache_key

      self.sql = %(
        SELECT
          rules.id      AS rule_id,
          settings.id   AS setting_id,
          sets.id       AS set_id,
          sets.left_id  AS anchor_id,
          sets.right_id AS set_tag_id
        FROM cards rules
        JOIN cards sets     ON rules.left_id  = sets.id
        JOIN cards settings ON rules.right_id = settings.id
        WHERE     sets.type_id = #{SetID}
          AND settings.type_id = #{SettingID}
          AND (settings.codename != 'follow' OR rules.db_content != '')
          AND    rules.trash is false
          AND     sets.trash is false
          AND settings.trash is false;
      ).freeze

      self.cache_key = "RULES".freeze

      class << self
        def read
          Card.cache.read(cache_key) || populate
        end

        def populate
          Card.cache.write cache_key, lookup_hash
        end

        def clear
          Card.cache.write cache_key, nil
        end

        def lookup_hash
          rows.each_with_object({}) do |row, hash|
            next unless (key = lookup_key row)

            hash[key] = row["rule_id"].to_i
          end
        end

        def lookup_key row
          return false unless (setting_code = setting_code(row))

          anchor_id = row["anchor_id"]
          return false unless (pattern_code = pattern_code(anchor_id, row))

          [anchor_id, pattern_code, setting_code].compact.map(&:to_s).join "+"
        end

        def pattern_code anchor_id, row
          set_class_id = anchor_id.nil? ? row["set_id"] : row["set_tag_id"]
          Card::Codename[set_class_id.to_i]
        end

        def setting_code row
          Card::Codename[row["setting_id"].to_i]
        end

        def rows
          Card.connection.select_all sql
        end
      end
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
card-1.107.0 lib/card/rule/cache.rb
card-1.106.0 lib/card/rule/cache.rb
card-1.105.6 lib/card/rule/cache.rb
card-1.105.5 lib/card/rule/cache.rb
card-1.105.4 lib/card/rule/cache.rb
card-1.105.3 lib/card/rule/cache.rb
card-1.105.2.pre1 lib/card/rule/cache.rb
card-1.105.2 lib/card/rule/cache.rb
card-1.105.1 lib/card/rule/cache.rb
card-1.105.0 lib/card/rule/cache.rb
card-1.104.2 lib/card/rule/cache.rb
card-1.104.1 lib/card/rule/cache.rb
card-1.104.0 lib/card/rule/cache.rb
card-1.103.4 lib/card/rule/cache.rb
card-1.103.3 lib/card/rule/cache.rb
card-1.103.2 lib/card/rule/cache.rb
card-1.103.1 lib/card/rule/cache.rb
card-1.103.0 lib/card/rule/cache.rb
card-1.101.7 lib/card/rule/cache.rb
card-1.102.0 lib/card/rule/cache.rb