Class: OptionListMapper
- Inherits:
-
Object
- Object
- OptionListMapper
- Defined in:
- lib/option_list_mapper.rb
Instance Attribute Summary collapse
-
#modified_options ⇒ Object
simple should return array of hashes => value list should return array of one dimensional hashes that the value is an array => [value, value] optionally list may return a value that is a hash that will be converted to simple or list.
Class Method Summary collapse
- .create_options(options_list = "", kvs = [], singles = [], opts = {}) ⇒ Object
- .map_single(key, value) ⇒ Object
- .map_to_string(string, key, value, display_brackets = true, display_key = true) ⇒ Object
Instance Method Summary collapse
-
#initialize(options_list, kvs, singles, opts) ⇒ OptionListMapper
constructor
A new instance of OptionListMapper.
Constructor Details
#initialize(options_list, kvs, singles, opts) ⇒ OptionListMapper
Returns a new instance of OptionListMapper.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/option_list_mapper.rb', line 6 def initialize(,kvs,singles,opts) = opts.dup @modified_options = kvs.each do |key| next if [key].nil? OptionListMapper.map_to_string(@modified_options, key, [key]) @modified_options << ' ' end @modified_options << singles.map{|key| OptionListMapper.map_single(key, [key])}.join(' ') end |
Instance Attribute Details
#modified_options ⇒ Object
simple should return array of hashes => value list should return array of one dimensional hashes that the value is an array => [value, value] optionally list may return a value that is a hash that will be converted to simple or list
5 6 7 |
# File 'lib/option_list_mapper.rb', line 5 def @modified_options end |
Class Method Details
.create_options(options_list = "", kvs = [], singles = [], opts = {}) ⇒ Object
19 20 21 |
# File 'lib/option_list_mapper.rb', line 19 def (="",kvs=[],singles=[],opts={}) new(,kvs,singles,opts)..strip end |
.map_single(key, value) ⇒ Object
48 49 50 51 |
# File 'lib/option_list_mapper.rb', line 48 def map_single(key, value) return '' if value.nil? "#{key} #{value}" end |
.map_to_string(string, key, value, display_brackets = true, display_key = true) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/option_list_mapper.rb', line 23 def map_to_string(string, key, value, display_brackets=true, display_key=true) string << "#{key}=" if display_key if not value.is_a?(Array) and not value.is_a?(Hash) string << value.to_s elsif value.is_a?(Array) string << '{' if display_brackets value.each_with_index{|v, index| if v.is_a?(Array) map_to_string(string, key, v, true, false) elsif v.is_a?(Hash) map_to_string(string, key, v, false, false) else string << v.to_s end string << ' ' unless index == value.size - 1 } string << '}' if display_brackets elsif value.is_a?(Hash) string << '{' if display_brackets map_to_string(string, value.keys.first, value.values.first) string << '}' if display_brackets end string end |