Module: Contender::CopyOnWriteStructure::ClassMethods

Defined in:
lib/contender/copy_on_write_structure.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#delegated_typeObject (readonly)

Returns the value of attribute delegated_type.



56
57
58
# File 'lib/contender/copy_on_write_structure.rb', line 56

def delegated_type
  @delegated_type
end

Instance Method Details

#accessors(*args) ⇒ Object Also known as: accessor



12
13
14
15
16
17
18
19
20
# File 'lib/contender/copy_on_write_structure.rb', line 12

def accessors(*args)
  @accessors ||= Set.new

  args.each do |method|
    @accessors.add method

    def_delegator :current_reference, method
  end
end

#accessors!Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/contender/copy_on_write_structure.rb', line 22

def accessors!
  methods = Set.new(delegated_type.instance_methods(false))

  methods = methods - @accessors if @accessors
  methods = methods - @mutators if @mutators

  methods.each do |method|
    def_delegator :current_reference, method
  end
end

#delegate_to(type) ⇒ Object



52
53
54
# File 'lib/contender/copy_on_write_structure.rb', line 52

def delegate_to(type)
  @delegated_type = type
end

#mutators(*args) ⇒ Object Also known as: mutator



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/contender/copy_on_write_structure.rb', line 33

def mutators(*args)
  @mutators ||= Set.new

  args.each do |method|
    @mutators.add method

    class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
      def #{method}(*args, &block)
        update_reference do |current|
          current.#{method}(*args, &block)
        end
      end
    RUBY_EVAL
  end
end