class Ro::Node::List

Public Class Methods

new(*args, &block) click to toggle source
# File lib/ro/node/list.rb, line 8
def initialize(*args, &block)
  options = Map.options_for!(args)

  root = args.shift || options[:root]
  type = args.shift || options[:type]

  @root = Root.new(root)
  @type = type.nil? ? nil : String(type)
  @index = {}

  block.call(self) if block
end

Public Instance Methods

[](*args, &block) click to toggle source
Calls superclass method
# File lib/ro/node/list.rb, line 53
def [](*args, &block)
  key = args.first

  case key
    when String, Symbol
      if @type.nil?
        type = key.to_s
        list = select{|node| type == node._type}
        list.type = type
        list
      else
        id = Slug.for(key.to_s)
        detect{|node| id == node.id}
      end
    else
      super(*args, &block)
  end
end
_binding() click to toggle source
# File lib/ro/node/list.rb, line 137
def _binding
  Kernel.binding
end
add(node) click to toggle source
# File lib/ro/node/list.rb, line 29
def add(node)
  return nil if node.nil?

  unless index.has_key?(node.identifier)
    push(node)
    index[node.identifier] = node
    node
  else
    false
  end
end
binding() click to toggle source
# File lib/ro/node/list.rb, line 133
def binding
  Kernel.binding
end
find(*args, &block) click to toggle source
# File lib/ro/node/list.rb, line 94
def find(*args, &block)
  case
    when !args.empty? && block
      raise ArgumentError.new
    when args.empty? && block
      detect{|node| node.instance_eval(&block)}

    when args.size == 1
      id = args.first.to_s
      detect{|node| node.id == id}

    when args.size > 1
      where(*args, &block)

    else
      raise ArgumentError.new
  end
end
identifier() click to toggle source
# File lib/ro/node/list.rb, line 113
def identifier
  [root, type].compact.join('/')
end
load(path) click to toggle source
# File lib/ro/node/list.rb, line 25
def load(path)
  add( node = Node.new(path) )
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/ro/node/list.rb, line 119
def method_missing(method, *args, &block)
  Ro.log "Ro::List(#{ identifier })#method_missing(#{ method.inspect }, #{ args.inspect })"

  if @type.nil?
    type = method.to_s
    list = self[type]
    super unless list
    list.empty? ? super : list
  else
    node = self[Slug.for(method, :join => '-')] || self[Slug.for(method, :join => '_')]
    node.nil? ? super : node
  end
end
nodes() click to toggle source
# File lib/ro/node/list.rb, line 21
def nodes
  self
end
select(*args, &block) click to toggle source
Calls superclass method
# File lib/ro/node/list.rb, line 72
def select(*args, &block)
  List.new(root){|list| list.replace(super)}
end
where(*args, &block) click to toggle source
# File lib/ro/node/list.rb, line 76
def where(*args, &block)
  case
    when !args.empty? && block
      raise ArgumentError.new

    when args.empty? && block
      select{|node| node.instance_eval(&block)}

    when !args.empty?
      ids = args.flatten.compact.uniq.map{|arg| Slug.for(arg.to_s)}
      index = ids.inject(Hash.new){|h,id| h.update(id => id)}
      select{|node| index[node.id]}

    else
      raise ArgumentError.new
  end
end