class WatirModel

Attributes

apis[W]
data_types[W]
defaults[W]
keys[W]

Public Class Methods

apis() click to toggle source
# File lib/watir_model.rb, line 26
def apis
  @apis ||= {}
end
convert(hash, *args) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
data_types() click to toggle source
# File lib/watir_model.rb, line 22
def data_types
  @data_types ||= {}
end
default_directory() click to toggle source
# File lib/watir_model.rb, line 84
def default_directory
  'config/data'
end
default_value_keys(hash) click to toggle source
# 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
defaults() click to toggle source
# File lib/watir_model.rb, line 30
def defaults
  @defaults ||= {}
end
factory_file(type) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
keys() click to toggle source
# File lib/watir_model.rb, line 18
def keys
  @keys ||= []
end
method_missing(method, *args, &block) click to toggle source
Calls superclass method
# 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
new(hash = {}) click to toggle source
# 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
valid_keys() click to toggle source
# File lib/watir_model.rb, line 34
def valid_keys
  keys + apis.keys
end

Public Instance Methods

==(other)
Alias for: eql?
[](key) click to toggle source
# File lib/watir_model.rb, line 152
def [] key
  send key
end
apis() click to toggle source
# File lib/watir_model.rb, line 144
def apis
  self.class.apis
end
eql?(other) click to toggle source
# File lib/watir_model.rb, line 156
def eql?(other)
  keys.all? { |k| send(k) == other[k] }
end
Also aliased as: ==
keys() click to toggle source
# File lib/watir_model.rb, line 140
def keys
  self.class.keys
end
to_api() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_json(*) click to toggle source
# File lib/watir_model.rb, line 178
def to_json(*)
  to_h.to_json
end
update(hash) click to toggle source
# 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_keys() click to toggle source
# File lib/watir_model.rb, line 148
def valid_keys
  self.class.valid_keys
end

Private Instance Methods

default_value(key, block) click to toggle source
# File lib/watir_model.rb, line 192
def default_value(key, block)
  instance_exec(&block)
end