Module: Xqsr3::Diagnostics::InspectBuilder

Included in:
Containers::FrequencyMap
Defined in:
lib/xqsr3/doc_.rb,
lib/xqsr3/diagnostics/inspect_builder.rb

Overview

Inspect builder

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.make_inspect(o, **options) ⇒ Object

Generates an inspect string for the include-ing class

Signature

  • Parameters:

    • o The target of the inspect message for which a message will be built

  • Options:

    • :no_class (boolean) Elides the class qualification

    • :no_object_id (boolean) Elides the object id

    • :show_fields (boolean) Shows (all) object fields

    • :hidden_fields ([ String ]) Names of fields to be omitted (when :show_fields is specified). Overridden by :shown_fields

    • :shown_fields ([ String ]) Names of fields to be shown (when :show_fields is specified). Overrides :hidden_fields

    • :truncate_width (Integer) Specifies a maximum width for the values of fields

    • :deep_inspect (boolean) Causes fields’ values to be obtained via their own inspect methods



80
81
82
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/xqsr3/diagnostics/inspect_builder.rb', line 80

def self.make_inspect o, **options

  r = ''

  unless options[:no_class]

    r += o.class.to_s
  end

  unless options[:no_object_id]

    r += ':' unless r.empty?
    r += "0x#{o.object_id.to_s.rjust(16, '0')}"
  end

  if options[:show_fields]

    normalise = InspectBuilder_Utilities::NORMALISE_FUNCTION

    hide_fields = normalise.call(options[:hidden_fields] || [])
    show_fields = normalise.call(options[:shown_fields] || [])
    trunc_w = options[:truncate_width]
    ivars = normalise.call(o.instance_variables)

    unless show_fields.empty?

      ivars = ivars & show_fields
    else

      o.class.ancestors.each do |ancestor|

        ihf_constant = :INSPECT_HIDDEN_FIELDS

        if ancestor.const_defined? ihf_constant

          ihfs = ancestor.const_get ihf_constant

          if ::Array === ihfs && ihfs.all? { |c| ::String === c }

            hide_fields += normalise.call(ihfs)
          else

            warn "class/module #{ancestor}'s #{ihf_constant} should be an array of strings"
          end
        end
      end

      ivars = ivars - hide_fields
    end

    els = ivars.sort.map do |iv_name|

      iv_value = o.instance_variable_get(iv_name)
      iv_class = iv_value.class
      iv_value = ::Xqsr3::StringUtilities::Truncate.string_truncate(iv_value.to_s, trunc_w) if trunc_w
      if options[:deep_inspect]

        iv_value = iv_value.inspect
      else

        case iv_value
        when ::Array


        when ::String

          iv_value = "'#{iv_value}'"
        end
      end

      "#{iv_name}(#{iv_class})=#{iv_value}"
    end.join('; ')

    r += ': ' unless r.empty?
    r += els
  end

  r = '#<' + r + '>'

  r
end

Instance Method Details

#make_inspect(**options) ⇒ Object

Creates an inspect string from self

see InspectBuilder::make_inspect



165
166
167
168
# File 'lib/xqsr3/diagnostics/inspect_builder.rb', line 165

def make_inspect **options

  ::Xqsr3::Diagnostics::InspectBuilder.make_inspect self, **options
end