Class: RubyJard::Decorators::ArrayDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_jard/decorators/array_decorator.rb

Overview

Decorate Array data structure, supports singleline and multiline form.

Instance Method Summary collapse

Constructor Details

#initialize(generic_decorator) ⇒ ArrayDecorator

Returns a new instance of ArrayDecorator.



8
9
10
11
# File 'lib/ruby_jard/decorators/array_decorator.rb', line 8

def initialize(generic_decorator)
  @generic_decorator = generic_decorator
  @attributes_decorator = RubyJard::Decorators::AttributesDecorator.new(generic_decorator)
end

Instance Method Details

#decorate_multiline(variable, first_line_limit:, lines:, line_limit:, depth: 0) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby_jard/decorators/array_decorator.rb', line 28

def decorate_multiline(variable, first_line_limit:, lines:, line_limit:, depth: 0)
  if variable.length > lines * 2
    return do_decorate_multiline(variable, lines: lines, line_limit: line_limit, depth: depth)
  end

  singleline = decorate_singleline(variable, line_limit: first_line_limit, depth: depth)
  if (singleline.map(&:content_length).sum < line_limit && same_type?(variable, lines)) || variable.length <= 1
    [singleline]
  else
    do_decorate_multiline(variable, lines: lines, line_limit: line_limit, depth: depth)
  end
end

#decorate_singleline(variable, line_limit:, depth: 0) ⇒ Object



17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_jard/decorators/array_decorator.rb', line 17

def decorate_singleline(variable, line_limit:, depth: 0)
  spans = []
  spans << RubyJard::Span.new(content: '[', styles: :text_primary)
  spans += @attributes_decorator.inline_values(
    variable.each_with_index, total: variable.length, line_limit: line_limit - 2, depth: depth + 1
  )
  spans << RubyJard::Span.new(content: ']', styles: :text_primary)

  spans
end

#match?(variable) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/ruby_jard/decorators/array_decorator.rb', line 13

def match?(variable)
  RubyJard::Reflection.call_is_a?(variable, Array)
end