Module: Liszt

Defined in:
lib/liszt.rb,
lib/liszt/railtie.rb,
lib/liszt/version.rb,
lib/liszt/redis_list.rb,
lib/liszt/instantizeable.rb

Defined Under Namespace

Modules: ClassMethods, InstanceMethods, Instantizeable Classes: Railtie, RedisList

Constant Summary collapse

VERSION =
"0.1.2"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.redisObject

Returns the value of attribute redis.



10
11
12
# File 'lib/liszt.rb', line 10

def redis
  @redis
end

Class Method Details

.merge_id_lists(canonical, modified, append = false) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/liszt.rb', line 12

def merge_id_lists(canonical, modified, append = false)
  if append
    (modified + (canonical - modified)) & canonical
  else
    ((canonical - modified) + modified) & canonical
  end
end

Instance Method Details

#acts_as_liszt(options = {}) ⇒ Object

Set up a scoped ordering for this model.

Liszt currently only supports one type of ranking per model. It also doesn't currently support re-sorting lists when a scope changes. The assumption is that attributes used as scopes won't change after creation.

The other major limitation at the moment is that scopes can't be nil. If a record has nil for a scope value, its associated list will never have any items in it.

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :scope (Symbol, Array)

    The attribute or attributes to use as list constraints.

  • :conditions (Hash)

    Any extra constraints to impose.

  • :sort_by (Proc)

    A lambda to pass into initialize_list! the first time an item is added to an uninitialized list. It has the same semantics as Enumerable#sort_by.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/liszt.rb', line 38

def acts_as_liszt(options = {})
  extend Instantizeable
  extend ClassMethods
  include InstanceMethods

  # Make "instantized" versions of the class methods.
  ClassMethods.instance_methods.each do |method|
    instantize method
  end

  options.reverse_merge! :conditions => {}, :scope => []

  @liszt_conditions = options[:conditions]
  @liszt_scope = Array(options[:scope]).sort_by(&:to_s)
  @liszt_query = nil
  @liszt_sort_by = options[:sort_by]
  @liszt_append_new_items = options[:append_new_items]
end