Module: Watir::LoadView

Included in:
Search
Defined in:
lib/watir-search/load_view.rb

Instance Method Summary collapse

Instance Method Details

#decode_hash(hash) ⇒ Object



31
32
33
34
35
36
37
38
# File 'lib/watir-search/load_view.rb', line 31

def decode_hash(hash)
  rehash(hash)
  new_hash = @new_hash
  %w(key value hash).each do |var|
    instance_variable_set("@new_#{var}", nil)
  end
  new_hash
end

#decode_key(parent_key, key) ⇒ Object



5
6
7
# File 'lib/watir-search/load_view.rb', line 5

def decode_key(parent_key, key)
  "#{parent_key}_#{key}".gsub(/^_/, '')
end

#load_view(file: nil, hash: nil, parent: nil) ⇒ Object



64
65
66
67
# File 'lib/watir-search/load_view.rb', line 64

def load_view(file:nil, hash:nil, parent:nil)
  hash ||= decode_hash(original_hash(file))
  view_array(hash).each { |h| view_accessor(parent, h) }
end

#original_hash(file) ⇒ Object



60
61
62
# File 'lib/watir-search/load_view.rb', line 60

def original_hash(file)
  YAML.load_file(File.join(File.dirname(__FILE__), file))
end

#read_new_hash(parent_key, key, value) ⇒ Object



9
10
11
12
# File 'lib/watir-search/load_view.rb', line 9

def read_new_hash(parent_key, key, value)
  @new_key = decode_key(parent_key, key)
  @new_value = value
end

#rehash(hash, parent_key = nil) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/watir-search/load_view.rb', line 19

def rehash(hash, parent_key = nil)
  hash.each do |key, value|
    if value.is_a?(Hash)
      read_new_hash(parent_key, key, value)
      rehash(value, decode_key(parent_key, key))
    else
      write_out_new_hash
      break
    end
  end
end

#view_accessor(parent, hash) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/watir-search/load_view.rb', line 40

def view_accessor(parent, hash)
  attributes = hash.reject { |k, _v| %i(var_name node).include?(k) }
  instance_variable_set(
    "@#{hash[:var_name]}", parent.send(hash[:node], attributes))
  define_singleton_method(hash[:var_name]) do
    instance_variable_get("@#{hash[:var_name]}")
  end
end

#view_array(hash) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/watir-search/load_view.rb', line 49

def view_array(hash)
  array = []
  hash.define_singleton_method(:to_attr) do
    first = shift
    str_hash = { var_name: first[0] }.merge(first[1])
    Hash[str_hash.map { |k, v| [k.to_sym, v] }]
  end
  hash.each { array << hash.to_attr }
  array
end

#write_out_new_hashObject



14
15
16
17
# File 'lib/watir-search/load_view.rb', line 14

def write_out_new_hash
  @new_hash = {} unless @new_hash
  @new_hash[@new_key] = @new_value
end