Sha256: 3d01a1798db6c2dd6ecf663c45bc64b9e105b1f1917351c5e2f73ab34c4b9d91

Contents?: true

Size: 1.24 KB

Versions: 2

Compression:

Stored size: 1.24 KB

Contents

module RedisBackedModel

  class SortedSet
  require 'date'
    
  
    def initialize(model, model_id, definition)
      @model      = model
      @model_id   = model_id
      @definition = definition
    end
  
    def to_redis
      "zadd #{key} #{score} #{member}"
    end
  
    private
      
      attr_accessor :model, :model_id #, :by_attribute, :score_label, :score
      
      def definition_keys
        @definition.keys.first
      end
      
      def definition_values
        @definition.values.first
      end
      
      def key_by
        parse_definition(definition_keys)[1]
      end
      
      def key_for
        parse_definition(definition_keys)[0]        
      end

      def key_model_name
        @model.to_s.underscore.pluralize
      end

      def key_for_value
        parse_definition(definition_values)[0]
      end

      def member
        @model_id
      end
        
      def key
        "#{key_model_name}_for_#{key_for}_by_#{key_by}:#{key_for_value}"
      end

      def score
        score = parse_definition(definition_values)[1]
        key_by == 'date' ? Date.parse(score).to_time.to_f : score
      end
      
      def parse_definition(string)
        string.match(/.*\[(\S+)\]/)[1].split('|')
      end
  
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
redis_backed_model-0.0.2 lib/redis_backed_model/sorted_set.rb
redis_backed_model-0.0.1 lib/redis_backed_model/sorted_set.rb