Module: Spark::Helpers

Extended by:
Helpers
Included in:
Helpers
Defined in:
lib/fleetio_spark/helper.rb

Instance Method Summary collapse

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( options, 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

  options = sub_key options, tl_keys, 'aria'
  opts = {}

  options['aria'].each do |key, val|
    opts["aria-#{key}"] = val
  end

  options.delete 'aria'
  options.merge! opts

  options
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( options={}, tl_keys=[] )
  tl_keys = tl_keys.split(' ') if tl_keys.is_a? String
  tl_keys = tl_keys | DEFAULT_DATA_KEYS
  sub_key options, 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, options={} )
  ( INPUT_OPTIONS[type] || {} ).deep_merge options
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(options, tl_keys, parent)
  options = dasherize_keys( options )
  tl_keys = tl_keys.split(' ') if tl_keys.is_a? String

  parent = parent.to_s if parent.is_a?( Symbol ) 
  options[parent] ||= {}

  new_hash = {}

  # Look through option keys and reparent keys which match dasherized search keys
  options.each do |k,v|
    new_hash[dasherize k] = options.delete k if tl_keys.include? dasherize k
  end

  options[parent].merge! new_hash
  options

end