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.

Direct Known Subclasses

MergeField

Constant Summary

Constants inherited from Data

Data::TYPE_CONVERSION_TREE

Instance Attribute Summary collapse

Attributes inherited from Data

#dataset, #name

Instance Method Summary collapse

Methods inherited from Data

#database_type, #merge

Constructor Details

#initialize(dataset, name, schema) ⇒ Field

Create a new instance of Field.

Parameters:

  • dataset (Linkage::Dataset)
  • name (Symbol)

    The field’s name

  • schema (Hash)

    The field’s schema information



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

#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

#collationObject



87
88
89
# File 'lib/linkage/field.rb', line 87

def collation
  schema[:collation]
end

#primary_key?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/linkage/field.rb', line 83

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
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

Returns:

  • (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(options = {})
  @name
end