Class: Orthoses::Content

Inherits:
Object
  • Object
show all
Defined in:
lib/orthoses/content.rb,
lib/orthoses/content/environment.rb,
lib/orthoses/content/header_builder.rb,
lib/orthoses/content/duplication_checker.rb

Overview

Common interface for output. Orthoses::Content expect to use result of middleware.

store = @loader.call
store["Foo::Bar"] << "def baz: () -> void"

By default, Orthoses::Content search constant of keys. and decide class or module declaration. Also declaraion can specify directly.

store["Foo::Bar"].header = "class Foo::Bar < Baz"

Orthoses::Content can generate RBS.

store["Foo::Bar"].to_rbs

Defined Under Namespace

Classes: DuplicationChecker, Environment, HeaderBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:) ⇒ Content

Returns a new instance of Content.



24
25
26
27
28
29
30
# File 'lib/orthoses/content.rb', line 24

def initialize(name:)
  Orthoses.logger.debug("Create Orthoses::Content for #{name}")
  @name = name
  @body = []
  @header = nil
  @comment = nil
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



20
21
22
# File 'lib/orthoses/content.rb', line 20

def body
  @body
end

#commentObject

Returns the value of attribute comment.



22
23
24
# File 'lib/orthoses/content.rb', line 22

def comment
  @comment
end

#headerObject

Returns the value of attribute header.



21
22
23
# File 'lib/orthoses/content.rb', line 21

def header
  @header
end

#nameObject (readonly)

Returns the value of attribute name.



19
20
21
# File 'lib/orthoses/content.rb', line 19

def name
  @name
end

Instance Method Details

#<<(line) ⇒ Object



32
33
34
# File 'lib/orthoses/content.rb', line 32

def <<(line)
  @body << line
end

#auto_headerObject



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/orthoses/content.rb', line 87

def auto_header
  env = Utils.rbs_environment
  if entry = env.class_decls[TypeName(name).absolute!]
    @header = Content::HeaderBuilder.new(env: env).build(entry: entry)
    return
  end

  return unless @header.nil?

  if interface?
    self.header = "interface #{name}"
    return
  end

  val = Object.const_get(name)

  case val
  when Class
    self.header = "class #{Utils.module_name(val)}#{type_params(name)}#{build_super_class(val)}"
  when Module
    self.header = "module #{Utils.module_name(val)}#{type_params(name)}"
  else
    raise "#{val.inspect} is not class/module"
  end
end

#concat(other) ⇒ Object



36
37
38
# File 'lib/orthoses/content.rb', line 36

def concat(other)
  @body.concat(other)
end

#delete(val) ⇒ Object



48
49
50
# File 'lib/orthoses/content.rb', line 48

def delete(val)
  @body.delete(val)
end

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/orthoses/content.rb', line 40

def empty?
  @body.empty?
end

#interface?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/orthoses/content.rb', line 44

def interface?
  @name.split('::').last.start_with?('_')
end

#original_rbsObject



76
77
78
79
80
81
82
83
84
85
# File 'lib/orthoses/content.rb', line 76

def original_rbs
  io = StringIO.new
  io.puts @comment if @comment
  io.puts @header
  @body.each do |line|
    io.puts "  #{line}"
  end
  io.puts "end"
  io.string
end

#sort!Object



67
68
69
70
71
72
73
74
# File 'lib/orthoses/content.rb', line 67

def sort!
  require 'rbs/sorter'
  sorter = ::RBS::Sorter.new(nil, stdout: nil)
  decl = to_decl
  sorter.sort_decl!(decl)
  lines = decl_to_lines(decl)
  @body.replace(lines)
end

#to_declObject



57
58
59
60
# File 'lib/orthoses/content.rb', line 57

def to_decl
  auto_header
  uniqed_body_decl
end

#to_rbsObject



52
53
54
55
# File 'lib/orthoses/content.rb', line 52

def to_rbs
  auto_header
  uniqed_body_string
end

#uniq!Object



62
63
64
65
# File 'lib/orthoses/content.rb', line 62

def uniq!
  lines = decl_to_lines(to_decl)
  @body.replace(lines)
end