Class: RStore::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/rstore/configuration.rb

Constant Summary collapse

Validations =

Validations for RStore::CSV specific options

Hash.new { |h,k| lambda { |value| true }}.
      merge!({recursive:   lambda { |value| value.boolean_or_nil? },
has_headers: lambda { |value| value.boolean_or_nil? },
selector:    lambda { |value| value.is_a?(String) }})

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, options) ⇒ Configuration

Returns a new instance of Configuration.

Raises:

  • (ArgumentError)


34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rstore/configuration.rb', line 34

def initialize path, options
  new_options  = options.dup

  @path        = path
  self.options = new_options
  # options= deletes all valid options already processed from new_options,
  # leaving only options that are not valid.
  # Therefore, new_options.size > 0 indicates that options not recognized where passed.
  raise ArgumentError, arg_error_message(@path, new_options) if new_options.size > 0

  @file_options  = extract_with(Configuration.default_file_options)
  @parse_options = extract_with(Configuration.default_parse_options)
end

Class Attribute Details

.default_file_optionsObject (readonly)

Returns the value of attribute default_file_options.



8
9
10
# File 'lib/rstore/configuration.rb', line 8

def default_file_options
  @default_file_options
end

.default_optionsObject (readonly)

Returns the value of attribute default_options.



10
11
12
# File 'lib/rstore/configuration.rb', line 10

def default_options
  @default_options
end

.default_parse_optionsObject (readonly)

Returns the value of attribute default_parse_options.



9
10
11
# File 'lib/rstore/configuration.rb', line 9

def default_parse_options
  @default_parse_options
end

Instance Attribute Details

#file_optionsObject (readonly)

Returns the value of attribute file_options.



29
30
31
# File 'lib/rstore/configuration.rb', line 29

def file_options
  @file_options
end

#optionsObject

Returns the value of attribute options.



28
29
30
# File 'lib/rstore/configuration.rb', line 28

def options
  @options
end

#parse_optionsObject (readonly)

Returns the value of attribute parse_options.



30
31
32
# File 'lib/rstore/configuration.rb', line 30

def parse_options
  @parse_options
end

#pathObject (readonly)

Returns the value of attribute path.



31
32
33
# File 'lib/rstore/configuration.rb', line 31

def path
  @path
end

Class Method Details

.change_default_options(options) ⇒ Object

Raises:

  • (ArgumentError)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rstore/configuration.rb', line 81

def self.change_default_options options
  raise ArgumentError, "#{options} must be an instance of Hash" unless options.is_a?(Hash)
  new_options = Configuration.default_options.merge(options)
  raise ArgumentError, "#{options} contains unknown option key" if new_options.size > Configuration.default_options.size
  new_options.each do |option, value|
    error_message = "'#{value}' (#{value.class}) is not a valid value for option #{option.inspect}"
    raise ArgumentError, error_message unless valid_value?(option, value)
  end

  @default_options = new_options
end

.reset_default_optionsObject



94
95
96
# File 'lib/rstore/configuration.rb', line 94

def self.reset_default_options
  @default_options = @default_file_options.merge(@default_parse_options)
end

.valid_value?(option, value) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
# File 'lib/rstore/configuration.rb', line 115

def self.valid_value? option, value
  Validations[option][value]
end

Instance Method Details

#[](key) ⇒ Object



99
100
101
102
103
104
105
106
107
108
# File 'lib/rstore/configuration.rb', line 99

def [] key
  target = instance_variables.find do |var|
    var.to_s.gsub(/@/,'').to_sym == key
  end
  if target
    instance_variable_get(target)
  else
    raise ArgumentError, "'#{key}' is not an instance variable"
  end
end

#arg_error_message(path, new_options) ⇒ Object



120
121
122
123
# File 'lib/rstore/configuration.rb', line 120

def arg_error_message path, new_options
  keys = new_options.keys.join(', ')
  "Unsupported options: #{keys} for path '#{path}'"
end

#extract_with(options) ⇒ Object



69
70
71
72
73
74
75
76
77
78
# File 'lib/rstore/configuration.rb', line 69

def extract_with options
  keys = options.keys
  @options.inject({}) do |acc, (option, value)|
    if keys.include?(option)
      acc[option] = value
    end

  acc
  end
end