Module: Depict::Presentable::ClassMethods

Defined in:
lib/depict/presentable.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/depict/presentable.rb', line 54

def method_missing(method, *args)
   names = depict_presentations.keys

   match = /^new_from_(#{names.join("|")})_presentation$/.match(method.to_s)

   if match
      new_from_presentation(match[1].to_sym, *args)
   else
      super(method, *args)
   end
end

Instance Method Details

#define_presentation(name, options = {}, &block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/depict/presentable.rb', line 17

def define_presentation(name, options={}, &block)
   if options[:extends]
      extends_presenter = depict_presentations[options[:extends]]

      if extends_presenter == nil
         raise UndefinedPresentationError.new("undefined presentation: #{options[:extends]}")
      end
   else
      extends_presenter = Depict::Presenter
   end

   depict_presentations[name] = extends_presenter.define(&block)
end

#depict_presentationsObject



13
14
15
# File 'lib/depict/presentable.rb', line 13

def depict_presentations
   @depict_presentations ||= {}
end

#new_from_presentation(name, attrs) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/depict/presentable.rb', line 31

def new_from_presentation(name, attrs)
   object = self.new

   presenter_class = depict_presentations[name]

   if presenter_class
      presenter = presenter_class.new(object)
      presenter.attributes = attrs
   end

   object
end

#respond_to?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
51
52
# File 'lib/depict/presentable.rb', line 44

def respond_to?(method, include_private=false)
   names = depict_presentations.keys

   if /^new_from_(#{names.join("|")})_presentation$/ =~ method.to_s
      true
   else
      super(method, include_private)
   end
end