Sha256: c2d229b39582d6638b92615160d87dd08fc68cd04c9936cb182965990997b9ea

Contents?: true

Size: 1.24 KB

Versions: 1

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

1 entries across 1 versions & 1 rubygems

Version Path
redis_backed_model-0.0.3 lib/redis_backed_model/sorted_set.rb