Module: Necromancy::Control

Included in:
Necromancy, Alternative, Applicative, Arrow, Category
Defined in:
lib/necromancy/control.rb

Defined Under Namespace

Modules: Alternative, Applicative, Arrow, Category

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(mod) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/necromancy/control.rb', line 78

def self.extended(mod)
  return unless mod.name and mod.name.start_with? self.name
  mthname = mod.name.sub("#{self.name}::", '')
  define_method mthname do |*args, &block|
    if args.empty?
      branch { include mod }
    else
      branch { include mod; protected *mod.instance_methods }[*args, &block]
    end
  end
end

Instance Method Details

#[](target, *targets) ⇒ Control

Reveals some methods and Creates some aliases.

Examples:

require 'necromancy'
N = Necromancy.Alternative[:<< => :if,
                           :>> => :then,
                           :|  => :else].new
puts (1..100).map &( (N%15).zero? .then(proc{"FizzBuzz"}) .else   \
                     (N%3).zero?  .then(proc{"Fizz"})     .else   \
                     (N%5).zero?  .then(proc{"Buzz"})     .else N )

Parameters:

  • target (Symbol)

    Reveals a method of the symbol.

  • target (Hash)

    Hides all methods of each key and creates aliases from each value to each key.

  • targets (Array)

    A list of Symbol or Hash that will be processing as target.

Returns:



50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/necromancy/control.rb', line 50

def [](target, *targets)
  targets.unshift(target)
  names = targets.select { |t| t.is_a? Symbol }
  aliases = targets.select { |t| t.is_a? Hash }.inject(:merge) || {}
  branch do
    public *names
    aliases.each do |org, als|
      alias_method(als, org)
      protected org
      public als
    end
  end
end

#call(*targets) ⇒ Object

Deprecated.


20
21
22
23
24
25
26
# File 'lib/necromancy/control.rb', line 20

def call(*targets)
  warn <<EOF
Necromancy.Hoge.() deprecated.
Use Necromancy.Hoge().
EOF
  branch { protected *instance_methods }[*targets]
end

#hiding(name, *names) ⇒ Control

Hides some methods.

Examples:

require 'necromancy'
N = Necromancy.Alternative.hiding(:*, :**).new
ary = [1, nil, 2, nil, 3]
ary.map &(N | proc{0}) * 10 # => [10, 0, 20, 0, 3]

Parameters:

  • name (Symbol)

    Hides a method of the name.

  • names (Array)

    A list of a Symbol that will be processing as name.

Returns:



73
74
75
76
# File 'lib/necromancy/control.rb', line 73

def hiding(name, *names)
  names.unshift(name)
  branch { protected *names }
end

#newNecromancy

Creates a Necromancy object including the Necromancy::Control.

Returns:



7
8
9
10
# File 'lib/necromancy/control.rb', line 7

def new
  mod = self
  Class.new(::Necromancy::Necromancy) { include mod }.new
end

#using(*targets) ⇒ Object

Deprecated.


29
30
31
32
33
34
35
# File 'lib/necromancy/control.rb', line 29

def using(*targets)
  warn <<EOF
Necromancy.Hoge.using() deprecated.
Use Necromancy.Hoge[].
EOF
  self[*targets]
end