Class: SeparatedValues::Row

Inherits:
Object
  • Object
show all
Defined in:
lib/separated_values/row.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(values, options = DEFAULT_OPTIONS) ⇒ Row

Returns a new instance of Row.



5
6
7
8
# File 'lib/separated_values/row.rb', line 5

def initialize(values, options = DEFAULT_OPTIONS)
  @values = values
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



20
21
22
23
# File 'lib/separated_values/row.rb', line 20

def method_missing(name, *args, &block)
  return to_h[name] if respond_to_missing?(name, *args)
  super
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/separated_values/row.rb', line 3

def options
  @options
end

Instance Method Details

#[](index) ⇒ Object



10
11
12
# File 'lib/separated_values/row.rb', line 10

def [](index)
  @values[index]
end

#respond_to_missing?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/separated_values/row.rb', line 25

def respond_to_missing?(name, *args)
  !!options[:schema] && options[:schema].include?(name)
end

#to_h(schema = options[:schema]) ⇒ Object



14
15
16
17
18
# File 'lib/separated_values/row.rb', line 14

def to_h(schema = options[:schema])
  schema.each_with_object({}).with_index do |(key, hash), i|
    hash[key] = @values[i]
  end
end