module DefDsl

Constants

VERSION

Public Class Methods

all_modules(klass, trash = []) click to toggle source

@param klass @param trash [Class, Module, Array<Class, Module>]

optional black list of entities to visit and return

@return [Array<Class, Module>]

# File lib/def_dsl.rb, line 80
def all_modules klass, trash = []
  trash = [*trash]
  trash += [klass]
  children = shallow(klass, trash)
  all = [klass, children.
         map { |x| all_modules x, trash + children }].
         flatten.select { |x| Module === x }
  all
end
arity_based(&block) click to toggle source

implicit way to use instance_eval, by passing block with zero arity @param block [Proc] @return [Proc]

# File lib/def_dsl.rb, line 94
def arity_based &block
  block.arity == 0 ? proc { |obj| obj.instance_eval &block }
                   : block
end
shallow(given, trash = []) click to toggle source

todo: to test optional black list or run mutant :)

@param given [Class, Module] @return [Array<Class, Module>] that explicitly defined in given

# File lib/def_dsl.rb, line 64
def shallow given, trash = []
  if given.class == Module;   given.constants.map { |x| given.const_get x }
  elsif given.class == Class
    here = given.constants.map { |x| given.const_get x }
    sup = given.superclass
    other = sup.constants.map { |x| sup.const_get x }
    here - other
  else []
  end - trash
end
traverse(target, trash = [], &block) click to toggle source

traverse tree of classes and modules, acts {arity_based}

@param target [Class, Module] root of search tree @param trash [Array<Class, Module>] black list of entities to visit and yield @yield [Class, Module] once per entity

# File lib/def_dsl.rb, line 105
def traverse target, trash = [], &block
  all_modules(target, trash).each &DefDsl.arity_based(&block)
end

Public Instance Methods

def_dsl(trash = []) click to toggle source

todo: test so, trash(?)

# File lib/def_dsl.rb, line 38
def def_dsl trash = []
  DefDsl.traverse self, trash do |who|
    who.send :include, So
    who.send :include, Lazy if who.instance_eval { @lazy }

    DefDsl.shallow(who, trash).each do |mod|
      mini = mod.name.underscore # .....
      method = mod.instance_eval { @dsl } ||  mod.name.underscore

      who.module_eval do
        define_method method do |*a,&b|
          so [mini,method],*a,&b # artifact...
        end
      end
      # or check superclass if respond for meta_modules + test
      if who.class == Module
        who.send :module_function, method
      end
    end
  end
end

Private Instance Methods

all_modules(klass, trash = []) click to toggle source

@param klass @param trash [Class, Module, Array<Class, Module>]

optional black list of entities to visit and return

@return [Array<Class, Module>]

# File lib/def_dsl.rb, line 80
def all_modules klass, trash = []
  trash = [*trash]
  trash += [klass]
  children = shallow(klass, trash)
  all = [klass, children.
         map { |x| all_modules x, trash + children }].
         flatten.select { |x| Module === x }
  all
end
arity_based(&block) click to toggle source

implicit way to use instance_eval, by passing block with zero arity @param block [Proc] @return [Proc]

# File lib/def_dsl.rb, line 94
def arity_based &block
  block.arity == 0 ? proc { |obj| obj.instance_eval &block }
                   : block
end
shallow(given, trash = []) click to toggle source

todo: to test optional black list or run mutant :)

@param given [Class, Module] @return [Array<Class, Module>] that explicitly defined in given

# File lib/def_dsl.rb, line 64
def shallow given, trash = []
  if given.class == Module;   given.constants.map { |x| given.const_get x }
  elsif given.class == Class
    here = given.constants.map { |x| given.const_get x }
    sup = given.superclass
    other = sup.constants.map { |x| sup.const_get x }
    here - other
  else []
  end - trash
end
traverse(target, trash = [], &block) click to toggle source

traverse tree of classes and modules, acts {arity_based}

@param target [Class, Module] root of search tree @param trash [Array<Class, Module>] black list of entities to visit and yield @yield [Class, Module] once per entity

# File lib/def_dsl.rb, line 105
def traverse target, trash = [], &block
  all_modules(target, trash).each &DefDsl.arity_based(&block)
end