Class: RubyJard::Decorators::StructDecorator

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

Overview

Decorator for Struct. TODO: This one should handle Open Struct too

Instance Method Summary collapse

Constructor Details

#initialize(generic_decorator) ⇒ StructDecorator

Returns a new instance of StructDecorator.



9
10
11
12
# File 'lib/ruby_jard/decorators/struct_decorator.rb', line 9

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



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/ruby_jard/decorators/struct_decorator.rb', line 32

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

  singleline = decorate_singleline(variable, line_limit: first_line_limit)

  if singleline.map(&:content_length).sum < line_limit || 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



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby_jard/decorators/struct_decorator.rb', line 18

def decorate_singleline(variable, line_limit:, depth: 0)
  spans = [RubyJard::Span.new(content: '#<struct', margin_right: 1, styles: :text_dim)]
  unless variable.class.name.nil?
    spans << RubyJard::Span.new(content: variable.class.name.to_s, margin_right: 1, styles: :text_primary)
  end
  spans += @attributes_decorator.inline_pairs(
    variable.members.each_with_index,
    total: variable.length, line_limit: line_limit - spans.map(&:content_length).sum - 1,
    process_key: false, depth: depth + 1,
    value_proc: ->(key) { variable[key] }
  )
  spans << RubyJard::Span.new(content: '>', styles: :text_dim)
end

#match?(variable) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ruby_jard/decorators/struct_decorator.rb', line 14

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