Class: Betterdocs::ResultRepresenterCollector

Inherits:
Object
  • Object
show all
Defined in:
lib/betterdocs/result_representer_collector.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResultRepresenterCollector

Returns a new instance of ResultRepresenterCollector.



3
4
5
6
# File 'lib/betterdocs/result_representer_collector.rb', line 3

def initialize
  @properties = {}
  @links      = {}
end

Instance Attribute Details

Returns the value of attribute links.



10
11
12
# File 'lib/betterdocs/result_representer_collector.rb', line 10

def links
  @links
end

#propertiesObject (readonly)

Returns the value of attribute properties.



8
9
10
# File 'lib/betterdocs/result_representer_collector.rb', line 8

def properties
  @properties
end

Instance Method Details

#add_element(representer, type, name, **options, &block) ⇒ Object



22
23
24
25
# File 'lib/betterdocs/result_representer_collector.rb', line 22

def add_element(representer, type, name, **options, &block)
  element = build_element(representer, type, name, options, &block)
  element.add_to_collector(self)
end


17
18
19
20
# File 'lib/betterdocs/result_representer_collector.rb', line 17

def link(link_name)
  link_name = link_name.to_sym
  @links[link_name]
end


42
43
44
45
46
47
48
49
50
51
52
# File 'lib/betterdocs/result_representer_collector.rb', line 42

def nested_links(path = [])
  result = links.values.map { |l| l.below_path(path) }
  properties.values.each_with_object(result) do |property, result|
    if sr = property.sub_representer?
      nested_property = property.below_path(path)
      links = sr.docs.nested_links(nested_property.path)
      result.concat links
    end
  end
  result
end

#nested_properties(path = []) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/betterdocs/result_representer_collector.rb', line 33

def nested_properties(path = [])
  properties.values.each_with_object([]) do |property, result|
    result << property.below_path(path)
    if sr = property.sub_representer?
      result.concat sr.docs.nested_properties(path + property.path)
    end
  end
end

#property(property_name) ⇒ Object



12
13
14
15
# File 'lib/betterdocs/result_representer_collector.rb', line 12

def property(property_name)
  property_name = property_name.to_sym
  @properties[property_name]
end

#representerObject



27
28
29
30
31
# File 'lib/betterdocs/result_representer_collector.rb', line 27

def representer
  (@properties.values + @links.values).find { |v|
    v.representer and break v.representer
  }
end

#to_sObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/betterdocs/result_representer_collector.rb', line 54

def to_s
  result = "*** #{representer} ***\n"
  if properties = @properties.values.full?
    result << "\nProperties:"
    nested_properties.each_with_object(result) do |property, r|
      r << "\n#{property.full_name}: (#{property.types * '|'}): #{property.description}\n"
    end
  end
  if links = @links.values.full?
    result << "\nLinks:"
    links.each_with_object(result) do |link, r|
      r << "\n#{link.full_name}: #{link.description}\n" # TODO resolve link.url in some useful way
    end
  end
  result
end