Sha256: b1c42e2fb694bbfc46e94ae80307fe08c71ebbce714ff344e720c34e1bb01428

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module Luruju

  module JudeApi

    class JudeBase

      include DynamicMethod

      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 = Inflector.pluralize(clazz)
        ope = param.instance_of?(Regexp) ? "=~" : "=="
        instance_eval("#{a_getter}.find{|i|i.#{attr} #{ope} param}");
      end

      def dyna_children_access(method, *args)
        raise IsNotMyRole unless self.class.method_defined?(:owned_elements)
        api_name = "Jude" + Inflector.classify(method.to_s)
        raise IsNotMyRole unless JudeApi.constants.include?(api_name)
        owned_elements.select{|p| p.instance_of?(JudeApi.module_eval(api_name))}
      end
      
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luruju-0.0.1 lib/luruju/add_useful_method.rb