Class: ActionView::Helpers::JavaScriptCollectionProxy

Inherits:
JavaScriptProxy show all
Defined in:
lib/action_view/helpers/prototype_helper.rb

Overview

:nodoc:

Direct Known Subclasses

JavaScriptElementCollectionProxy

Constant Summary collapse

ENUMERABLE_METHODS_WITH_RETURN =
[:all, :any, :collect, :map, :detect, :find, :find_all, :select, :max, :min, :partition, :reject, :sort_by, :in_groups_of, :each_slice]
ENUMERABLE_METHODS =
ENUMERABLE_METHODS_WITH_RETURN + [:each]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, pattern) ⇒ JavaScriptCollectionProxy

Returns a new instance of JavaScriptCollectionProxy.



1168
1169
1170
# File 'lib/action_view/helpers/prototype_helper.rb', line 1168

def initialize(generator, pattern)
  super(generator, @pattern = pattern)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *arguments, &block) ⇒ Object (private)



1215
1216
1217
1218
1219
1220
1221
1222
1223
# File 'lib/action_view/helpers/prototype_helper.rb', line 1215

def method_missing(method, *arguments, &block)
  if ENUMERABLE_METHODS.include?(method)
    returnable = ENUMERABLE_METHODS_WITH_RETURN.include?(method)
    variable   = arguments.first if returnable
    enumerate(method, {:variable => (arguments.first if returnable), :return => returnable, :yield_args => %w(value index)}, &block)
  else
    super
  end
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



1165
1166
1167
# File 'lib/action_view/helpers/prototype_helper.rb', line 1165

def generator
  @generator
end

Instance Method Details

#each_slice(variable, number, &block) ⇒ Object



1172
1173
1174
1175
1176
1177
1178
1179
# File 'lib/action_view/helpers/prototype_helper.rb', line 1172

def each_slice(variable, number, &block)
  if block
    enumerate :eachSlice, :variable => variable, :method_args => [number], :yield_args => %w(value index), :return => true, &block
  else
    add_variable_assignment!(variable)
    append_enumerable_function!("eachSlice(#{number.to_json});")
  end
end

#grep(variable, pattern, &block) ⇒ Object



1181
1182
1183
# File 'lib/action_view/helpers/prototype_helper.rb', line 1181

def grep(variable, pattern, &block)
  enumerate :grep, :variable => variable, :return => true, :method_args => [pattern], :yield_args => %w(value index), &block
end

#in_groups_of(variable, number, fill_with = nil) ⇒ Object



1185
1186
1187
1188
1189
1190
# File 'lib/action_view/helpers/prototype_helper.rb', line 1185

def in_groups_of(variable, number, fill_with = nil)
  arguments = [number]
  arguments << fill_with unless fill_with.nil?
  add_variable_assignment!(variable)
  append_enumerable_function!("inGroupsOf(#{arguments_for_call arguments});")
end

#inject(variable, memo, &block) ⇒ Object



1192
1193
1194
# File 'lib/action_view/helpers/prototype_helper.rb', line 1192

def inject(variable, memo, &block)
  enumerate :inject, :variable => variable, :method_args => [memo], :yield_args => %w(memo value index), :return => true, &block
end

#pluck(variable, property) ⇒ Object



1196
1197
1198
1199
# File 'lib/action_view/helpers/prototype_helper.rb', line 1196

def pluck(variable, property)
  add_variable_assignment!(variable)
  append_enumerable_function!("pluck(#{property.to_json});")
end

#zip(variable, *arguments, &block) ⇒ Object



1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
# File 'lib/action_view/helpers/prototype_helper.rb', line 1201

def zip(variable, *arguments, &block)
  add_variable_assignment!(variable)
  append_enumerable_function!("zip(#{arguments_for_call arguments}")
  if block
    function_chain[-1] += ", function(array) {"
    yield ActiveSupport::JSON::Variable.new('array')
    add_return_statement!
    @generator << '});'
  else
    function_chain[-1] += ');'
  end
end