Class: YARD::Dizby::ConfigAccessHandler

Inherits:
Handlers::Ruby::AttributeHandler
  • Object
show all
Defined in:
lib/yard_dizby/config_access_handler.rb

Instance Method Summary collapse

Instance Method Details

#access_permissionsObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yard_dizby/config_access_handler.rb', line 12

def access_permissions
  case statement.method_name(true)
  when :config_reader
    [true, false]
  when :config_writer
    [false, true]
  when :config_accessor
    [true, true]
  else
    [false, false]
  end
end

#processObject



84
85
86
87
88
89
90
91
# File 'lib/yard_dizby/config_access_handler.rb', line 84

def process
  return if statement.type == :var_ref || statement.type == :vcall
  params = statement.parameters(false).dup

  validated_attribute_names(params).each do |name|
    process_access(name)
  end
end

#process_access(name) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/yard_dizby/config_access_handler.rb', line 76

def process_access(name)
  read, write = access_permissions
  namespace.attributes[scope][name] ||= SymbolHash[read: nil, write: nil]

  process_reader(name, read)
  process_writer(name, write)
end

#process_impl(method, permission) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/yard_dizby/config_access_handler.rb', line 25

def process_impl(method, permission)
  if permission
    obj = MethodObject.new(namespace, method, scope)
    yield obj

    register(obj)
    obj
  else
    namespace.children.find do |o|
      o.name == method.to_sym && o.scope == scope
    end
  end
end

#process_reader(attribute, permission) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/yard_dizby/config_access_handler.rb', line 48

def process_reader(attribute, permission)
  final =
    process_impl(attribute, permission) do |obj|
      signature(obj, "def #{attribute}", "  @config[:#{attribute}]\nend")
      if obj.docstring.blank?(false)
        obj.docstring =
          "Returns the value of configuration attribute #{attribute}"
      end
    end

  store_obj :read, attribute, final
end

#process_writer(attribute, permission) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yard_dizby/config_access_handler.rb', line 61

def process_writer(attribute, permission)
  final =
    process_impl("#{attribute}=", permission) do |obj|
      obj.parameters = [['value', nil]]
      signature(obj, "def #{attribute}=(value)",
                "  @config[:#{attribute}] = value\nend")
      obj.docstring = <<-eos if obj.docstring.blank?(false)
        Sets the configuration attribute #{attribute}
        @param value the value to set the attribute #{attribute} to.
      eos
    end

  store_obj :write, attribute, final
end

#signature(obj, sig, src) ⇒ Object



43
44
45
46
# File 'lib/yard_dizby/config_access_handler.rb', line 43

def signature(obj, sig, src)
  obj.signature ||= sig
  obj.source ||= "#{sig}\n#{src}"
end

#store_obj(type, name, obj) ⇒ Object



39
40
41
# File 'lib/yard_dizby/config_access_handler.rb', line 39

def store_obj(type, name, obj)
  namespace.attributes[scope][name][type] = obj if obj
end