Module: HasOrder::InstanceMethods

Defined in:
lib/has_order.rb

Instance Method Summary collapse

Instance Method Details

#and_higherObject



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

def and_higher
  where_position(:gteq)
end

#and_lowerObject



64
65
66
# File 'lib/has_order.rb', line 64

def and_lower
  where_position(:lteq)
end

#higherObject



68
69
70
# File 'lib/has_order.rb', line 68

def higher
  where_position(:gt)
end

#lowerObject



60
61
62
# File 'lib/has_order.rb', line 60

def lower
  where_position(:lt)
end

#move_after(node) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/has_order.rb', line 112

def move_after(node)
  self.class.transaction do
    node_pos = node.position
    pos = node_pos + 1

    if list_scope.at(pos).exists?
      node.higher.shift!
    end

    self.position = pos
    save!
  end
end

#move_before(node) ⇒ Object



97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/has_order.rb', line 97

def move_before(node)
  self.class.transaction do
    node_pos = node.position
    pos = node_pos > 0 ? node_pos - 1 : node_pos

    if list_scope.at(pos).exists?
      pos = node_pos
      node.and_higher.shift!
    end

    self.position = pos
    save!
  end
end

#move_to(pos) ⇒ Object



86
87
88
89
90
91
92
93
94
95
# File 'lib/has_order.rb', line 86

def move_to(pos)
  self.class.transaction do
    if node = list_scope.at(pos).first
      node.and_higher.shift!
    end

    self.position = pos
    save!
  end
end

#nextObject



82
83
84
# File 'lib/has_order.rb', line 82

def next
  higher.ordered.first
end

#positionObject



52
53
54
# File 'lib/has_order.rb', line 52

def position
  self[position_column]
end

#position=(pos) ⇒ Object



56
57
58
# File 'lib/has_order.rb', line 56

def position=(pos)
  self[position_column] = pos
end

#prevObject Also known as: previous



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

def prev
  lower.ordered.last
end