Module: Lazylead::ORM

Included in:
System, Task, Team
Defined in:
lib/lazylead/model.rb

Overview

ORM-related methods

Defined Under Namespace

Classes: Retry, System, Task, Team, Verbose

Instance Method Summary collapse

Instance Method Details

#env(opts) ⇒ Object

Tables from database may have configuration in json column. Some of those properties might be configured/overridden by environment variable using macros “$…”

{
   ...
   "user" = "${my_user}",
   "pass" = "${my_pass}"
   "url" = "https://url.com"
   ...
}

next, we need to configure following environment variables(ENV)

my_user=XXXXX
my_pass=YYYYY

thus, the result of this method is

{
   ...
   "user" = "XXXXXX",
   "pass" = "YYYYYY"
   "url" = "https://url.com"
   ...
}


67
68
69
70
71
72
73
74
# File 'lib/lazylead/model.rb', line 67

def env(opts)
  opts.each_with_object({}) do |e, o|
    k = e[0]
    v = e[1]
    v = ENV.fetch(v.slice(2, v.length - 3), nil) if v.respond_to?(:start_with?) && v.start_with?("${")
    o[k] = v
  end
end

#inspectObject



92
93
94
# File 'lib/lazylead/model.rb', line 92

def inspect
  to_s
end

#to_h?Boolean

Returns:

  • (Boolean)


81
82
83
84
85
86
# File 'lib/lazylead/model.rb', line 81

def to_h?
  return true unless to_hash.nil?
  false
rescue StandardError => _e
  false
end

#to_hashObject

Convert json-based database column ‘properties’ to hash.



77
78
79
# File 'lib/lazylead/model.rb', line 77

def to_hash
  JSON.parse(properties).to_h
end

#to_sObject



88
89
90
# File 'lib/lazylead/model.rb', line 88

def to_s
  attributes.map { |k, v| "#{k}='#{v}'" }.join(", ")
end