Class: Partializer

Inherits:
Object
  • Object
show all
Extended by:
Resolver
Includes:
Resolver
Defined in:
lib/partializer.rb,
lib/partializer/engine.rb,
lib/partializer/partial.rb,
lib/partializer/partials.rb,
lib/partializer/resolver.rb,
lib/partializer/collection.rb,
lib/partializer/path_helper.rb,
lib/partializer/view_helper.rb

Defined Under Namespace

Modules: PathHelper, Rails, Resolver, ViewHelper Classes: Collection, InvalidPartialError, Partial, Partials

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Resolver

create_partializer, resolve, resolve_hash, resolve_partializer, resolve_sym, resolve_value

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth_name, *args, &block) ⇒ Object (protected)



89
90
91
92
# File 'lib/partializer.rb', line 89

def method_missing meth_name, *args, &block    
  instance = create_partializer(meth_name)
  instance.respond_to?(:partials) ? instance.partials : instance
end

Class Method Details

.partialize(*args, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/partializer.rb', line 27

def partialize *args, &block
  name = self.name.to_s.underscore

  hashie = args.flatten.inject({}) do |res, arg|
    item = resolve_value(arg)
    res.merge! Hashie::Mash.new arg.to_sym => item
  end

  collection = Partializer::Collection.new name, args, hashie

  define_method :partials do
    @partials ||= collection # Partializer::Collection.new hashie, partials
  end
end

.partializer(name, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/partializer.rb', line 42

def partializer name, options = {}, &block
  default_parent = ::Partializer

  parent = options[:parent] || default_parent
  raise ArgumentError, "Parent must have a partialize method, was: #{parent}" unless parent.respond_to? :partialize

  clazz_name = name.to_s.camelize
  context = self.kind_of?(Class) ? self : self.class

  clazz = parent ? Class.new(parent) : Class.new
  context.const_set clazz_name, clazz          
  clazz = context.const_get(clazz_name)

  clazz.instance_eval(&block) if block_given?
  clazz
end

.partials_for(name, *args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/partializer.rb', line 59

def partials_for name, *args
  hash = args.flatten.inject({}) do |res, arg|
    key = arg.kind_of?(Hash) ? arg.keys.first : arg
    res[key.to_sym] = resolve(arg)
    res
  end
  define_method name do
    Partializer::Collection.new name, hash.keys, hash
  end
end

Instance Method Details

#partials_for(name, *args) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/partializer.rb', line 17

def partials_for name, *args
  hash = args.flatten.inject({}) do |res, arg|
    key = arg.kind_of?(Hash) ? arg.keys.first : arg
    res[key.to_sym] = self.class.resolve(arg)
    res
  end
  Partializer::Collection.new name, hash.keys, hash
end