Class: Stencil::View

Inherits:
Object
  • Object
show all
Defined in:
lib/stencil/view.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ View

Returns a new instance of View.



92
93
94
# File 'lib/stencil/view.rb', line 92

def initialize(template)
  @template = template
end

Instance Attribute Details

#templateObject (readonly)

Returns the value of attribute template.



96
97
98
# File 'lib/stencil/view.rb', line 96

def template
  @template
end

Instance Method Details

#render_item(data, item) ⇒ Object



112
113
114
# File 'lib/stencil/view.rb', line 112

def render_item(data, item)
  item
end

#render_list(data, list) ⇒ Object



106
107
108
109
110
# File 'lib/stencil/view.rb', line 106

def render_list(data, list)
  list.map do |item|
    render_sub(data, item)
  end
end

#render_map(data, hash) ⇒ Object



98
99
100
101
102
103
104
# File 'lib/stencil/view.rb', line 98

def render_map(data, hash)
  rendered = {}
  hash.each_pair do |key, value|
    rendered[key.to_s] = render_sub(data, value)
  end
  rendered
end

#render_sub(data, template) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/stencil/view.rb', line 116

def render_sub(data, template)
  case template
  when Hash
    render_map(data, template)
  when Array
    render_list(data, template)
  when Capsule
    render_sub(data, template.render(data))
  else 
    render_item(data, template)
  end
end

#viewset(data) ⇒ Object



129
130
131
# File 'lib/stencil/view.rb', line 129

def viewset(data)
  render_sub(data, @template)
end