Module: Partializer::Resolver

Included in:
Partializer, Partializer
Defined in:
lib/partializer/resolver.rb

Instance Method Summary collapse

Instance Method Details

#create_partializer(name) ⇒ Object



3
4
5
6
7
# File 'lib/partializer/resolver.rb', line 3

def create_partializer name
  context = self.kind_of?(Class) ? self : self.class
  clazz = "#{context.name}::#{name.to_s.camelize}".constantize
  clazz.new
end

#resolve(arg) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/partializer/resolver.rb', line 9

def resolve arg
  case arg
  when Hash
    resolve_hash(arg)
  when String, Symbol
    resolve_sym(arg)
  when Partializer::Collection
    arg
  else
    raise ArgumentError, "Could not partialize: #{arg}"
  end
end

#resolve_hash(hash) ⇒ Object



22
23
24
# File 'lib/partializer/resolver.rb', line 22

def resolve_hash hash
  Partializer::Collection.new 'hash', hash.keys, hash
end

#resolve_partializer(arg) ⇒ Object



46
47
48
49
50
# File 'lib/partializer/resolver.rb', line 46

def resolve_partializer arg
  arg.sub!(/^_/, '')
  instance = create_partializer(arg)
  {instance.class.name.to_s.demodulize.underscore => instance.partials}
end

#resolve_sym(arg) ⇒ Object

Creates a Collection from a Symbol :gallery gallery -> :gallery partials: [:gallery]



30
31
32
33
34
35
36
37
38
# File 'lib/partializer/resolver.rb', line 30

def resolve_sym arg
  value = resolve_value arg
  arg = arg.to_s.sub(/^_/, '')
  if value.to_s == arg.to_s
    Partializer::Collection.new 'sym', arg
  else
    Partializer::Collection.new 'sym', arg, {arg.to_sym => value}
  end
end

#resolve_value(arg) ⇒ Object

a Partializer or a sym!



41
42
43
44
# File 'lib/partializer/resolver.rb', line 41

def resolve_value arg
  arg = arg.to_s
  arg[0] == '_' ? resolve_partializer(arg) : arg.to_sym
end