Class: Linkage::Field
Overview
This class is for holding information about a particular field in a dataset.
Direct Known Subclasses
Constant Summary
Constants inherited from Data
Instance Attribute Summary collapse
-
#schema ⇒ Symbol
readonly
This field’s schema information.
Attributes inherited from Data
Instance Method Summary collapse
- #collation ⇒ Object
-
#initialize(dataset, name, schema) ⇒ Field
constructor
Create a new instance of Field.
- #primary_key? ⇒ Boolean
-
#ruby_type ⇒ Object
Convert the column schema information to a hash of column options, one of which must be :type.
- #static? ⇒ Boolean
- #to_expr(options = {}) ⇒ Object
Methods inherited from Data
Constructor Details
#initialize(dataset, name, schema) ⇒ Field
Create a new instance of Field.
13 14 15 16 17 |
# File 'lib/linkage/field.rb', line 13 def initialize(dataset, name, schema) @dataset = dataset @name = name @schema = schema end |
Instance Attribute Details
#schema ⇒ Symbol (readonly)
Returns This field’s schema information.
6 7 8 |
# File 'lib/linkage/field.rb', line 6 def schema @schema end |
Instance Method Details
#collation ⇒ Object
87 88 89 |
# File 'lib/linkage/field.rb', line 87 def collation schema[:collation] end |
#primary_key? ⇒ Boolean
83 84 85 |
# File 'lib/linkage/field.rb', line 83 def primary_key? schema && schema[:primary_key] end |
#ruby_type ⇒ Object
Note:
This method comes more or less straight from Sequel (lib/sequel/extensions/schema_dumper.rb).
Convert the column schema information to a hash of column options, one of which must be :type. The other options added should modify that type (e.g. :size). If a database type is not recognized, return it as a String type.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/linkage/field.rb', line 26 def ruby_type unless @ruby_type hsh = case t = @schema[:db_type].downcase when /\A(?:medium|small)?int(?:eger)?(?:\((?:\d+)\))?(?: unsigned)?\z/o {:type=>Integer} when /\Atinyint(?:\((\d+)\))?\z/o {:type =>@schema[:type] == :boolean ? TrueClass : Integer} when /\Abigint(?:\((?:\d+)\))?(?: unsigned)?\z/o {:type=>Bignum} when /\A(?:real|float|double(?: precision)?)\z/o {:type=>Float} when 'boolean' {:type=>TrueClass} when /\A(?:(?:tiny|medium|long|n)?text|clob)\z/o {:type=>String, :text=>true} when 'date' {:type=>Date} when /\A(?:small)?datetime\z/o {:type=>DateTime} when /\Atimestamp(?:\((\d+)\))?(?: with(?:out)? time zone)?\z/o {:type=>DateTime, :size=>($1.to_i if $1)} when /\Atime(?: with(?:out)? time zone)?\z/o {:type=>Time, :only_time=>true} when /\An?char(?:acter)?(?:\((\d+)\))?\z/o {:type=>String, :size=>($1.to_i if $1), :fixed=>true} when /\A(?:n?varchar|character varying|bpchar|string)(?:\((\d+)\))?\z/o {:type=>String, :size=>($1.to_i if $1)} when /\A(?:small)?money\z/o {:type=>BigDecimal, :size=>[19,2]} when /\A(?:decimal|numeric|number)(?:\((\d+)(?:,\s*(\d+))?\))?\z/o s = [($1.to_i if $1), ($2.to_i if $2)].compact {:type=>BigDecimal, :size=>(s.empty? ? nil : s)} when /\A(?:bytea|(?:tiny|medium|long)?blob|(?:var)?binary)(?:\((\d+)\))?\z/o {:type=>File, :size=>($1.to_i if $1)} when 'year' {:type=>Integer} else {:type=>String} end hsh[:collate] = collation hsh.delete_if { |k, v| v.nil? } @ruby_type = {:type => hsh.delete(:type)} @ruby_type[:opts] = hsh if !hsh.empty? end @ruby_type end |
#static? ⇒ Boolean
79 80 81 |
# File 'lib/linkage/field.rb', line 79 def static? false end |
#to_expr(options = {}) ⇒ Object
75 76 77 |
# File 'lib/linkage/field.rb', line 75 def to_expr( = {}) @name end |