module Luruju module JudeApi class JudeBase include DynamicMethod protected def dyna_01_select_xxxs_by_yyy method, *args raise IsNotMyRole unless /^(find_all|select)_(.+?)_by_(.+?)$/ =~ method.to_s a_getter, attr, param = $2, $3, args[0] raise IsNotMyRole unless self.class.method_defined?(a_getter) ope = param.instance_of?(Regexp) ? "=~" : "==" instance_eval "#{a_getter}.select{|i|i.#{attr} #{ope} param}" end def dyna_02_find_xxx_by_yyy method, *args raise IsNotMyRole unless /^find_(.+?)_by_(.+?)$/ =~ method.to_s clazz, attr, param = $1, $2, args[0] a_getter = clazz.pluralize ope = param.instance_of?(Regexp) ? "=~" : "==" instance_eval "#{a_getter}.find{|i|i.#{attr} #{ope} param}" end def dyna_is_kind_of? method, *args raise IsNotMyRole unless /^kind_of_([^?]+?)\?$/ =~ method.to_s c = "JudeApi::Jude#{$1.classify}Methods" c = 'JudeApi::JudeClassMethods' if c == 'JudeApi::JudeClasMethods' return self.kind_of?(eval(c)) rescue false end def dyna_owned_elements method, *args raise IsNotMyRole unless self.class.method_defined?(:owned_elements) raise IsNotMyRole if method.to_s.singularize == method.to_s api_name = "Jude" + method.to_s.classify raise IsNotMyRole unless JudeApi.constants.include?(api_name) owned_elements.select{|p| p.instance_of?(JudeApi.module_eval(api_name))} end def dyna_first_owned_element method, *args raise IsNotMyRole unless self.class.method_defined?(:owned_elements) raise IsNotMyRole if method.to_s.pluralize == method.to_s send(method.to_s.pluralize.to_sym, *args)[0] end def dyna_search method, *args, &block raise IsNotMyRole unless /^search_(.+?)$/ =~ method.to_s JudeBase.search search{|x|x.send("kind_of_#{$1.singularize}?")}, *args, &block end end end end