Class: Alchemy::ElementsRepository

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
app/models/alchemy/elements_repository.rb

Overview

Mimics ActiveRecord query interface but does this on the preloaded elements

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(elements) ⇒ ElementsRepository

Returns a new instance of ElementsRepository.

Parameters:

  • (ActiveRecord::Relation)

15
16
17
# File 'app/models/alchemy/elements_repository.rb', line 15

def initialize(elements)
  @elements = elements.to_a
end

Class Method Details

.noneObject

An empty set of elements


10
11
12
# File 'app/models/alchemy/elements_repository.rb', line 10

def self.none
  new([])
end

Instance Method Details

#children_of(parent) ⇒ Object


114
115
116
# File 'app/models/alchemy/elements_repository.rb', line 114

def children_of(parent)
  self.class.new(select { |e| e.parent_element_id == parent.id })
end

#each(&blk) ⇒ Object


118
119
120
# File 'app/models/alchemy/elements_repository.rb', line 118

def each(&blk)
  elements.each(&blk)
end

#excluded(*names) ⇒ Alchemy::ElementRepository

All elements excluding those wth given name(s)

Parameters:

  • (Array<String|Symbol>|String|Symbol)

Returns:

  • (Alchemy::ElementRepository)

55
56
57
58
# File 'app/models/alchemy/elements_repository.rb', line 55

def excluded(*names)
  names.flatten!
  self.class.new(reject { |e| e.name.in?(names.map!(&:to_s)) })
end

#expandedAlchemy::ElementRepository

All expanded elements

Returns:

  • (Alchemy::ElementRepository)

80
81
82
# File 'app/models/alchemy/elements_repository.rb', line 80

def expanded
  self.class.new reject(&:folded)
end

#fixedAlchemy::ElementRepository

All fixed elements

Returns:

  • (Alchemy::ElementRepository)

62
63
64
# File 'app/models/alchemy/elements_repository.rb', line 62

def fixed
  self.class.new select(&:fixed)
end

#foldedAlchemy::ElementRepository

All folded elements

Returns:

  • (Alchemy::ElementRepository)

74
75
76
# File 'app/models/alchemy/elements_repository.rb', line 74

def folded
  self.class.new select(&:folded)
end

#hiddenAlchemy::ElementRepository

All not fixed elements

Returns:

  • (Alchemy::ElementRepository)

27
28
29
# File 'app/models/alchemy/elements_repository.rb', line 27

def hidden
  self.class.new reject(&:public)
end

#limit(limit) ⇒ Alchemy::ElementRepository

Elements limitted by

Returns:

  • (Alchemy::ElementRepository)

110
111
112
# File 'app/models/alchemy/elements_repository.rb', line 110

def limit(limit)
  self.class.new elements[0..(limit.to_i - 1)]
end

#named(*names) ⇒ Alchemy::ElementRepository

All elements with given name(s)

Parameters:

  • (Array<String|Symbol>|String|Symbol)

Returns:

  • (Alchemy::ElementRepository)

34
35
36
37
# File 'app/models/alchemy/elements_repository.rb', line 34

def named(*names)
  names.flatten!
  self.class.new(select { |e| e.name.in?(names.map!(&:to_s)) })
end

#not_nestedAlchemy::ElementRepository

All not nested top level elements

Returns:

  • (Alchemy::ElementRepository)

86
87
88
# File 'app/models/alchemy/elements_repository.rb', line 86

def not_nested
  self.class.new(select { |e| e.parent_element_id.nil? })
end

#offset(offset) ⇒ Alchemy::ElementRepository

Elements off setted by

Returns:

  • (Alchemy::ElementRepository)

104
105
106
# File 'app/models/alchemy/elements_repository.rb', line 104

def offset(offset)
  self.class.new elements[offset.to_i..-1]
end

#randomAlchemy::ElementRepository

Elements in random order

Returns:

  • (Alchemy::ElementRepository)

98
99
100
# File 'app/models/alchemy/elements_repository.rb', line 98

def random
  self.class.new Array(elements).shuffle
end

#reverseAlchemy::ElementRepository

Elements in reversed order

Returns:

  • (Alchemy::ElementRepository)

92
93
94
# File 'app/models/alchemy/elements_repository.rb', line 92

def reverse
  self.class.new elements.reverse
end

#unfixedAlchemy::ElementRepository

All not fixed elements

Returns:

  • (Alchemy::ElementRepository)

68
69
70
# File 'app/models/alchemy/elements_repository.rb', line 68

def unfixed
  self.class.new reject(&:fixed)
end

#visibleAlchemy::ElementRepository

All visible elements

Returns:

  • (Alchemy::ElementRepository)

21
22
23
# File 'app/models/alchemy/elements_repository.rb', line 21

def visible
  self.class.new select(&:public)
end

#where(attrs) ⇒ Alchemy::ElementRepository

Filter elements by given attribute and value

Parameters:

  • (Array|Hash)

Returns:

  • (Alchemy::ElementRepository)

42
43
44
45
46
47
48
49
50
# File 'app/models/alchemy/elements_repository.rb', line 42

def where(attrs)
  self.class.new(
    select do |element|
      attrs.all? do |attr, value|
        element.public_send(attr) == value
      end
    end
  )
end