Module: ALaChart::ClassMethods

Defined in:
lib/a_la_chart/a_la_chart.rb

Instance Method Summary collapse

Instance Method Details

#a_la_chartObject



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/a_la_chart/a_la_chart.rb', line 96

def a_la_chart
  include ALaChart::InstanceMethods
  
  self.before_filter(:provide_chart_data, :only => [:index, :show])
  
  # TODO: Namespace this stuff??
  # :meta, 
  [:before, :data, :value, :set_chart].each do |method|
    # TODO: swap this in Rails 3 and find a way to attach to helper
    # module_eval <<-end_eval
    master_helper_module.module_eval <<-end_eval
      def #{method}(*args, &block)                    # def data(*args, &block)
        controller.send(%(#{method}), *args, &block)  #   controller.send(%(data), *args, &block)
      end                                             # end
    end_eval
  end
end

#before(*cases, &block) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/a_la_chart/a_la_chart.rb', line 176

def before(*cases, &block)
  if cases.blank?
    define_method("do_before_data") do
      # note: instance_eval binds scope variables, call does not
      instance_eval(&block)
      # block.call(binding)
    end
  else
    cases.each { |caze|
      define_method("do_before_data_#{caze}") do
        # note: instance_eval binds scope variables, call does not
        instance_eval(&block)
        # block.call(binding)
      end
    }
  end
end

#chart(*types, &block) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/a_la_chart/a_la_chart.rb', line 114

def chart(*types, &block)
  for type in types
    define_method("set_chart_#{type}") do
      block.call if block
    end
  end
end

#data(*attrs, &block) ⇒ Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/a_la_chart/a_la_chart.rb', line 122

def data(*attrs, &block)
  options = Hash === attrs.last ? attrs.pop : {}
  cases = attrs
  
  # if attrs.size == 1
  #   attrs = attrs[0]
  #   if attrs.class == Hash
  #     options = attrs
  #   elsif attrs.class == Symbol || attrs.class == String
  #     cases = [attrs]
  #   end
  # elsif attrs.size > 1
  #   if attrs[-1].class == Hash
  #     cases = attrs[0...-1]
  #     options = attrs[-1]
  #   else
  #     cases = attrs[0..-1]
  #   end
  # end
  # 
  # cases ||= []
  # options ||= {}
  
  if cases.blank?
    define_method("get_data") do
      do_before_data if defined?(do_before_data)
      # note: instance_eval binds scope variables, call does not
      instance_eval(&block) || []
      # block.call(binding)
    end
    unless options.blank?
      define_method("get_meta") do
        options
      end
    end
  else
    cases.each { |caze|
      define_method("get_data_#{caze}") do
        if respond_to?("do_before_data_#{caze}")
          return [] unless send("do_before_data_#{caze}")
        elsif defined?(do_before_data)
          return [] unless do_before_data
        end
        # note: instance_eval binds scope variables, call does not
        instance_eval(&block) || []
        # block.call(binding)
      end
      define_method("get_meta_#{caze}") do
        options
      end
    }
  end
end