Module: Train::Options::InstanceOptions

Defined in:
lib/train/options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#optionsHash (readonly)

Returns options, which created this Transport.

Returns:

  • (Hash)

    options, which created this Transport



60
61
62
# File 'lib/train/options.rb', line 60

def options
  @options
end

Instance Method Details

#default_audit_log_optionsObject



62
63
64
# File 'lib/train/options.rb', line 62

def default_audit_log_options
  self.class.default_audit_log_options
end

#default_optionsObject



66
67
68
# File 'lib/train/options.rb', line 66

def default_options
  self.class.default_options
end

#merge_options(base, opts) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/train/options.rb', line 70

def merge_options(base, opts)
  res = base.merge(opts || {})
  # Also merge the default audit log options into the options so that those are available at the time of validation.
  default_options.merge(default_audit_log_options).each do |field, hm|
    next unless res[field].nil? && hm.key?(:default)

    default = hm[:default]
    if default.is_a? Proc
      res[field] = default.call(res)
    elsif hm.key?(:coerce)
      field_value = hm[:coerce].call(res)
      res[field] = field_value.nil? ? default : field_value
    else
      res[field] = default
    end
  end
  res
end

#validate_audit_log_options(opts) ⇒ Object

Introduced this method to validate only audit log options and avoiding call to validate_options so that it will no break existing implementation.



101
102
103
104
105
106
107
108
109
# File 'lib/train/options.rb', line 101

def validate_audit_log_options(opts)
  default_audit_log_options.each do |field, hm|
    if opts[field].nil? && hm[:required]
      raise Train::ClientError,
        "You must provide a value for #{field.to_s.inspect}."
    end
  end
  opts
end

#validate_options(opts) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/train/options.rb', line 89

def validate_options(opts)
  default_options.each do |field, hm|
    if opts[field].nil? && hm[:required]
      raise Train::ClientError,
        "You must provide a value for #{field.to_s.inspect}."
    end
  end
  opts
end