Module: Liszt::ClassMethods

Defined in:
lib/liszt.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



45
46
47
48
49
# File 'lib/liszt.rb', line 45

def self.extended(base)
  class << base
    attr_reader :liszt_sort_by, :liszt_append_new_items
  end
end

Instance Method Details

#clear_list(obj = {}) ⇒ Object



109
110
111
# File 'lib/liszt.rb', line 109

def clear_list(obj={})
  ordered_list(obj).clear
end

#initialize_list!(obj = {}, &block) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/liszt.rb', line 51

def initialize_list!(obj={}, &block)
  objects = find(:all, :conditions => liszt_query(obj))

  # If the caller provided a block, or if they passed in a default
  # with the :sort_by option, sort the objects by that block's
  # output before populating the list with their ids. If not, put
  # the objects in descending order by id.
  ids = if block_given?
          objects.sort_by(&block).map(&:id)
        else
          if @liszt_sort_by
            objects.sort_by(&@liszt_sort_by).map(&:id)
          else
            objects.map(&:id).sort.reverse
          end
        end

  ordered_list(obj).clear_and_populate!(ids)
  ids
end

#liszt_key(obj = {}) ⇒ Object (private)

Return the key for the Redis list that includes the given object.



119
120
121
122
123
124
125
# File 'lib/liszt.rb', line 119

def liszt_key(obj={})
  key = "liszt:#{table_name}"
  @liszt_scope.each do |scope|
    key << ":#{scope}:#{obj[scope]}"
  end
  key
end

#liszt_query(obj = {}) ⇒ Object (private)

Return the query that retrieves objects eligible to be in the list that includes the given object.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/liszt.rb', line 129

def liszt_query(obj={})
  if @liszt_query.nil?
    query = ['1 = 1']

    @liszt_conditions.each do |key, value|
      query.first << " AND (#{table_name}.#{key} "
      if value.nil?
        query.first << "IS NULL)"
      else
        query.first << "= ?)"
        query << value
      end
    end

    @liszt_scope.each do |scope|
      query.first << " AND (#{table_name}.#{scope} = ?)"
    end

    @liszt_query = query
  end

  @liszt_query + @liszt_scope.map { |scope| obj[scope] }
end

#meets_list_conditions?(obj = {}) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/liszt.rb', line 113

def meets_list_conditions?(obj={})
  @liszt_conditions.all? { |key, value| obj[key] == value }
end

#ordered_list(obj = {}) ⇒ Object



72
73
74
# File 'lib/liszt.rb', line 72

def ordered_list(obj={})
  Liszt::RedisList.new(liszt_key(obj))
end

#ordered_list_ids(obj = {}) ⇒ Object



80
81
82
83
84
85
86
# File 'lib/liszt.rb', line 80

def ordered_list_ids(obj={})
  if ordered_list_initialized?(obj)
    ordered_list(obj).all
  else
    initialize_list!(obj)
  end
end

#ordered_list_initialized?(obj = {}) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/liszt.rb', line 76

def ordered_list_initialized?(obj={})
  ordered_list(obj).initialized?
end

#ordered_list_items(obj = {}, opts = {}) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/liszt.rb', line 88

def ordered_list_items(obj={}, opts={})
  force_refresh = opts.delete(:force_refresh) || false
  was_initialized = ordered_list_initialized?(obj)
  ids = ordered_list_ids(obj)

  # If ordered_list_ids just did the initialization, we can trust that
  # the list of ids is accurate and ignore the force_refresh flag.
  if force_refresh and was_initialized
    objs = find(:all, {:conditions => liszt_query(obj)}.merge(opts))
    real_ids = objs.map(&:id)
    unlisted_ids = real_ids - ids
    if unlisted_ids.count > 0
      ids = ordered_list(obj).clear_and_populate!(unlisted_ids + ids)
    end
  else
    objs = find(:all, {:conditions => ['id in (?)', ids]}.merge(opts))
  end

  objs.sort_by { |obj| ids.index(obj.id) }
end