Class: ObjectIdentifier::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/object_identifier/parameters.rb

Overview

ObjectIdentifier::Parameters encapsulates the attributes list and formatter options that may be needed for custom formatting during object identification.

Constant Summary collapse

KLASS_NOT_GIVEN =

This String to display if ‘formatter_options` isn’t present.

"NOT_GIVEN"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes: [], formatter_options: {}) ⇒ Parameters

Returns a new instance of Parameters.

Parameters:

  • attributes (Array, *args) (defaults to: [])

    A list of method calls to interrogate the given object(s) with.

  • formatter_options[:limit] (Hash)

    a customizable set of options

  • formatter_options[:klass] (Hash)

    a customizable set of options



31
32
33
34
35
36
37
# File 'lib/object_identifier/parameters.rb', line 31

def initialize(
      attributes: [],
      formatter_options: {})
  @attributes = attributes
  @limit = formatter_options.fetch(:limit, nil)
  @klass = formatter_options.fetch(:klass, KLASS_NOT_GIVEN)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



10
11
12
# File 'lib/object_identifier/parameters.rb', line 10

def attributes
  @attributes
end

Class Method Details

.build(attributes: [], formatter_options: {}) ⇒ Object

Factory method for building an ObjectIdentifier::Parameters object. Uses ObjectIdentifier.default_attributes if the given ‘attributes` array is empty.



15
16
17
18
19
20
21
22
23
# File 'lib/object_identifier/parameters.rb', line 15

def self.build(attributes: [], formatter_options: {})
  attrs = ObjectIdentifier::ArrayWrap.(attributes)
  attrs = ObjectIdentifier.default_attributes if attrs.empty?
  attrs.flatten!

  new(
    attributes: attrs,
    formatter_options: formatter_options.to_h)
end

Instance Method Details

#klassObject

NOTE: Expects a block if a value wasn’t supplied on initialization.



45
46
47
48
49
50
51
52
53
# File 'lib/object_identifier/parameters.rb', line 45

def klass
  if klass_given?
    @klass.to_s
  elsif block_given?
    yield.to_s
  else
    nil
  end
end

#limitObject

NOTE: Expects a block if a value wasn’t supplied on initialization.



40
41
42
# File 'lib/object_identifier/parameters.rb', line 40

def limit
  @limit || (yield if block_given?)
end