Class: Awspec::Generator::Doc::Type::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/awspec/generator/doc/type/base.rb

Instance Method Summary collapse

Constructor Details

#initializeBase

Returns a new instance of Base.



5
6
7
8
# File 'lib/awspec/generator/doc/type/base.rb', line 5

def initialize
  Awspec::Stub.load type_name.underscore
  @type_name = type_name
end

Instance Method Details

#collect_matchersObject



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/awspec/generator/doc/type/base.rb', line 51

def collect_matchers
  methods = @type.methods - Awspec::Helper::Finder.instance_methods - Object.methods
  methods.select! do |method|
    method.to_s.include?('?')
  end
  methods.map! do |method|
    next 'exist' if 'exists?' == method.to_s
    next 'have_' + Regexp.last_match[1] if /\Ahas_(.+)\?\z/ =~ method.to_s
    next 'be_' + Regexp.last_match[1] if /\A(.+)\?\z/ =~ method.to_s
    method.to_s
  end
end

#doc_templateObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/awspec/generator/doc/type/base.rb', line 64

def doc_template
  template = <<-'EOF'
## <a name="<%= @type_name.gsub(/ /, '_').underscore %>"><%= @type_name.gsub(/ /, '_').underscore %></a>

<%= @type_name %> resource type.
<%- if @descriptions.include?('first') -%><%= @descriptions['first'] %><%- end -%>
<% @matchers.each do |matcher| %>
### <%= matcher %>
<%- if @descriptions.include?(matcher) -%><%= @descriptions[matcher] %><%- end -%>
<% end %>
<%- unless its.empty? -%>### <%= its.join(', ') %><%- end -%>
<%- if @descriptions.include?('advanced') -%>

### :unlock: Advanced use
<%= @descriptions['advanced'] %><%- end -%>

EOF
  template
end

#generate_docObject

rubocop:disable Metrics/MethodLength



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/awspec/generator/doc/type/base.rb', line 15

def generate_doc
  @matchers += collect_matchers - @ignore_matchers
  @matchers.sort! do |a, b|
    ret = sort_num(a) <=> sort_num(b)
    next ret if ret != 0
    a.casecmp(b)
  end
  if @ret.respond_to?(:members)
    @describes += @ret.members.select do |describe|
      if @ret[describe].is_a?(Array)
        next true unless @ret[describe].first.is_a?(Array) || @ret[describe].first.is_a?(Hash) || @ret[describe].first.is_a?(Struct) # rubocop:disable Metrics/LineLength
      else
        next true unless @ret[describe].is_a?(Hash) || @ret[describe].is_a?(Struct)
      end
    end
  end
  its = @describes.map do |describe|
    'its(:' + describe.to_s + ')'
  end

  @descriptions = {}
  merge_file = File.dirname(__FILE__) + '/../../../../../doc/_resource_types/' + type_name.underscore + '.md'
  if File.exist?(merge_file)
    matcher = nil
    File.foreach(merge_file) do |line|
      if /\A### (.+)\Z/ =~ line
        matcher = Regexp.last_match[1]
        next
      end
      @descriptions[matcher] = '' unless @descriptions[matcher]
      @descriptions[matcher] += line
    end
  end
  ERB.new(doc_template, nil, '-').result(binding)
end

#sort_num(str) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/awspec/generator/doc/type/base.rb', line 84

def sort_num(str)
  case str
  when 'exist'
    0
  when /\Abe_/
    1
  when /\Ahave_/
    2
  else
    3
  end
end

#type_nameObject



10
11
12
# File 'lib/awspec/generator/doc/type/base.rb', line 10

def type_name
  self.class.to_s.split('::').last
end