Class: SurveyGizmo::Collection

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

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &blk) ⇒ Object



33
34
35
36
# File 'lib/survey_gizmo/collection.rb', line 33

def method_missing(meth, *args, &blk)
  lazy_load
  @collection.send(meth, *args, &blk)
end

Instance Method Details

#eachObject



23
24
25
26
27
28
29
30
# File 'lib/survey_gizmo/collection.rb', line 23

def each
  lazy_load
  if block_given?
    @collection.each{ |o| yield(o) }
  else
    @collection.each
  end
end

#lengthObject



18
19
20
# File 'lib/survey_gizmo/collection.rb', line 18

def length
  @array.length
end

#modelObject

Raises:

  • (NameError)


39
40
41
42
43
44
45
46
# File 'lib/survey_gizmo/collection.rb', line 39

def model
  return @model if defined?(@model)
  return (@model = options[:target]) if options[:target].is_a?(Class)
  name_string = options[:target].is_a?(Symbol) ? ActiveSupport::Inflector.classify(options[:target]) : options[:target]
  @model = name_string[/::/] ? Object.const_get?(name_string) : Resource.descendants.detect{ |d| ActiveSupport::Inflector.demodulize(d.name) == name_string }
  raise NameError, "#{name_string} is not a descendant of SurveyGizmo::Resource" unless @model
  @model
end