watir_model 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9085dc14b69f2a8e1ed12aecd5241f513e990524
4
- data.tar.gz: 2be96f1b547f5f1b7b572f87bd02f808c704fad4
3
+ metadata.gz: fb66fe24b50991cf49d310d6a97f089e30913591
4
+ data.tar.gz: 7e3688b4c12b17d386371604ab359e63cdb66434
5
5
  SHA512:
6
- metadata.gz: a15324a7547c57422ffeec87c1c3a957ba6acfb9341b198145f59aa6857e488d42b4d104c3d38cbad75c46fb6faef945a8d15c0a5028e579e16cd25f0a516520
7
- data.tar.gz: ed15f70dc9e37ee0fac354d173b11b65caa73b4920105a3ed8b1685b3a9922b1fbf391f545bac116f13341602b79c0261f5f589b202d7f823eb7d8039bb86cc0
6
+ metadata.gz: e1c922f20e1d39fb88d9f8fc92cd08736937ebc63375a2f7c051cbf8751102dc5b17a19fb392cd8bba4076fdb5532c41e3805d416941690fffc5db0301cd1e5d
7
+ data.tar.gz: 2101fd335a12e59c92a8fb83d8d39ac9e22313ad1afd96453363a024e46926f814ccbb86567ecdefb3e5fb5a13ed0628d03136dc39eae2fcd8056b203249ea43
data/CHANGES.md CHANGED
@@ -1,3 +1,10 @@
1
+ ### 0.4.0 (2017-12-24)
2
+
3
+ * support for `:data_type` parameter to allow conversion and enforcement of key types
4
+ * support for data defaults pulled from yaml files
5
+ * support for referencing yaml defaults during initialization
6
+ * support for key aliases to support comparing same values from different sources
7
+
1
8
  ### 0.3.1 (2017-05-19)
2
9
 
3
10
  * initialize and convert Model using Hash with keys as Strings
@@ -0,0 +1,4 @@
1
+ :alaska:
2
+ :state: AK
3
+ :city: Anchorage
4
+ :zip: '99501'
@@ -0,0 +1,4 @@
1
+ :alaska:
2
+ :state: AK
3
+ :city: Anchorage
4
+ :zip: '99501'
@@ -1,30 +1,81 @@
1
1
  require 'active_support/hash_with_indifferent_access'
2
+ require 'json'
3
+ require 'date'
4
+ require 'time'
5
+ require 'yml_reader'
2
6
 
3
7
  class WatirModel
4
8
  class << self
9
+ include YmlReader
5
10
 
6
- attr_writer :keys, :defaults
11
+ attr_writer :keys, :aliases, :data_types, :defaults
7
12
 
8
13
  def keys
9
14
  @keys ||= []
10
15
  end
11
16
 
17
+ def aliases
18
+ @aliases ||= {}
19
+ end
20
+
21
+ def data_types
22
+ @data_types ||= {}
23
+ end
24
+
12
25
  def defaults
13
26
  @defaults ||= {}
14
27
  end
15
28
 
16
29
  def inherited(subclass)
17
30
  subclass.keys = keys.dup
31
+ subclass.aliases = aliases.dup
18
32
  subclass.defaults = defaults.dup
33
+ subclass.data_types = data_types.dup
19
34
  end
20
35
 
21
- # define a key and an optional block that provides a default value for the key
22
- def key(symbol, &block)
36
+ def key(symbol, data_type: nil, aliases: [], &block)
23
37
  keys << symbol unless @keys.include? symbol
38
+ aliases.each { |alias_key| self.aliases[alias_key] = symbol }
24
39
  attr_accessor symbol
40
+ data_types[symbol] = data_type if data_type
25
41
  defaults[symbol] = block if block
26
42
  end
27
43
 
44
+ def convert_type(key, value)
45
+ data_type = data_types[key]
46
+ return value if data_type.nil?
47
+ return value if data_type.is_a?(Class) && value.is_a?(data_type)
48
+ value = case
49
+ when data_type == String
50
+ value.to_s
51
+ when data_type == Time
52
+ Time.parse value
53
+ when data_type == DateTime
54
+ DateTime.parse value
55
+ when data_type == Integer
56
+ value.to_i
57
+ when data_type == Float
58
+ value.to_f
59
+ when data_type == :boolean
60
+ return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
61
+ value = eval(value)
62
+ return value if value.is_a?(TrueClass) || value.is_a?(FalseClass)
63
+ raise StandardError, "Unable to convert #{value} to TrueClass or FalseClass"
64
+ when data_type == Symbol
65
+ value.to_sym
66
+ when data_type == Hash
67
+ JSON.parse value
68
+ when data_type == Array
69
+ JSON.parse value
70
+ else
71
+ file = factory_file(data_type)
72
+ data = data_from_yaml(file, value) || value
73
+ data_type.new(data)
74
+ end
75
+ return value if value.is_a?(data_type)
76
+ raise StandardError, "Unable to convert #{value} to #{data_type}"
77
+ end
78
+
28
79
  def convert(hash, *args)
29
80
  hash.deep_symbolize_keys!
30
81
  filtered = hash.reject { |k| !keys.include?(k) }
@@ -36,6 +87,27 @@ class WatirModel
36
87
  end
37
88
  model
38
89
  end
90
+
91
+ def default_directory
92
+ 'config/data'
93
+ end
94
+
95
+ def method_missing(method, *args, &block)
96
+ file = factory_file(self)
97
+ return super unless file
98
+ data = data_from_yaml(file, method)
99
+ raise ArgumentError, "Factory '#{method}' does not exist in '#{file}'" if data.nil?
100
+ new(data)
101
+ end
102
+
103
+ def factory_file(type)
104
+ Dir.glob("#{WatirModel.yml_directory}/#{type.to_s.downcase}.yml").first
105
+ end
106
+
107
+ def data_from_yaml(file, value)
108
+ return nil unless !file.nil? || value.is_a?(Symbol) || value.is_a?(String)
109
+ YAML.load_file(file)[value.to_sym]
110
+ end
39
111
  end
40
112
 
41
113
  def initialize(hash={})
@@ -45,6 +117,7 @@ class WatirModel
45
117
  (self.class.defaults.keys - hash.keys).each do |key|
46
118
  block = self.class.defaults[key]
47
119
  value = default_value(key, block)
120
+ value = self.class.convert_type(key, value)
48
121
  instance_variable_set("@#{key}", value)
49
122
  end
50
123
  end
@@ -52,17 +125,27 @@ class WatirModel
52
125
  def update(hash)
53
126
  hash ||= {}
54
127
 
128
+ (hash.keys & aliases.keys).each do |alias_key|
129
+ hash[aliases[alias_key]] = hash.delete(alias_key)
130
+ end
131
+
55
132
  unknown = hash.keys - keys
56
133
  if unknown.count > 0
57
134
  raise ArgumentError, "unknown keyword#{'s' if unknown.count > 1}: #{unknown.join ', '}"
58
135
  end
59
- hash.each { |key, v| instance_variable_set "@#{key}", v }
136
+ hash.each do |key, val|
137
+ instance_variable_set "@#{key}", self.class.convert_type(key, val)
138
+ end
60
139
  end
61
140
 
62
141
  def keys
63
142
  self.class.keys
64
143
  end
65
144
 
145
+ def aliases
146
+ self.class.aliases
147
+ end
148
+
66
149
  def [] key
67
150
  send key
68
151
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "watir_model"
5
- spec.version = "0.3.1"
5
+ spec.version = "0.4.0"
6
6
  spec.authors = ["Titus Fortner"]
7
7
  spec.email = ["titusfortner@gmail.com"]
8
8
 
@@ -25,6 +25,7 @@ Gem::Specification.new do |spec|
25
25
  spec.require_paths = ["lib"]
26
26
 
27
27
  spec.add_runtime_dependency 'activesupport'
28
+ spec.add_runtime_dependency 'yml_reader', '>= 0.6'
28
29
 
29
30
  spec.add_development_dependency "bundler", "~> 1.9"
30
31
  spec.add_development_dependency "rake", "~> 10.0"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: watir_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Titus Fortner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-20 00:00:00.000000000 Z
11
+ date: 2017-12-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: yml_reader
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0.6'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0.6'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,8 @@ files:
94
108
  - LICENSE
95
109
  - README.md
96
110
  - Rakefile
111
+ - config/alternate/address.yml
112
+ - config/data/address.yml
97
113
  - lib/config_model.rb
98
114
  - lib/watir_model.rb
99
115
  - watir_model.gemspec
@@ -118,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
134
  version: '0'
119
135
  requirements: []
120
136
  rubyforge_project:
121
- rubygems_version: 2.6.8
137
+ rubygems_version: 2.6.11
122
138
  signing_key:
123
139
  specification_version: 4
124
140
  summary: This is a simple class for modelling test data.