Class: Widget

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

Constant Summary collapse

VERSION =

–{{{

'0.0.2'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.class_factory(path, &block) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/widgets.rb', line 149

def self.class_factory path, &block
#--{{{
  classes = path.camelize.split %r/::/
  klass = Widget
  classes.each do |const|
    subklass = Class.new Widget
    klass.module_eval{
      remove_const const if const_defined? const
      const_set const, subklass
    }
    klass = subklass
  end
  klass.module_eval &block
  klass
#--}}}
end

.const_for(name) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/widgets.rb', line 38

def const_for name
#--{{{
  consts = name.camelize.split %r/::/
  klass = Widget
  consts.each{|const| klass = klass.const_get(const)}
  klass
#--}}}
end

.for_controller(controller, name, *a, &b) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/widgets.rb', line 12

def for_controller controller, name, *a, &b
#--{{{
  klass = Widget.load name

  returning( klass.allocate ) do |obj|
    obj.instance_eval do
      @controller = controller
      @defined_at = caller
      initialize *a, &b
    end
  end
#--}}}
end

.load(name) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/widgets.rb', line 26

def load name
#--{{{
  lib = File.join libdir, "#{ name }.rb"
  RAILS_ENV == "development" ? Kernel.load(lib) : Kernel.require(lib)
  begin
    const_for name
  rescue
    raise "wtf? Widget '#{ name }' was not defined in #{ lib }"
  end
#--}}}
end

.versionObject



7
# File 'lib/widgets.rb', line 7

def self.version() VERSION end

Instance Method Details

#[](k) ⇒ Object



131
132
133
134
135
# File 'lib/widgets.rb', line 131

def [] k
#--{{{
  send k
#--}}}
end

#[]=(k, v) ⇒ Object



137
138
139
140
141
# File 'lib/widgets.rb', line 137

def []= k, v
#--{{{
  send k, v
#--}}}
end

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



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/widgets.rb', line 70

def configure options = {} , &block
#--{{{
  options.to_options!
  has_attribute = attributes.inject({}){|h,k| h.update k.to_s => true, k.to_sym => true}
  options.each do |k,v|
    if has_attribute[k] 
      send k, v
    else
      attribute k => v
    end
  end
  instance_eval &block if block
  self
#--}}}
end

#inherited_attributesObject



86
87
88
89
90
91
92
93
94
95
# File 'lib/widgets.rb', line 86

def inherited_attributes
#--{{{
  ancestors = self.class.ancestors
  offset = ancestors.index Widget
  ancestors = ancestors[0, offset + 1].compact if offset
  ancestors.reverse.map do |ancestor|
    ancestor.attributes
  end.flatten.uniq
#--}}}
end

#inspectObject



58
59
60
61
62
# File 'lib/widgets.rb', line 58

def inspect
#--{{{
  "#{ name }=#{ super }"
#--}}}
end

#render(options = {}) ⇒ Object Also known as: to_s



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/widgets.rb', line 103

def render options = {}
#--{{{
  raise "recursive render of #{ name }" if in_render?
  in_render true
  begin
    return '' unless show?
    widget = self
    template = widget.template
    locals = widget.to_hash.update options.to_options!
    locals[:widget] = locals['widget'] = locals[:w] = locals['w'] = widget
    controller.instance_eval do
      render_to_string :file => template, :use_full_path => true, :locals => locals
    end
  ensure
    in_render false
  end
#--}}}
end

#templateObject



64
65
66
67
68
# File 'lib/widgets.rb', line 64

def template
#--{{{
  "widgets/#{ name }"
#--}}}
end

#to_hashObject



97
98
99
100
101
# File 'lib/widgets.rb', line 97

def to_hash 
#--{{{
  inherited_attributes.inject({}){|h,a| h.update a.to_sym => send(a)}
#--}}}
end

#widget(name, *a, &b) ⇒ Object



143
144
145
146
147
# File 'lib/widgets.rb', line 143

def widget name, *a, &b
#--{{{
  controller.widget name, *a, &b
#--}}}
end

#with_contentObject Also known as: for_content



123
124
125
126
127
128
# File 'lib/widgets.rb', line 123

def with_content
#--{{{
  content yield 
  render
#--}}}
end