Class: Alchemy::ElementsRepository
- Inherits:
-
Object
- Object
- Alchemy::ElementsRepository
- 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
-
.none ⇒ Object
An empty set of elements.
Instance Method Summary collapse
- #children_of(parent) ⇒ Object
- #each ⇒ Object
-
#excluded(*names) ⇒ Alchemy::ElementRepository
All elements excluding those wth given name(s).
-
#expanded ⇒ Alchemy::ElementRepository
All expanded elements.
-
#fixed ⇒ Alchemy::ElementRepository
All fixed elements.
-
#folded ⇒ Alchemy::ElementRepository
All folded elements.
-
#hidden ⇒ Alchemy::ElementRepository
All not fixed elements.
-
#initialize(elements) ⇒ ElementsRepository
constructor
A new instance of ElementsRepository.
-
#limit(limit) ⇒ Alchemy::ElementRepository
Elements limitted by.
-
#named(*names) ⇒ Alchemy::ElementRepository
All elements with given name(s).
-
#not_nested ⇒ Alchemy::ElementRepository
All not nested top level elements.
-
#offset(offset) ⇒ Alchemy::ElementRepository
Elements off setted by.
-
#random ⇒ Alchemy::ElementRepository
Elements in random order.
-
#reverse ⇒ Alchemy::ElementRepository
Elements in reversed order.
-
#unfixed ⇒ Alchemy::ElementRepository
All not fixed elements.
-
#visible ⇒ Alchemy::ElementRepository
All visible elements.
-
#where(attrs) ⇒ Alchemy::ElementRepository
Filter elements by given attribute and value.
Constructor Details
#initialize(elements) ⇒ ElementsRepository
Returns a new instance of ElementsRepository.
15 16 17 |
# File 'app/models/alchemy/elements_repository.rb', line 15 def initialize(elements) @elements = elements.to_a end |
Class Method Details
.none ⇒ Object
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 ⇒ Object
118 119 120 |
# File 'app/models/alchemy/elements_repository.rb', line 118 def each(&) elements.each(&) end |
#excluded(*names) ⇒ Alchemy::ElementRepository
All elements excluding those wth given name(s)
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 |
#expanded ⇒ Alchemy::ElementRepository
All expanded elements
80 81 82 |
# File 'app/models/alchemy/elements_repository.rb', line 80 def self.class.new reject(&:folded) end |
#fixed ⇒ Alchemy::ElementRepository
All fixed elements
62 63 64 |
# File 'app/models/alchemy/elements_repository.rb', line 62 def fixed self.class.new select(&:fixed) end |
#folded ⇒ Alchemy::ElementRepository
All folded elements
74 75 76 |
# File 'app/models/alchemy/elements_repository.rb', line 74 def folded self.class.new select(&:folded) end |
#hidden ⇒ Alchemy::ElementRepository
All not fixed elements
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
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)
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_nested ⇒ Alchemy::ElementRepository
All not nested top level elements
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
104 105 106 |
# File 'app/models/alchemy/elements_repository.rb', line 104 def offset(offset) self.class.new elements[offset.to_i..] end |
#random ⇒ Alchemy::ElementRepository
Elements in random order
98 99 100 |
# File 'app/models/alchemy/elements_repository.rb', line 98 def random self.class.new Array(elements).shuffle end |
#reverse ⇒ Alchemy::ElementRepository
Elements in reversed order
92 93 94 |
# File 'app/models/alchemy/elements_repository.rb', line 92 def reverse self.class.new elements.reverse end |
#unfixed ⇒ Alchemy::ElementRepository
All not fixed elements
68 69 70 |
# File 'app/models/alchemy/elements_repository.rb', line 68 def unfixed self.class.new reject(&:fixed) end |
#visible ⇒ Alchemy::ElementRepository
All visible elements
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
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 |