Module: Sequel::Plugins::List::InstanceMethods

Defined in:
lib/sequel/plugins/list.rb

Instance Method Summary collapse

Instance Method Details

#at_position(p) ⇒ Object

The model object at the given position in the list containing this instance.



91
92
93
# File 'lib/sequel/plugins/list.rb', line 91

def at_position(p)
  list_dataset.first(position_field => p)
end

#before_createObject

Set the value of the position_field to the maximum value plus 1 unless the position field already has a value.



97
98
99
100
101
# File 'lib/sequel/plugins/list.rb', line 97

def before_create
  unless send(position_field)
    send("#{position_field}=", list_dataset.max(position_field).to_i+1)
  end
end

#last_positionObject

Find the last position in the list containing this instance.



104
105
106
# File 'lib/sequel/plugins/list.rb', line 104

def last_position
  list_dataset.max(position_field).to_i
end

#list_datasetObject

A dataset that represents the list containing this instance.



109
110
111
# File 'lib/sequel/plugins/list.rb', line 109

def list_dataset
  model.scope_proc ? model.scope_proc.call(self) : model.dataset
end

#move_down(n = 1) ⇒ Object

Move this instance down the given number of places in the list, or 1 place if no argument is specified.



115
116
117
# File 'lib/sequel/plugins/list.rb', line 115

def move_down(n = 1)
  move_to(position_value + n)
end

#move_to(target, lp = nil) ⇒ Object

Move this instance to the given place in the list. Raises an exception if target is less than 1 or greater than the last position in the list.



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/sequel/plugins/list.rb', line 121

def move_to(target, lp = nil)
  current = position_value
  if target != current
    checked_transaction do
      ds = list_dataset
      op, ds = if target < current
        raise(Sequel::Error, "Moving too far up (target = #{target})") if target < 1
        [:+, ds.filter(position_field=>target...current)]
      else
        lp ||= last_position
        raise(Sequel::Error, "Moving too far down (target = #{target}, last_position = #{lp})") if target > lp
        [:-, ds.filter(position_field=>(current + 1)..target)]
      end
      ds.update(position_field => Sequel::SQL::NumericExpression.new(op, position_field, 1))
      update(position_field => target)
    end
  end
  self
end

#move_to_bottomObject

Move this instance to the bottom (last position) of the list.



142
143
144
145
# File 'lib/sequel/plugins/list.rb', line 142

def move_to_bottom
  lp = last_position 
  move_to(lp, lp)
end

#move_to_topObject

Move this instance to the top (first position, position 1) of the list.



148
149
150
# File 'lib/sequel/plugins/list.rb', line 148

def move_to_top
  move_to(1)
end

#move_up(n = 1) ⇒ Object

Move this instance the given number of places up in the list, or 1 place if no argument is specified.



154
155
156
# File 'lib/sequel/plugins/list.rb', line 154

def move_up(n = 1)
  move_to(position_value - n) 
end

#next(n = 1) ⇒ Object

The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.



160
161
162
# File 'lib/sequel/plugins/list.rb', line 160

def next(n = 1)
  n == 0 ? self : at_position(position_value + n)
end

#position_valueObject

The value of the model’s position field for this instance.



165
166
167
# File 'lib/sequel/plugins/list.rb', line 165

def position_value
  send(position_field)
end

#prev(n = 1) ⇒ Object

The model instance the given number of places below this model instance in the list, or 1 place below if no argument is given.



171
172
173
# File 'lib/sequel/plugins/list.rb', line 171

def prev(n = 1)
  self.next(n * -1)
end