Class: Chemtrail::ReferencePresenter

Inherits:
Object
  • Object
show all
Defined in:
lib/chemtrail/reference_presenter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argument) ⇒ ReferencePresenter

Returns a new instance of ReferencePresenter.



4
5
6
# File 'lib/chemtrail/reference_presenter.rb', line 4

def initialize(argument)
  @argument = argument
end

Instance Attribute Details

#argumentObject (readonly)

Returns the value of attribute argument.



2
3
4
# File 'lib/chemtrail/reference_presenter.rb', line 2

def argument
  @argument
end

Instance Method Details

#hashlike?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/chemtrail/reference_presenter.rb', line 16

def hashlike?
  argument.respond_to?(:to_hash)
end

#iterable?Boolean

Returns:

  • (Boolean)


12
13
14
# File 'lib/chemtrail/reference_presenter.rb', line 12

def iterable?
  argument.respond_to?(:each)
end

#reference?Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/chemtrail/reference_presenter.rb', line 8

def reference?
  argument.respond_to?(:to_reference)
end

#to_parameterObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chemtrail/reference_presenter.rb', line 20

def to_parameter
  if reference?
    argument.to_reference
  else
    if iterable?
      if hashlike?
        argument.reduce(argument) { |h, (k, v)| h[k] = self.class.new(v).to_parameter; h }
      else
        argument.map { |item| self.class.new(item).to_parameter }
      end
    else
      argument
    end
  end
end