Class: Parlour::RbiGenerator::Arbitrary

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

Overview

Represents miscellaneous Ruby code.

Instance Attribute Summary collapse

Attributes inherited from RbiObject

#generator

Attributes inherited from TypedObject

#comments, #generated_by, #name

Instance Method Summary collapse

Methods inherited from TypedObject

#add_comment, #describe, #describe_tree

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

#describe_attrsObject



84
85
86
# File 'lib/parlour/rbi_generator/arbitrary.rb', line 84

def describe_attrs
  [:code]
end

#generalize_from_rbi!Object

Nothing to do



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

def generalize_from_rbi!; 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