Class: Traitor::Trait

Inherits:
Object
  • Object
show all
Defined in:
lib/traitor/trait.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ Trait

Returns a new instance of Trait.



5
6
7
8
# File 'lib/traitor/trait.rb', line 5

def initialize(&block)
  @module_block = block
  @container = Module.new(&block)
end

Class Method Details

.check_conflict(host_class) ⇒ Object



30
31
32
# File 'lib/traitor/trait.rb', line 30

def self.check_conflict(host_class)
  ConflictCheck.check(host_class)
end

Instance Method Details

#call(host, m, *args, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/traitor/trait.rb', line 18

def call(host, m, *args, &block)
  metaclass = class << host; self; end
  blk = @module_block

  refinement = Module.new do
    refine metaclass, &blk
  end

  host.send :using, refinement
  host.send m, *args, &block
end

#implements?(m) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/traitor/trait.rb', line 14

def implements?(m)
  trait_methods.include?(m)
end

#trait_methodsObject



10
11
12
# File 'lib/traitor/trait.rb', line 10

def trait_methods
  @container.instance_methods(false)
end