Module: NanDoc::Helpers::NanDocHelpers::BlockAttrAccessor

Included in:
SiteMap, TopNav
Defined in:
lib/nandoc/helpers.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#block_attr_accessorsObject

Returns the value of attribute block_attr_accessors.



6
7
8
# File 'lib/nandoc/helpers.rb', line 6

def block_attr_accessors
  @block_attr_accessors
end

Class Method Details

.extended(mod) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/nandoc/helpers.rb', line 8

def extended mod
  mod.instance_variable_set('@block_attr_accessors', []) unless
    mod.instance_variable_defined?('@block_attr_accessors')
  mod.send(:define_method, :block_attr_accessor_init) do
    self.class.block_attr_accessors.each do |name|
      instance_variable_set("@#{name}_proc",nil) unless
        instance_variable_defined?("@#{name}_proc")
    end
  end
end

Instance Method Details

#block_attr_accessor(*names) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/nandoc/helpers.rb', line 19

def block_attr_accessor *names
  names.each do |setter_name|
    @block_attr_accessors.push(setter_name) # etc
    attr_name = "@#{setter_name}_proc"
    getter_name = "#{setter_name}_proc"
    define_method(setter_name) do |&block|
      raise ArgumentError.new(
        "no block given for #{self.class}##{setter_name}"
      ) unless block
      instance_variable_set attr_name, block
    end
    define_method(getter_name) do
      instance_variable_get attr_name
    end
  end
end