Class: Moka::SiteTree::NodeArray

Inherits:
Array show all
Defined in:
lib/commands/lib/site_tree.rb

Instance Method Summary collapse

Methods inherited from Array

#extract_options!

Instance Method Details

#find(name) ⇒ Object

Returns the element having the value of the variable name equal to the argument, or nil if such an element does not exist.



210
211
212
213
214
215
216
217
# File 'lib/commands/lib/site_tree.rb', line 210

def find(name)
  self.each do |c|
    if c.respond_to?(:name) and c.name == name.to_s
      return c
    end
  end
  return nil
end

#order_by(ordering_var) ⇒ Object

Orders the elements by a variable. The elements that do not have that variable are placed last.



220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/commands/lib/site_tree.rb', line 220

def order_by(ordering_var)
  return self.sort do |a, b|
    if a.respond_to?(ordering_var.to_sym) and b.respond_to?(ordering_var.to_sym)
      a.send(ordering_var.to_sym) <=> b.send(ordering_var.to_sym)
    else
      if b.respond_to?(ordering_var.to_sym)
        -1
      else
        1
      end
    end
  end
end

#where(conditions) ⇒ Object

Returns another NodeArray containing only the elements satisfying the conditions. conditions can be a Hash containing pairs variable_name => variable_value or a string to be evalued as ruby code.

Examples:

@site.groups.where(=> somevalue, :another_var => another_value)

@site.groups.where(“order < 5”)



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
# File 'lib/commands/lib/site_tree.rb', line 242

def where(conditions)
  found = NodeArray.new
  self.each do |c|
    if conditions.is_a? Hash
      satisfied = true
      conditions.each do |var_name, var_value|
        unless c.respond_to?(var_name.to_sym) and c.send(var_name.to_sym) == var_value
          satisfied = false
          break
        end
      end
      if satisfied
        found << c
      end
    else # conditions should be a string (or something that acts like one)
      if c.instance_eval(conditions)
        found << c
      end
    end
  end
  return found
end