Class: WatirModel
Direct Known Subclasses
Class Attribute Summary collapse
Class Method Summary collapse
- .convert(hash, *args) ⇒ Object
- .convert_type(key, value) ⇒ Object
- .data_from_yaml(file, value) ⇒ Object
- .default_directory ⇒ Object
- .default_value_keys(hash) ⇒ Object
- .factory_file(type) ⇒ Object
- .inherited(subclass) ⇒ Object
- .key(symbol, data_type: nil, api: nil, &block) ⇒ Object
- .method_missing(method, *args, &block) ⇒ Object
- .valid_keys ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #apis ⇒ Object
- #eql?(other) ⇒ Boolean (also: #==)
-
#initialize(hash = {}) ⇒ WatirModel
constructor
A new instance of WatirModel.
- #keys ⇒ Object
- #to_api ⇒ Object
- #to_h(opt = nil) ⇒ Object
- #to_hash(opt = nil) ⇒ Object
- #to_json ⇒ Object
- #update(hash) ⇒ Object
- #valid_keys ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ WatirModel
Returns a new instance of WatirModel.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/watir_model.rb', line 127 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
.apis ⇒ Object
24 25 26 |
# File 'lib/watir_model.rb', line 24 def apis @apis ||= {} end |
.data_types ⇒ Object
20 21 22 |
# File 'lib/watir_model.rb', line 20 def data_types @data_types ||= {} end |
.defaults ⇒ Object
28 29 30 |
# File 'lib/watir_model.rb', line 28 def defaults @defaults ||= {} end |
.keys ⇒ Object
16 17 18 |
# File 'lib/watir_model.rb', line 16 def keys @keys ||= [] end |
Class Method Details
.convert(hash, *args) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/watir_model.rb', line 84 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
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/watir_model.rb', line 51 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) value = case when data_type == String value.to_s when [Time, Date, DateTime].include?(data_type) data_type.parse value when data_type == Integer value.to_i when data_type == Float value.to_f when data_type == :boolean return value if value.is_a?(TrueClass) || value.is_a?(FalseClass) value = eval(value) return value if value.is_a?(TrueClass) || value.is_a?(FalseClass) raise StandardError, "Unable to convert #{value} to TrueClass or FalseClass" when data_type == Symbol value.to_sym when data_type == Hash JSON.parse value when data_type == Array JSON.parse value else file = factory_file(data_type) data = data_from_yaml(file, value) || value data_type.new(data) end return value if value.is_a?(data_type) raise StandardError, "Unable to convert #{value} to #{data_type}" end |
.data_from_yaml(file, value) ⇒ Object
121 122 123 124 |
# File 'lib/watir_model.rb', line 121 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_directory ⇒ Object
99 100 101 |
# File 'lib/watir_model.rb', line 99 def default_directory 'config/data' end |
.default_value_keys(hash) ⇒ Object
103 104 105 106 107 |
# File 'lib/watir_model.rb', line 103 def default_value_keys(hash) hash.keys.map do |key| keys.include?(key) ? key : apis[key] end end |
.factory_file(type) ⇒ Object
117 118 119 |
# File 'lib/watir_model.rb', line 117 def factory_file(type) Dir.glob("#{WatirModel.yml_directory}/#{type.to_s[/[^:]*$/].underscore}.yml").first end |
.inherited(subclass) ⇒ Object
36 37 38 39 40 41 |
# File 'lib/watir_model.rb', line 36 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
43 44 45 46 47 48 49 |
# File 'lib/watir_model.rb', line 43 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
109 110 111 112 113 114 115 |
# File 'lib/watir_model.rb', line 109 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_keys ⇒ Object
32 33 34 |
# File 'lib/watir_model.rb', line 32 def valid_keys keys + apis.keys end |
Instance Method Details
#[](key) ⇒ Object
167 168 169 |
# File 'lib/watir_model.rb', line 167 def [] key send key end |
#apis ⇒ Object
159 160 161 |
# File 'lib/watir_model.rb', line 159 def apis self.class.apis end |
#eql?(other) ⇒ Boolean Also known as: ==
171 172 173 |
# File 'lib/watir_model.rb', line 171 def eql?(other) keys.all? { |k| send(k) == other[k] } end |
#keys ⇒ Object
155 156 157 |
# File 'lib/watir_model.rb', line 155 def keys self.class.keys end |
#to_api ⇒ Object
195 196 197 198 199 200 201 |
# File 'lib/watir_model.rb', line 195 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
181 182 183 184 185 186 187 188 189 |
# File 'lib/watir_model.rb', line 181 def to_h(opt = nil) opt ||= keys opt.each_with_object({}) do |key, hash| value = send(key) next if value.nil? value = value.to_h if value.is_a? WatirModel hash[key] = value end end |
#to_hash(opt = nil) ⇒ Object
176 177 178 179 |
# File 'lib/watir_model.rb', line 176 def to_hash(opt = nil) warn "#to_hash is deprecated, use #to_h instead" to_h opt end |
#to_json ⇒ Object
191 192 193 |
# File 'lib/watir_model.rb', line 191 def to_json(*) to_h.to_json end |
#update(hash) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/watir_model.rb', line 139 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 ⇒ Object
163 164 165 |
# File 'lib/watir_model.rb', line 163 def valid_keys self.class.valid_keys end |