Module: DefDsl

Defined in:
lib/def_dsl.rb,
lib/def_dsl/version.rb

Defined Under Namespace

Modules: Lazy, So

Constant Summary collapse

VERSION =
"0.0.5"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_modules(klass, trash = []) ⇒ Array<Class, Module>

Parameters:

  • klass
  • trash (Class, Module, Array<Class, Module>) (defaults to: [])

    optional black list of entities to visit and return

Returns:

  • (Array<Class, Module>)


80
81
82
83
84
85
86
87
88
# 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) ⇒ Proc

implicit way to use instance_eval, by passing block with zero arity

Parameters:

  • block (Proc)

Returns:

  • (Proc)


94
95
96
97
# 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 = []) ⇒ Array<Class, Module>

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

Parameters:

  • given (Class, Module)

Returns:

  • (Array<Class, Module>)

    that explicitly defined in given



64
65
66
67
68
69
70
71
72
73
# 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 = []) {|Class, Module| ... } ⇒ Object

traverse tree of classes and modules, acts arity_based

Parameters:

  • target (Class, Module)

    root of search tree

  • trash (Array<Class, Module>) (defaults to: [])

    black list of entities to visit and yield

Yields:

  • (Class, Module)

    once per entity



105
106
107
# File 'lib/def_dsl.rb', line 105

def traverse target, trash = [], &block
  all_modules(target, trash).each &DefDsl.arity_based(&block)
end

Instance Method Details

#def_dsl(trash = []) ⇒ Object

todo: test so, trash(?)



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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