Class: Parlour::RbiGenerator::Arbitrary

Inherits:
RbiObject
  • Object
show all
Defined in:
lib/parlour/rbi_generator/arbitrary.rb

Overview

Represents miscellaneous Ruby code.

Instance Attribute Summary collapse

Attributes inherited from RbiObject

#comments, #generated_by, #generator, #name

Instance Method Summary collapse

Methods inherited from RbiObject

#add_comment

Constructor Details

#initialize(generator, code: '', &block) ⇒ Arbitrary

Creates new arbitrary code.

Parameters:

  • code (String) (defaults to: '')

    The arbitrary code string. Indentation is added to the beginning of each line.



17
18
19
20
21
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 17

def initialize(generator, code: '', &block)
  super(generator, '')
  @code = code
  yield_self(&block) if block
end

Instance Attribute Details

#codeObject

Returns arbitrary code string.



25
26
27
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 25

def code
  @code
end

Instance Method Details

#==(other) ⇒ Boolean

Returns true if this instance is equal to another arbitrary code line.

Parameters:

Returns:

  • (Boolean)


33
34
35
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 33

def ==(other)
  Arbitrary === other && code == other.code
end

#describeString

Returns a human-readable brief string description of this code.

Returns:

  • (String)


87
88
89
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 87

def describe
  "Arbitrary code (#{code})"
end

#generate_rbi(indent_level, options) ⇒ Array<String>

Generates the RBI lines for this arbitrary code.

Parameters:

  • indent_level (Integer)

    The indentation level to generate the lines at.

  • options (Options)

    The formatting options to use.

Returns:

  • (Array<String>)

    The RBI lines, formatted as specified.



48
49
50
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 48

def generate_rbi(indent_level, options)
  code.split("\n").map { |l| options.indented(indent_level, l) }
end

#merge_into_self(others) ⇒ void

This method returns an undefined value.

Given an array of Parlour::RbiGenerator::Arbitrary instances, merges them into this one. This particular implementation always throws an exception, because #mergeable? is always false.

Parameters:



79
80
81
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 79

def merge_into_self(others)
  raise 'arbitrary code is never mergeable'
end

#mergeable?(others) ⇒ Boolean

Given an array of Parlour::RbiGenerator::Arbitrary instances, returns true if they may be merged into this instance using #merge_into_self. This is always false.

Parameters:

Returns:

  • (Boolean)

    Whether this instance may be merged with them.



63
64
65
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 63

def mergeable?(others)
  false
end