Class: SuperList::Data

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

Overview

Data Store

Instance Method Summary collapse

Constructor Details

#initialize(values, options) ⇒ Data

Returns a new instance of Data.



27
28
29
# File 'lib/super_list.rb', line 27

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

Instance Method Details

#get_key(value, opts = {}) ⇒ Object



51
52
53
# File 'lib/super_list.rb', line 51

def get_key(value, opts={})
  keys[values(opts).index(value)] rescue nil
end

#get_value(key, format = nil, opts = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
# File 'lib/super_list.rb', line 40

def get_value(key, format=nil, opts={})
  opts = options.merge(opts)

  value = @values[key]
  format = format || opts[:format] || :default
  result = value.is_a?(Hash) ? value[format] : value

  return I18n.t(result, :scope => opts[:i18n_scope], :default => opts[:i18n_default], :locale => opts[:locale]) if opts[:use_i18n]
  result
end

#keysObject



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

def keys
  @values.keys
end

#map(format = nil, opts = {}, &blk) ⇒ Object



55
56
57
58
59
60
61
# File 'lib/super_list.rb', line 55

def map(format=nil, opts={}, &blk)
  if block_given?
    keys.zip(values(format, opts)).map &blk
  else
    keys.zip(values(format, opts))
  end
end

#optionsObject



71
72
73
# File 'lib/super_list.rb', line 71

def options
  SuperList.options.merge(@options)
end

#select_options(format = nil, opts = {}, &blk) ⇒ Object



63
64
65
66
67
68
69
# File 'lib/super_list.rb', line 63

def select_options(format=nil, opts={}, &blk)
  if block_given?
    values(format, opts).zip(keys).map &blk
  else
    values(format, opts).zip(keys)
  end
end

#values(format = nil, opts = {}) ⇒ Object



35
36
37
38
# File 'lib/super_list.rb', line 35

def values(format=nil, opts={})
  opts, format = format, nil if format.is_a?(Hash)
  keys.map {|key| get_value(key, format, opts) }
end