Class: Arrow::Field

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, data_type, nullable = false) ⇒ Field #initialize(description) ⇒ Field

Creates a new Arrow::Field.

Overloads:

  • #initialize(name, data_type, nullable = false) ⇒ Field

    Examples:

    Create a field with DataTypes

    Arrow::Field.new("visible", Arrow::BooleanDataType.new)

    Create a field with data type description

    Arrow::Field.new("visible", :boolean)

    Create a field with name as ‘Symbol`

    Arrow::Field.new(:visible, :boolean)

    Parameters:

    • name (String, Symbol)

      The name of the field.

    • data_type (Arrow::DataType, Hash, String, Symbol)

      The data type of the field.

      You can specify data type as a description by ‘Hash`.

      See DataType.resolve how to specify data type description.

    • nullable (Boolean, nil) (defaults to: false)

      (false) Whether the field may contain zero or more nulls.

      ‘nil` is processed as `true` (null may be contained).

  • #initialize(description) ⇒ Field

    Examples:

    Create a field with DataTypes

    Arrow::Field.new(name: "visible",
                     data_type: Arrow::BooleanDataType.new)

    Create a field with data type description

    Arrow::Field.new(name: "visible", data_type: {type: :boolean}

    Create a field with shortcut form

    Arrow::Field.new(name: "visible", type: :boolean)

    Parameters:

    • description (Hash)

      The description of the field.

      Field description is a raw ‘Hash`. Field description must have `:name` and `:data_type` values. `:name` is the name of the field. `:data_type` is the data type of the field. You can use DataType or data type description as `:data_type` value.

      See DataType.resolve how to specify data type description.

      There is a shortcut for convenience. If field description doesn’t have ‘:data_type`, all keys except `:name` are processes as data type description. For example, the following field descriptions are the same:

      “‘ruby “visible”, data_type: {type: :boolean} “visible”, type: :boolean # Shortcut version “`

    Options Hash (description):

    • :name (String, Symbol)

      The name of the field.

    • :data_type (Arrow::DataType, Hash)

      The data type of the field. You can specify data type description by ‘Hash`.

      See DataType.resolve how to specify data type description.

    • :nullable (Boolean, nil)

      Whether the field may contain zero or more nulls.

      ‘nil` is processed as `true` (null may be contained).



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/arrow/field.rb', line 113

def initialize(*args)
  n_args = args.size
  case n_args
  when 1
    description = args[0]
    name = nil
    data_type = nil
    data_type_description = {}
    nullable = nil
    description.each do |key, value|
      key = key.to_sym
      case key
      when :name
        name = value
      when :data_type
        data_type = DataType.resolve(value)
      when :nullable
        nullable = value
      else
        data_type_description[key] = value
      end
    end
    data_type ||= DataType.resolve(data_type_description)
  when 2
    name = args[0]
    data_type = args[1]
    if data_type.is_a?(Hash)
      data_type = data_type.dup
      nullable = data_type.delete(:nullable)
    else
      nullable = nil
    end
    data_type = DataType.resolve(data_type)
  when 3
    name = args[0]
    data_type = DataType.resolve(args[1])
    nullable = args[2]
  else
    message = "wrong number of arguments (given #{n_args}, expected 1..3)"
    raise ArgumentError, message
  end
  nullable = true if nullable.nil?

  initialize_raw(name, data_type, nullable)
end

Class Method Details

.try_convert(value) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/arrow/field.rb', line 22

def try_convert(value)
  case value
  when Hash
    begin
      new(value)
    rescue ArgumentError
      nil
    end
  else
    nil
  end
end