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 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.
151 152 153 154 |
# File 'lib/squirm/procedure.rb', line 151 def initialize(string) @string = string @hash = self.class.hashify(string) end |
Instance Attribute Details
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
136 137 138 |
# File 'lib/squirm/procedure.rb', line 136 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.
204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/squirm/procedure.rb', line 204 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.
182 183 184 |
# File 'lib/squirm/procedure.rb', line 182 def [](key) (key.kind_of?(Fixnum) ? keys : hash)[key] end |
#format(*args) ⇒ Object
Formats arguments used to call the stored procedure.
When given a anything other than a hash, the arguments are returned without modification.
When given a hash, the return value is an array or arguments in the order needed when calling the procedure. Missing values are replaced by nil.
170 171 172 |
# File 'lib/squirm/procedure.rb', line 170 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.
198 199 200 |
# File 'lib/squirm/procedure.rb', line 198 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.
190 191 192 193 194 |
# File 'lib/squirm/procedure.rb', line 190 def to_params @params ||= each_with_index.map do |key, index| "$%s::%s" % [index.next, hash[key]] end.join(", ") end |