Class: WatirModel

Inherits:
Object
  • Object
show all
Extended by:
DataConversions, YmlReader
Defined in:
lib/watir_model.rb

Direct Known Subclasses

ConfigModel

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DataConversions

convert_to_array, convert_to_boolean, convert_to_float, convert_to_hash, convert_to_integer, convert_to_string, convert_to_symbol, convert_to_time, parse_json

Constructor Details

#initialize(hash = {}) ⇒ WatirModel

Returns a new instance of WatirModel.



112
113
114
115
116
117
118
119
120
121
122
# File 'lib/watir_model.rb', line 112

def initialize(hash = {})
  hash.deep_symbolize_keys!
  update(hash)

  (self.class.defaults.keys - hash.keys).each do |key|
    block = self.class.defaults[key]
    value = default_value(key, block)
    value = self.class.convert_type(key, value)
    instance_variable_set("@#{key}", value)
  end
end

Class Attribute Details

.apisObject



26
27
28
# File 'lib/watir_model.rb', line 26

def apis
  @apis ||= {}
end

.data_typesObject



22
23
24
# File 'lib/watir_model.rb', line 22

def data_types
  @data_types ||= {}
end

.defaultsObject



30
31
32
# File 'lib/watir_model.rb', line 30

def defaults
  @defaults ||= {}
end

.keysObject



18
19
20
# File 'lib/watir_model.rb', line 18

def keys
  @keys ||= []
end

Class Method Details

.convert(hash, *args) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/watir_model.rb', line 69

def convert(hash, *args)
  hash.deep_symbolize_keys!
  filtered = hash.select { |k| valid_keys.include?(k) }
  unless (defaults.keys - default_value_keys(filtered)).empty?
    raise StandardError, "Can not convert Hash to Model when keys with default values are missing"
  end
  model = new(filtered)
  args.each do |key|
    model.instance_eval do
      define_singleton_method(key) { hash[key] }
    end
  end
  model
end

.convert_type(key, value) ⇒ Object

Raises:

  • (StandardError)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/watir_model.rb', line 53

def convert_type(key, value)
  data_type = data_types[key]
  return value if data_type.nil?
  return value if data_type.is_a?(Class) && value.is_a?(data_type)
  method = "convert_to_#{data_type.to_s.underscore.tr('/', '_')}"
  value = if respond_to? method
            send(method, value)
          else
            file = factory_file(data_type)
            data = data_from_yaml(file, value) || value
            data_type.new(data)
          end
  return value unless value.nil?
  raise StandardError, "Unable to convert #{value} to #{data_type}"
end

.data_from_yaml(file, value) ⇒ Object



106
107
108
109
# File 'lib/watir_model.rb', line 106

def data_from_yaml(file, value)
  return nil if file.nil? || !(value.is_a?(Symbol) || value.is_a?(String))
  YAML.load_file(file)[value.to_sym]
end

.default_directoryObject



84
85
86
# File 'lib/watir_model.rb', line 84

def default_directory
  'config/data'
end

.default_value_keys(hash) ⇒ Object



88
89
90
91
92
# File 'lib/watir_model.rb', line 88

def default_value_keys(hash)
  hash.keys.map do |key|
    keys.include?(key) ? key : apis[key]
  end
end

.factory_file(type) ⇒ Object



102
103
104
# File 'lib/watir_model.rb', line 102

def factory_file(type)
  Dir.glob("#{WatirModel.yml_directory}/#{type.to_s[/[^:]*$/].underscore}.yml").first
end

.inherited(subclass) ⇒ Object



38
39
40
41
42
43
# File 'lib/watir_model.rb', line 38

def inherited(subclass)
  subclass.keys = keys.dup
  subclass.apis = apis.dup
  subclass.defaults = defaults.dup
  subclass.data_types = data_types.dup
end

.key(symbol, data_type: nil, api: nil, &block) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/watir_model.rb', line 45

def key(symbol, data_type: nil, api: nil, &block)
  keys << symbol unless @keys.include? symbol
  attr_accessor symbol
  apis[api] = symbol if api
  data_types[symbol] = data_type if data_type
  defaults[symbol] = block if block
end

.method_missing(method, *args, &block) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
# File 'lib/watir_model.rb', line 94

def method_missing(method, *args, &block)
  file = factory_file(self)
  return super unless file
  data = data_from_yaml(file, method)
  raise ArgumentError, "Factory '#{method}' does not exist in '#{file}'" if data.nil?
  new(data)
end

.valid_keysObject



34
35
36
# File 'lib/watir_model.rb', line 34

def valid_keys
  keys + apis.keys
end

Instance Method Details

#[](key) ⇒ Object



152
153
154
# File 'lib/watir_model.rb', line 152

def [] key
  send key
end

#apisObject



144
145
146
# File 'lib/watir_model.rb', line 144

def apis
  self.class.apis
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


156
157
158
# File 'lib/watir_model.rb', line 156

def eql?(other)
  keys.all? { |k| send(k) == other[k] }
end

#keysObject



140
141
142
# File 'lib/watir_model.rb', line 140

def keys
  self.class.keys
end

#to_apiObject



182
183
184
185
186
187
188
# File 'lib/watir_model.rb', line 182

def to_api
  hash = to_h
  apis.each do |key, value|
    hash[key] = hash.delete(value)
  end
  hash.to_json
end

#to_h(opt = nil) ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
# File 'lib/watir_model.rb', line 166

def to_h(opt = nil)
  opt ||= keys
  opt.each_with_object({}) do |key, hash|
    value = send(key)
    next if value.nil?
    method = "convert_from_#{value.class.to_s.underscore.tr('/', '_')}"
    value = send(method, value) if respond_to?(method)
    value = value.to_h if value.is_a? WatirModel
    hash[key] = value
  end
end

#to_hash(opt = nil) ⇒ Object



161
162
163
164
# File 'lib/watir_model.rb', line 161

def to_hash(opt = nil)
  warn "#to_hash is deprecated, use #to_h instead"
  to_h opt
end

#to_jsonObject



178
179
180
# File 'lib/watir_model.rb', line 178

def to_json(*)
  to_h.to_json
end

#update(hash) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/watir_model.rb', line 124

def update(hash)
  hash ||= {}

  (hash.keys & apis.keys).each do |api_key|
    hash[apis[api_key]] = hash.delete(api_key)
  end

  unknown = hash.keys - keys
  if unknown.count > 0
    raise ArgumentError, "unknown keyword#{'s' if unknown.count > 1}: #{unknown.join ', '}"
  end
  hash.each do |key, val|
    instance_variable_set "@#{key}", self.class.convert_type(key, val)
  end
end

#valid_keysObject



148
149
150
# File 'lib/watir_model.rb', line 148

def valid_keys
  self.class.valid_keys
end