Class: Artdeco::Decorator

Inherits:
Object
  • Object
show all
Includes:
DecoratorMethods
Defined in:
lib/artdeco.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from DecoratorMethods

#decorate

Constructor Details

#initialize(*args) ⇒ Decorator

Args may be either the params hash of the request or an object which responds to :params and optionally to :view_context, e.g. a controller instance If a view_context is given it will be accessible in various blocks by calling :h



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/artdeco.rb', line 54

def initialize *args
  opts = args.extract_options!
  
  @decorator_classes = ([opts.delete(:decorators)] + [opts.delete(:decorator)]).flatten.compact
  @decorator_classes = nil if @decorator_classes.empty? # required for #decorate

  case args.size
  when 0
    @view_context = opts.delete :view_context 
    @params = opts.delete(:params){opts}
  when 1
    arg = args.first
    @view_context = arg.respond_to?(:view_context) ? arg.view_context : nil

    if arg.respond_to? :params
      @params = arg.params.symbolize_keys.merge(opts)
    else
      raise ArgumentError, 'argument must respond_to :params'
    end
  else
    raise ArgumentError, 'too many arguments' if args.size > 1
  end
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



48
49
50
# File 'lib/artdeco.rb', line 48

def params
  @params
end

#view_contextObject (readonly) Also known as: h

Returns the value of attribute view_context.



48
49
50
# File 'lib/artdeco.rb', line 48

def view_context
  @view_context
end

Instance Method Details

#eval(data, model = nil) ⇒ Object

evaluate data (string or proc) if model is provided it will accessible in evaluated data



80
81
82
83
84
85
86
87
# File 'lib/artdeco.rb', line 80

def eval data, model = nil
  case data
  when Proc
    (model ? model : self).instance_exec(&data)
  else
    data
  end
end