Class: PactBroker::Config::Setting

Inherits:
Object
  • Object
show all
Defined in:
lib/pact_broker/config/setting.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_db_type(object) ⇒ Object

rubocop: disable Metrics/CyclomaticComplexity



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/pact_broker/config/setting.rb', line 50

def self.get_db_type(object)
  case object
  when true, false
    "boolean"
  when String, nil
    "string"
  when SpaceDelimitedStringList
    "space_delimited_string_list"
  when Array, Hash
    "json"
  when Integer
    "integer"
  when Float
    "float"
  when Symbol
    "symbol"
  else
    nil
  end
end

.get_db_value(object) ⇒ Object

rubocop: enable Metrics/CyclomaticComplexity



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pact_broker/config/setting.rb', line 32

def self.get_db_value(object)
  case object
  when String, Integer, Float, NilClass
    object
  when TrueClass
    "1"
  when FalseClass
    "0"
  when SpaceDelimitedStringList, Symbol
    object.to_s
  when Array, Hash
    object.to_json
  else
    nil
  end
end

Instance Method Details

#set_value_from(object) ⇒ Object



5
6
7
8
9
# File 'lib/pact_broker/config/setting.rb', line 5

def set_value_from(object)
  self.type = Setting.get_db_type(object)
  self.value = Setting.get_db_value(object)
  self
end

#value_objectObject

rubocop: disable Metrics/CyclomaticComplexity



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/pact_broker/config/setting.rb', line 12

def value_object
  case type
  when "json"
    JSON.parse(value, symbolize_names: true)
  when "string"
    value
  when "symbol"
    value.to_sym
  when "integer"
    Integer(value)
  when "float"
    Float(value)
  when "space_delimited_string_list"
    SpaceDelimitedStringList.parse(value)
  when "boolean"
    value == "1"
  end
end