Class: RubyJard::Decorators::HashDecorator

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

Overview

Decorate Hash data structure, supports singleline and multiline form.

Instance Method Summary collapse

Constructor Details

#initialize(generic_decorator) ⇒ HashDecorator

Returns a new instance of HashDecorator.



8
9
10
11
# File 'lib/ruby_jard/decorators/hash_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



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_jard/decorators/hash_decorator.rb', line 23

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



13
14
15
16
17
18
19
20
21
# File 'lib/ruby_jard/decorators/hash_decorator.rb', line 13

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

#match?(variable) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/ruby_jard/decorators/hash_decorator.rb', line 36

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