Class: Sqlyzer::Parameter::Default
- Inherits:
-
Object
- Object
- Sqlyzer::Parameter::Default
- Includes:
- Comparable
- Defined in:
- lib/sqlyzer/parameters.rb
Overview
Proxy class for Symbol. Permit to extend a Symbol class with coresponding Sql type handling Mixin.
This class mainly exists to enable extending a Symbol, which is not possible with Ruby regular Symbol class. This is nearly an abstract class (as Ruby doesn’t really implement this concept) and must be extended with coresponding Sql type Mixin. If no type specification is given, the class will be considered as a Sqlyzer::Parameter::Types::Text attribute.
Instance Attribute Summary collapse
-
#symbol ⇒ Object
readonly
Returns the value of attribute symbol.
Class Method Summary collapse
-
.get_from_default(symbol, val) ⇒ Object
Create an instance of Default by giving a Symbol and a default value.
-
.get_from_type(symbol, type) ⇒ Object
Create a new instance of Default by giving a Symbol and the coresponding Sql type Mixin.
Instance Method Summary collapse
-
#/(default) ⇒ Object
Call sql_parameter_default and return self.
-
#<=>(other) ⇒ Object
Implement the Ruby Comparable Mixin, enabling to compare Default with Default, Symbol or String (in this order).
-
#initialize(symbol) ⇒ Default
constructor
Constructor method.
-
#to_owner(owner, data) ⇒ Object
Unserialize Sql representation of data to assign it to owner.
-
#to_s ⇒ Object
Call contained Symbol to_s method.
-
#to_sql(owner) ⇒ Object
Serialize the Symbol value contained in owner to Sql representation.
-
#to_sql_type ⇒ Object
Return the Sql type name of the current instance.
Constructor Details
#initialize(symbol) ⇒ Default
Constructor method. The fresh Default object will possess symbol as @symbol attribute.
200 201 202 |
# File 'lib/sqlyzer/parameters.rb', line 200 def initialize(symbol) @symbol = symbol end |
Instance Attribute Details
#symbol ⇒ Object (readonly)
Returns the value of attribute symbol.
193 194 195 |
# File 'lib/sqlyzer/parameters.rb', line 193 def symbol @symbol end |
Class Method Details
.get_from_default(symbol, val) ⇒ Object
Create an instance of Default by giving a Symbol and a default value. Coresponding Sql type Mixin will be determined from default value type.
Here is the list of coresponding Sql type for each Ruby class handled :
-
Integer, Fixnum <=> Sqlyzer::Parameter::Types::Int (SQL_INT),
-
Float <=> Sqlyzer::Parameter::Types::Float (SQL_FLOAT),
-
Time <=> Sqlyzer::Parameter::Types::Date (SQL_DATE),
-
TrueClass, FalseClass <=> Sqlyzer::Parameter::Types::Bool (SQL_BOOL),
-
Range <=> Sqlyzer::Parameter::Types::VarChar (SQL_VARCHAR).
Any other Ruby class will be considered as a Sqlyzer::Parameter::Types::Text (SQL_TEXT).
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 |
# File 'lib/sqlyzer/parameters.rb', line 308 def Default.get_from_default(symbol, val) res = Default.new(symbol) case val when Object::Integer, Object::Fixnum then res.extend Types::Alias::SQL_INT when Object::Float then res.extend Types::Alias::SQL_FLOAT when Object::Time then res.extend Types::Alias::SQL_DATE when Object::TrueClass, Object::FalseClass then res.extend Types::Alias::SQL_BOOL when Object::Range then res.extend Types::Alias::SQL_VARCHAR else res.extend Types::Alias::SQL_TEXT end res/val end |
Instance Method Details
#/(default) ⇒ Object
Call sql_parameter_default and return self.
257 258 259 260 |
# File 'lib/sqlyzer/parameters.rb', line 257 def /(default) sql_parameter_default default self end |
#<=>(other) ⇒ Object
Implement the Ruby Comparable Mixin, enabling to compare Default with Default, Symbol or String (in this order).
244 245 246 247 248 249 250 251 252 |
# File 'lib/sqlyzer/parameters.rb', line 244 def <=>(other) if other.kind_of?(Default) @symbol.to_i <=> other.symbol.to_i elsif other.kind_of?(Symbol) @symbol.to_i <=> other.to_i else @symbol.to_s <=> other.to_s end end |
#to_owner(owner, data) ⇒ Object
Unserialize Sql representation of data to assign it to owner.
289 290 291 |
# File 'lib/sqlyzer/parameters.rb', line 289 def to_owner(owner, data) sql_parameter_set(owner, data) end |
#to_s ⇒ Object
Call contained Symbol to_s method.
265 266 267 |
# File 'lib/sqlyzer/parameters.rb', line 265 def to_s @symbol.to_s end |
#to_sql(owner) ⇒ Object
Serialize the Symbol value contained in owner to Sql representation.
282 283 284 |
# File 'lib/sqlyzer/parameters.rb', line 282 def to_sql(owner) sql_parameter_eval(owner).to_s end |
#to_sql_type ⇒ Object
Return the Sql type name of the current instance.
This method will raise an error unless current instance was extend with coresponding Sql type Mixin.
275 276 277 |
# File 'lib/sqlyzer/parameters.rb', line 275 def to_sql_type raise 'unreached' end |