Class: Squirm::Procedure::Arguments
- Inherits:
-
Object
- Object
- Squirm::Procedure::Arguments
- Extended by:
- Forwardable
- Includes:
- Enumerable
- Defined in:
- lib/squirm/procedure.rb
Overview
A collection of argument definitions for a stored procedure. This class delegates both to an internal hash and to its keys, so it has mixed Array/Hash-like behavior. This allows you to access arguments by offset or name.
This may seem like an odd mix of behaviors but is intended to idiomatically translate Postgres’s support for both named and unnamed stored procedure arguments.
Instance Attribute Summary collapse
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
Class Method Summary collapse
-
.hashify(string) ⇒ Object
Converts an argument string to a hash whose keys are argument names and whose values are argument types.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Gets an argument’s Postgres type by index or offset.
-
#format(*args) ⇒ Object
Formats the arguments used to call the stored procedure.
-
#info_sql ⇒ Object
Gets an SQL query used to look up meta information about a stored procedure with a matching argument signature.
-
#initialize(string) ⇒ Arguments
constructor
Gets an instance of Arguments from a string.
-
#to_params ⇒ Object
Gets Postgres-formatted params for use in calling the procedure.
Constructor Details
#initialize(string) ⇒ Arguments
Gets an instance of Arguments from a string.
This string can come from a lookup in the pg_proc catalog, or in the case of overloaded functions, will be specified explicitly by the programmer.
161 162 163 164 |
# File 'lib/squirm/procedure.rb', line 161 def initialize(string) @string = string @hash = self.class.hashify(string) end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
146 147 148 |
# File 'lib/squirm/procedure.rb', line 146 def hash @hash end |
Class Method Details
.hashify(string) ⇒ Object
Converts an argument string to a hash whose keys are argument names and whose values are argument types.
214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/squirm/procedure.rb', line 214 def self.hashify(string) hash = {} string.split(",").map do |arg| arg, type = arg.strip.split(/\s+/, 2) type ||= arg arg = arg.gsub(/\s+/, '_').gsub(/\A_/, '') count = hash.keys.count {|elem| elem =~ /#{arg}[\d]?/} key = count == 0 ? arg : arg + count.next.to_s hash[key.to_sym] = type end hash end |
Instance Method Details
#[](offset) ⇒ Object #[](key) ⇒ Object
Gets an argument’s Postgres type by index or offset.
192 193 194 |
# File 'lib/squirm/procedure.rb', line 192 def [](key) (key.kind_of?(Fixnum) ? keys : hash)[key] end |
#format(*args) ⇒ Object
Formats the arguments used to call the stored procedure.
When given anything other than a hash, the arguments are returned without modification.
When given a hash, the return value is an array of arguments in the order needed when calling the procedure. Missing values are replaced by nil
.
180 181 182 |
# File 'lib/squirm/procedure.rb', line 180 def format(*args) args.first.kind_of?(Hash) ? map {|name| args[0][name]} : args end |
#info_sql ⇒ Object
Gets an SQL query used to look up meta information about a stored procedure with a matching argument signature.
208 209 210 |
# File 'lib/squirm/procedure.rb', line 208 def info_sql "#{INFO_SQL} AND pg_catalog.pg_get_function_arguments(p.oid) = '#{to_s}'" end |
#to_params ⇒ Object
Gets Postgres-formatted params for use in calling the procedure.
200 201 202 203 204 |
# File 'lib/squirm/procedure.rb', line 200 def to_params @params ||= each_with_index.map do |key, index| "$%s::%s" % [index.next, hash[key]] end.join(", ") end |