Class: Lafcadio::ObjectStore::MethodDispatch

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(orig_method, *other_args) ⇒ MethodDispatch

Returns a new instance of MethodDispatch.



747
748
749
750
751
752
753
# File 'lib/lafcadio/objectStore.rb', line 747

def initialize( orig_method, *other_args )
	@orig_method = orig_method
	@orig_args = other_args
	@maybe_proc = @orig_args.shift if @orig_args.size > 0
	@methodName = orig_method.id2name
	dispatch_method
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



745
746
747
# File 'lib/lafcadio/objectStore.rb', line 745

def args
  @args
end

#symbolObject (readonly)

Returns the value of attribute symbol.



745
746
747
# File 'lib/lafcadio/objectStore.rb', line 745

def symbol
  @symbol
end

Instance Method Details

#camel_case_method_nameObject



755
756
757
# File 'lib/lafcadio/objectStore.rb', line 755

def camel_case_method_name
	@orig_method.id2name.underscore_to_camel_case
end

#dispatch(object_store) ⇒ Object



759
760
761
762
# File 'lib/lafcadio/objectStore.rb', line 759

def dispatch( object_store )
	target = ( @target or object_store )
	target.send( @symbol, *@args )
end

#dispatch_allObject



764
765
766
767
# File 'lib/lafcadio/objectStore.rb', line 764

def dispatch_all
	@symbol = :all
	@args = [ @domain_class ]
end

#dispatch_domain_class_get(searchTerm, fieldName) ⇒ Object



769
770
771
772
773
# File 'lib/lafcadio/objectStore.rb', line 769

def dispatch_domain_class_get( searchTerm, fieldName )
	@symbol = :get
	@args = [ searchTerm, fieldName ]
	@target = @domain_class
end

#dispatch_get_map_object(domain_class) ⇒ Object



775
776
777
778
# File 'lib/lafcadio/objectStore.rb', line 775

def dispatch_get_map_object( domain_class )
	@symbol = :get_map_object
	@args = [ domain_class, @orig_args[0], @orig_args[1] ]
end

#dispatch_methodObject



805
806
807
808
809
810
811
812
813
814
815
# File 'lib/lafcadio/objectStore.rb', line 805

def dispatch_method
	unless ( dispatch_singular )
		domain_class_name = camel_case_method_name.singular
		begin
			@domain_class = Module.by_name domain_class_name
			dispatch_plural
		rescue NameError
			# skip it
		end
	end
end

#dispatch_pluralObject



780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'lib/lafcadio/objectStore.rb', line 780

def dispatch_plural
	if @orig_args.size == 0 && @maybe_proc.nil?
		dispatch_all
	else
		searchTerm, fieldName = @orig_args[0..1]
		if searchTerm.nil? && @maybe_proc.nil? && fieldName.nil?
			raise_plural_needs_field_arg_if_first_arg_nil
		elsif !@maybe_proc.nil? && searchTerm.nil?
			dispatch_plural_by_query_block
		elsif @maybe_proc.nil? && ( !( searchTerm.nil? && fieldName.nil? ) )
			dispatch_domain_class_get( searchTerm, fieldName )
		else
			raise_plural_cant_have_both_query_block_and_search_term
		end
	end
end

#dispatch_plural_by_query_blockObject



797
798
799
800
801
802
803
# File 'lib/lafcadio/objectStore.rb', line 797

def dispatch_plural_by_query_block
	inferrer = Query::Inferrer.new( @domain_class ) { |obj|
		@maybe_proc.call( obj )
	}
	@symbol = :query
	@args = [ inferrer.execute ]
end

#dispatch_singularObject



817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
# File 'lib/lafcadio/objectStore.rb', line 817

def dispatch_singular
	begin
		d_class_name = @orig_method.id2name.underscore_to_camel_case
		domain_class = Module.by_name d_class_name
		if @orig_args[0].class <= Integer
			@symbol = :get
			@args = [ domain_class, @orig_args[0] ]
		elsif @orig_args[0].class <= DomainObject
			dispatch_get_map_object domain_class
		elsif @orig_args.empty?
			@symbol = :get
		end
		true
	rescue NameError
		false
	end
end

#raise_plural_cant_have_both_query_block_and_search_termObject

Raises:

  • (ArgumentError)


841
842
843
844
845
846
# File 'lib/lafcadio/objectStore.rb', line 841

def raise_plural_cant_have_both_query_block_and_search_term
	raise(
		ArgumentError, "Shouldn't send both a query block and a search term",
		caller
	)
end

#raise_plural_needs_field_arg_if_first_arg_nilObject

Raises:

  • (ArgumentError)


835
836
837
838
839
# File 'lib/lafcadio/objectStore.rb', line 835

def raise_plural_needs_field_arg_if_first_arg_nil
	msg = "ObjectStore\##{ @orig_method } needs a field name as its " +
	      "second argument if its first argument is nil"
	raise( ArgumentError, msg, caller )
end