Class: ActionView::Helpers::JavaScriptCollectionProxy

Inherits:
JavaScriptProxy
  • Object
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

Methods inherited from JavaScriptProxy

#is_a?

Constructor Details

#initialize(generator, pattern) ⇒ JavaScriptCollectionProxy

Returns a new instance of JavaScriptCollectionProxy.



775
776
777
# File 'lib/action_view/helpers/prototype_helper.rb', line 775

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)



822
823
824
825
826
827
828
829
830
# File 'lib/action_view/helpers/prototype_helper.rb', line 822

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 => variable, :return => returnable, :yield_args => %w(value index)}, &block)
  else
    super
  end
end

Instance Attribute Details

#generatorObject (readonly)

Returns the value of attribute generator.



772
773
774
# File 'lib/action_view/helpers/prototype_helper.rb', line 772

def generator
  @generator
end

Instance Method Details

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



779
780
781
782
783
784
785
786
# File 'lib/action_view/helpers/prototype_helper.rb', line 779

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(#{::ActiveSupport::JSON.encode(number)});")
  end
end

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



788
789
790
# File 'lib/action_view/helpers/prototype_helper.rb', line 788

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

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



792
793
794
795
796
797
# File 'lib/action_view/helpers/prototype_helper.rb', line 792

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



799
800
801
# File 'lib/action_view/helpers/prototype_helper.rb', line 799

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



803
804
805
806
# File 'lib/action_view/helpers/prototype_helper.rb', line 803

def pluck(variable, property)
  add_variable_assignment!(variable)
  append_enumerable_function!("pluck(#{::ActiveSupport::JSON.encode(property)});")
end

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



808
809
810
811
812
813
814
815
816
817
818
819
# File 'lib/action_view/helpers/prototype_helper.rb', line 808

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 JsonLiteral.new('array')
    add_return_statement!
    @generator << '});'
  else
    function_chain[-1] += ');'
  end
end