class Partializer::Collection

Attributes

action[R]
hashie[R]
name[R]
ns[R]

Public Class Methods

new(name, *items) click to toggle source
# File lib/partializer/collection.rb, line 8
def initialize name, *items
  hash = items.extract_options!      
  partials << partial_keys(items)
  self.name = name.to_s
  @hashie = Hashie::Mash.new resolve(hash) unless hash.empty?
  set_names unless hashie.nil?
end

Public Instance Methods

each() { |partial| ... } click to toggle source
# File lib/partializer/collection.rb, line 16
def each &block
  partials.each {|partial| yield partial }      
end
partials() click to toggle source
# File lib/partializer/collection.rb, line 20
def partials
  @partials ||= Partializer::Partials.new
end
path() click to toggle source
# File lib/partializer/collection.rb, line 24
def path
  [ns, action, name.gsub('.', '/')].compact.join('/')
end
Also aliased as: to_partial_path
set_context(ns, action) click to toggle source
# File lib/partializer/collection.rb, line 30
def set_context ns, action
  @ns, @action = [ns, action]
  partials.list.each {|p| p.send :set_context, ns, action }
end
to_partial_path()
Alias for: path

Protected Instance Methods

method_missing(meth_name, *args, &block) click to toggle source
# File lib/partializer/collection.rb, line 93
def method_missing meth_name, *args, &block
  hashie.send(meth_name)
end
name=(name) click to toggle source
# File lib/partializer/collection.rb, line 37
def name= name
  @name = name
  partials.list.each do |p|
    p.path = name
  end
end
partial_keys(*args) click to toggle source
# File lib/partializer/collection.rb, line 82
def partial_keys *args
  args.flatten.map do |item| 
    case item
    when Hash
      item.keys.map(&:to_sym)
    when String, Symbol
      item.to_s.sub(/^_/, '').to_sym
    end
  end.flatten.compact.uniq
end
resolve(hash) click to toggle source
# File lib/partializer/collection.rb, line 56
def resolve hash
  hash.inject({}) do |res, pair|
    key = pair.first
    item = pair.last
    key = key.to_s.sub(/^_/, '').to_sym

    value = resolve_value key, item        
    
    res[key.to_s.sub(/^_/, '').to_sym] = value
    res
  end
end
resolve_value(key, item) click to toggle source
# File lib/partializer/collection.rb, line 69
def resolve_value key, item
  case item
  when Partializer::Collection
    item.hashie ? item.send(key) : item
  when Symbol, String
    Partializer::Collection.new('noname', item)
  when Hash
    item.values.first
  else
    raise ArgumentError, "cant resolve: #{item.inspect}"
  end
end
set_names() click to toggle source
# File lib/partializer/collection.rb, line 44
def set_names
  hashie.keys.each do |key|
    if key.kind_of?(String)
      h = hashie.send(key)
      if h.kind_of? Partializer::Collection
        h.send :name=, "#{name}.#{key}"
        h.set_names unless h.hashie.nil?
      end
    end
  end
end