Class: WiseGopher::RawParam

Inherits:
Object
  • Object
show all
Defined in:
lib/wise_gopher/raw_param.rb

Overview

Register query’s raw_params and interpolate string in query

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, optional: false, default: nil, prefix: nil, suffix: nil) ⇒ RawParam

Returns a new instance of RawParam.



8
9
10
11
12
13
14
# File 'lib/wise_gopher/raw_param.rb', line 8

def initialize(name, optional: false, default: nil, prefix: nil, suffix: nil)
  @name     = name.to_s.freeze
  @optional = optional
  @default  = default
  @prefix   = prefix.to_s.freeze
  @suffix   = suffix.to_s.freeze
end

Instance Attribute Details

#defaultObject (readonly)

Returns the value of attribute default.



6
7
8
# File 'lib/wise_gopher/raw_param.rb', line 6

def default
  @default
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/wise_gopher/raw_param.rb', line 6

def name
  @name
end

#optionalObject (readonly)

Returns the value of attribute optional.



6
7
8
# File 'lib/wise_gopher/raw_param.rb', line 6

def optional
  @optional
end

#prefixObject (readonly)

Returns the value of attribute prefix.



6
7
8
# File 'lib/wise_gopher/raw_param.rb', line 6

def prefix
  @prefix
end

#suffixObject (readonly)

Returns the value of attribute suffix.



6
7
8
# File 'lib/wise_gopher/raw_param.rb', line 6

def suffix
  @suffix
end

Instance Method Details

#optional?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/wise_gopher/raw_param.rb', line 26

def optional?
  optional || !!default
end

#to_s(string = nil) ⇒ Object

Raises:

  • (::ArgumentError)


16
17
18
19
20
21
22
23
24
# File 'lib/wise_gopher/raw_param.rb', line 16

def to_s(string = nil)
  raise ::ArgumentError, "value required" unless string || optional?

  content = string || default

  return "#{prefix}#{content}#{suffix}" if content

  ""
end