Class: Linkage::FieldSet

Inherits:
Hash
  • Object
show all
Defined in:
lib/linkage/field_set.rb

Overview

FieldSet is a Hash of Field values. It is usually associated with a Dataset. It looks up keys in a case-insensitive manner and doesn't care if you use strings or symbols.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dataset) ⇒ FieldSet

Create a new FieldSet.

Parameters:



14
15
16
17
18
19
20
21
22
23
# File 'lib/linkage/field_set.rb', line 14

def initialize(dataset)
  dataset.schema.each do |(name, column_schema)|
    field = Field.new(name, column_schema)
    self[name] = field

    if @primary_key.nil? && column_schema[:primary_key]
      @primary_key = field
    end
  end
end

Instance Attribute Details

#primary_keyField (readonly)

Returns primary key of this field set.

Returns:

  • (Field)

    primary key of this field set.



9
10
11
# File 'lib/linkage/field_set.rb', line 9

def primary_key
  @primary_key
end

Instance Method Details

#[](key) ⇒ Field

Returns the value for key, where key is matched in a case-insensitive manner.

Parameters:

  • key (String, Symbol)

Returns:



48
49
50
51
# File 'lib/linkage/field_set.rb', line 48

def [](key)
  k = fetch_key(key)
  k ? super(k) : nil
end

#fetch_key(key) ⇒ Symbol

Returns a key that matches the parameter in a case-insensitive manner.

Parameters:

  • key (String, Symbol)

Returns:

  • (Symbol)


38
39
40
41
# File 'lib/linkage/field_set.rb', line 38

def fetch_key(key)
  string_key = key.to_s
  keys.detect { |k| k.to_s.casecmp(string_key) == 0 }
end

#has_key?(key) ⇒ Boolean

Returns whether or not key is contained in the field set (case-insensitive).

Parameters:

  • key (String, Symbol)

Returns:

  • (Boolean)


30
31
32
# File 'lib/linkage/field_set.rb', line 30

def has_key?(key)
  !fetch_key(key).nil?
end