Class: RubyJard::Decorators::AttributesDecorator

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

Overview

Decorate collection data structure. Support:

  • Collection of values

  • Collection of key-value pairs

  • Individual value

  • Individual pair

This decorator should not be used directly.

Instance Method Summary collapse

Constructor Details

#initialize(generic_decorator) ⇒ AttributesDecorator

Returns a new instance of AttributesDecorator.



16
17
18
# File 'lib/ruby_jard/decorators/attributes_decorator.rb', line 16

def initialize(generic_decorator)
  @generic_decorator = generic_decorator
end

Instance Method Details

#inline_pairs(enum, total:, line_limit:, process_key:, value_proc: nil, depth: 0) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ruby_jard/decorators/attributes_decorator.rb', line 20

def inline_pairs(enum, total:, line_limit:, process_key:, value_proc: nil, depth: 0)
  return [ellipsis_span] if too_deep?(depth, line_limit)

  spans = []
  width = 1
  item_limit = total == 0 ? 0 : [line_limit / total, pair_limit(depth)].max

  enum.each do |(key, value), index|
    key_inspection = inspect_key(key, item_limit, process_key: process_key, depth: depth)
    key_inspection_length = key_inspection.map(&:content_length).sum

    value_inspection = @generic_decorator.decorate_singleline(
      value_proc.nil? ? value : value_proc.call(key),
      line_limit: [item_limit - key_inspection_length, pair_limit(depth)].max, depth: depth
    )
    value_inspection_length = value_inspection.map(&:content_length).sum

    if index > 0
      spans << separator_span
      width += 2
    end

    if width + key_inspection_length + value_inspection_length + 5 > line_limit
      spans << ellipsis_span
      break
    end

    spans += key_inspection
    width += key_inspection_length

    spans << arrow_span
    width += 3

    spans += value_inspection
    width += value_inspection_length
  end

  spans
end

#inline_values(enum, total:, line_limit:, depth: 0) ⇒ Object



83
84
85
86
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/ruby_jard/decorators/attributes_decorator.rb', line 83

def inline_values(enum, total:, line_limit:, depth: 0)
  return [ellipsis_span] if too_deep?(depth, line_limit)

  spans = []
  width = 1
  item_limit = total == 0 ? 0 : [line_limit / total, value_limit(depth)].max

  enum.each do |value, index|
    value_inspection = @generic_decorator.decorate_singleline(
      value, line_limit: [item_limit, value_limit(depth)].max, depth: depth
    )
    value_inspection_length = value_inspection.map(&:content_length).sum

    if index > 0
      spans << separator_span
      width += 2
    end

    if width + value_inspection_length + 2 > line_limit
      spans << ellipsis_span
      break
    end

    spans += value_inspection
    width += value_inspection_length
  end

  spans
end

#pair(key, value, line_limit:, process_key:, depth: 0) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/ruby_jard/decorators/attributes_decorator.rb', line 60

def pair(key, value, line_limit:, process_key:, depth: 0)
  return [ellipsis_span] if too_deep?(depth, line_limit)

  spans = []
  spans << indent_span
  width = indent_span.content_length

  key_inspection = inspect_key(key, line_limit - width, process_key: process_key, depth: depth)
  key_inspection_length = key_inspection.map(&:content_length).sum

  spans += key_inspection
  width += key_inspection_length

  spans << arrow_span
  width += 3

  value_inspection = @generic_decorator.decorate_singleline(
    value, line_limit: [line_limit - width, pair_limit(depth)].max, depth: depth
  )

  spans + value_inspection
end

#value(value, line_limit:, depth: 0) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ruby_jard/decorators/attributes_decorator.rb', line 113

def value(value, line_limit:, depth: 0)
  return [ellipsis_span] if too_deep?(depth, line_limit)

  spans = []
  spans << indent_span
  width = indent_span.content_length

  value_inspection = @generic_decorator.decorate_singleline(
    value, line_limit: [line_limit - width, value_limit(depth)].max, depth: depth
  )

  spans + value_inspection
end