Class: Linkage::Field
Overview
This class is for holding information about a particular field in a dataset.
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
-
#initialize(name, schema, ruby_type = nil) ⇒ 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 ⇒ Object
Methods inherited from Data
Constructor Details
#initialize(name, schema, ruby_type = nil) ⇒ Field
Create a new instance of Field.
13 14 15 16 17 |
# File 'lib/linkage/field.rb', line 13 def initialize(name, schema, ruby_type = nil) @name = name @schema = schema @ruby_type = ruby_type 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
#primary_key? ⇒ Boolean
81 82 83 |
# File 'lib/linkage/field.rb', line 81 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 |
# 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.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
77 78 79 |
# File 'lib/linkage/field.rb', line 77 def static? false end |
#to_expr ⇒ Object
73 74 75 |
# File 'lib/linkage/field.rb', line 73 def to_expr @name end |