Class: Array

Inherits:
Object
  • Object
show all
Defined in:
lib/array.rb,
lib/rand.rb

Overview

Rozszerzenie klasy Array umożliwiające korzystanie z paginacji za pomocą pluginu Kaminaro

Instance Method Summary collapse

Instance Method Details

#paginable(page, per_page, total_elements_amount) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/array.rb', line 4

def paginable(page, per_page, total_elements_amount)
  @paginate_page = page
  @paginate_per_page = per_page
  @paginate_total_elements_amount = total_elements_amount
  self.instance_eval <<-EVAL
    def current_page
      @paginate_page.to_i
    end

    def num_pages
      (@paginate_total_elements_amount/@paginate_per_page).floor.to_i
    end

    def limit_value
      @paginate_per_page.to_i
    end

    alias :total_pages :num_pages

  EVAL
end

#pick ⇒ Object

Choose and return a random element of self. [1, 2, 3, 4].pick #=> 2 (or 1, 3, 4)



40
41
42
# File 'lib/rand.rb', line 40

def pick
  self[pick_index]
end

#pick! ⇒ Object

Deletes a random element of self, returning that element. a = [1, 2, 3, 4] a.pick #=> 2 a #=> [1, 3, 4]



48
49
50
51
52
53
# File 'lib/rand.rb', line 48

def pick!
  i = pick_index
  rv = self[i]
  delete_at(i)
  rv
end

#pick_index ⇒ Object

Return the index of an random element of self. ["foo", "bar", "baz"].pick_index #=> 1 (or 0, or 2)



57
58
59
# File 'lib/rand.rb', line 57

def pick_index
  rand(size)
end

#pick_index! ⇒ Object

Destructive pick_index. Delete a random element of self and return its index. a = [11, 22, 33, 44] a.pick_index! #=> 2 a #=> [11, 22, 44]



66
67
68
69
70
# File 'lib/rand.rb', line 66

def pick_index!
  i = pick_index
  delete_at i
  i
end

#shuffle ⇒ Object

Return an array of the elements in random order. [11, 22, 33, 44].shuffle #=> [33, 11, 44, 22]



74
75
76
# File 'lib/rand.rb', line 74

def shuffle
  sort_by{rand}
end

#shuffle! ⇒ Object

Destructive shuffle. Arrange the elements of self in new order. a = [11, 22, 33, 44] a.shuffle! a #=> [33, 11, 44, 22]



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

def shuffle!
  sort!{rand <=> 0.5}
end