Class: Dao::Presenter

Inherits:
Object
  • Object
show all
Includes:
InstanceExec
Defined in:
lib/dao/presenter.rb

Constant Summary collapse

DefaultFormat =
lambda{|value| value}

Constants included from InstanceExec

InstanceExec::Code

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InstanceExec

extend_object, included

Constructor Details

#initialize(*args, &block) ⇒ Presenter

Returns a new instance of Presenter.



14
15
16
17
# File 'lib/dao/presenter.rb', line 14

def initialize(*args, &block)
  @result = args.shift if args.first.is_a?(Result)
  @formats = Map.new
end

Instance Attribute Details

#formatsObject

Returns the value of attribute formats.



12
13
14
# File 'lib/dao/presenter.rb', line 12

def formats
  @formats
end

#resultObject

Returns the value of attribute result.



11
12
13
# File 'lib/dao/presenter.rb', line 11

def result
  @result
end

Class Method Details

.for(*args, &block) ⇒ Object



6
7
8
# File 'lib/dao/presenter.rb', line 6

def for(*args, &block)
  new(*args, &block)
end

Instance Method Details

#==(other) ⇒ Object



79
80
81
# File 'lib/dao/presenter.rb', line 79

def ==(other)
  result == other.result
end

#attr_for(string) ⇒ Object



125
126
127
# File 'lib/dao/presenter.rb', line 125

def attr_for(string)
  slug_for(string).gsub(/_/, '-')
end

#class_for(keys, klass = nil) ⇒ Object



88
89
90
91
92
93
94
95
96
# File 'lib/dao/presenter.rb', line 88

def class_for(keys, klass = nil)
  klass = 
    if result.errors.on?(keys)
      [klass, 'dao', 'errors'].compact.join(' ')
    else
      [klass, 'dao'].compact.join(' ')
    end
  klass
end

#dataObject



71
72
73
# File 'lib/dao/presenter.rb', line 71

def data
  result.data
end

#error_for(keys, klass = nil) ⇒ Object



98
99
100
101
102
# File 'lib/dao/presenter.rb', line 98

def error_for(keys, klass = nil)
  if result.errors.on?(keys)
    result.errors.get(keys)
  end
end

#errorsObject



75
76
77
# File 'lib/dao/presenter.rb', line 75

def errors
  result.errors
end

#extend(*args, &block) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/dao/presenter.rb', line 27

def extend(*args, &block)
  return super if block.nil?
  singleton_class =
    class << self
      self
    end
  singleton_class.module_eval(&block)
end

#format(list_of_keys, &block) ⇒ Object



65
66
67
68
69
# File 'lib/dao/presenter.rb', line 65

def format(list_of_keys, &block)
  Array(list_of_keys).each do |keys|
    formats.set(keys, block)
  end
end

#format_for(keys) ⇒ Object



61
62
63
# File 'lib/dao/presenter.rb', line 61

def format_for(keys)
  formats.get(keys) || DefaultFormat
end

#id_for(keys) ⇒ Object



83
84
85
86
# File 'lib/dao/presenter.rb', line 83

def id_for(keys)
  id = [result.path, keys.join('-')].compact.join('_')
  slug_for(id)
end

#options_for(*hashes) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/dao/presenter.rb', line 109

def options_for(*hashes)
  map = Map.new
  hashes.flatten.each do |h|
    h.each{|k,v| map[attr_for(k)] = v unless v.nil?}
  end
  map
end

#slug_for(string) ⇒ Object



117
118
119
120
121
122
123
# File 'lib/dao/presenter.rb', line 117

def slug_for(string)
  string = string.to_s
  words = string.to_s.scan(%r/\w+/)
  words.map!{|word| word.gsub(%r/[^0-9a-zA-Z_:-]/, '')}
  words.delete_if{|word| word.nil? or word.strip.empty?}
  words.join('-').downcase.sub(/_+$/, '')
end

#tag(*args, &block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dao/presenter.rb', line 36

def tag(*args, &block)
  options = Dao.options_for!(args)
  args.push(:div) if args.empty?
  tagname = args.shift
  keys = args

  tag_method = "#{ tagname }_"

  id = options.delete(:id) || id_for(keys)
  klass = class_for(keys, options.delete(:class))
  error = error_for(keys, options.delete(:error))

  tag_options =
    options_for(options, :id => id, :class => klass, :data_error => error)

  value = value_for(keys)
  tag_value = instance_exec(value, &format_for(keys))

  send(tag_method, tag_options){ tag_value }
end

#value_for(keys) ⇒ Object



104
105
106
107
# File 'lib/dao/presenter.rb', line 104

def value_for(keys)
  return nil unless data.has?(keys)
  value = Tagz.escapeHTML(data.get(keys))
end