Class: Linkage::Field

Inherits:
Data
  • Object
show all
Defined in:
lib/linkage/field.rb

Overview

This class is for holding information about a particular field in a dataset.

Constant Summary

Constants inherited from Data

Data::TYPE_CONVERSION_TREE

Instance Attribute Summary collapse

Attributes inherited from Data

#name

Instance Method Summary collapse

Methods inherited from Data

#merge

Constructor Details

#initialize(name, schema, ruby_type = nil) ⇒ Field

Create a new instance of Field.

Parameters:

  • name (Symbol)

    The field’s name

  • schema (Hash)

    The field’s schema information

  • ruby_type (Hash) (defaults to: nil)

    The field’s ruby type



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

#schemaSymbol (readonly)

Returns This field’s schema information.

Returns:

  • (Symbol)

    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

Returns:

  • (Boolean)


81
82
83
# File 'lib/linkage/field.rb', line 81

def primary_key?
  schema && schema[:primary_key]
end

#ruby_typeObject

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

Returns:

  • (Boolean)


77
78
79
# File 'lib/linkage/field.rb', line 77

def static?
  false
end

#to_exprObject



73
74
75
# File 'lib/linkage/field.rb', line 73

def to_expr
  @name
end