Sha256: 79eddfc57e93ef784a58380a9bbb82ff67f84d9803261534d05264f11008df4e

Contents?: true

Size: 1.74 KB

Versions: 1

Compression:

Stored size: 1.74 KB

Contents

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
        return self.kind_of?(eval("JudeApi::#{$1.classify.jude_module}")) 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
      
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
luruju-0.1.0 lib/luruju/jude_api/add_dynamic_method.rb