Module: Spark::Helpers
Instance Method Summary collapse
- #dasherize(input) ⇒ Object
- #dasherize_keys(obj) ⇒ Object
-
#set_aria_keys(options, tl_keys = []) ⇒ Object
Convert aria: { key: val } to ‘aria-key’ => val.
-
#set_data_keys(options = {}, tl_keys = []) ⇒ Object
Move top level keys under data:.
- #set_type_defaults(type, options = {}) ⇒ Object
-
#sub_key(options, tl_keys, parent) ⇒ Object
Move top level keys under new parent key.
Instance Method Details
#dasherize(input) ⇒ Object
63 64 65 |
# File 'lib/fleetio_spark/helper.rb', line 63 def dasherize(input) input.strip.gsub(/[\W,_]+/, '-') end |
#dasherize_keys(obj) ⇒ Object
59 60 61 |
# File 'lib/fleetio_spark/helper.rb', line 59 def dasherize_keys(obj) obj.deep_transform_keys{ |key| dasherize(key.to_s) } end |
#set_aria_keys(options, tl_keys = []) ⇒ Object
Convert aria: { key: val } to ‘aria-key’ => val
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/fleetio_spark/helper.rb', line 16 def set_aria_keys( , tl_keys=[] ) # Add 'aria' parent to passed top level keys tl_keys = tl_keys.split(' ') if tl_keys.is_a? String tl_keys = tl_keys | DEFAULT_ARIA_KEYS = sub_key , tl_keys, 'aria' opts = {} ['aria'].each do |key, val| opts["aria-#{key}"] = val end .delete 'aria' .merge! opts end |
#set_data_keys(options = {}, tl_keys = []) ⇒ Object
Move top level keys under data:
9 10 11 12 13 |
# File 'lib/fleetio_spark/helper.rb', line 9 def set_data_keys( ={}, tl_keys=[] ) tl_keys = tl_keys.split(' ') if tl_keys.is_a? String tl_keys = tl_keys | DEFAULT_DATA_KEYS sub_key , tl_keys, 'data' end |
#set_type_defaults(type, options = {}) ⇒ Object
55 56 57 |
# File 'lib/fleetio_spark/helper.rb', line 55 def set_type_defaults( type, ={} ) ( INPUT_OPTIONS[type] || {} ).deep_merge end |
#sub_key(options, tl_keys, parent) ⇒ Object
Move top level keys under new parent key
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/fleetio_spark/helper.rb', line 36 def sub_key(, tl_keys, parent) = dasherize_keys( ) tl_keys = tl_keys.split(' ') if tl_keys.is_a? String parent = parent.to_s if parent.is_a?( Symbol ) [parent] ||= {} new_hash = {} # Look through option keys and reparent keys which match dasherized search keys .each do |k,v| new_hash[dasherize k] = .delete k if tl_keys.include? dasherize k end [parent].merge! new_hash end |