Class: PoeWatch::Base
- Inherits:
-
Object
- Object
- PoeWatch::Base
- Defined in:
- lib/base.rb
Constant Summary collapse
- INFLECTOR =
{ category: 'categories', item: 'items', league: 'leagues' }
Class Method Summary collapse
-
.__data ⇒ Object
Defines an instance variable that’ll be memoized and used to store Poe Watch data.
-
.__data=(value) ⇒ Object
Setter for instance variable.
-
.all ⇒ Object
Public: Gets all data as instance of the class.
-
.count ⇒ Object
Public: Count the number of items.
-
.find(params) ⇒ Object
Public: Gets a specific item You can pass regular expressions for string parameters.
-
.type ⇒ Object
Public: Gets the object type.
-
.where(params) ⇒ Object
Public: Gets items filtered by a query You can pass regular expressions for string parameters.
Instance Method Summary collapse
-
#initialize(raw_data) ⇒ Base
constructor
Public: Initialize a Poe Watch item.
Constructor Details
#initialize(raw_data) ⇒ Base
Public: Initialize a Poe Watch item. It will automatically create instance variables, setters, getters for instance variables, and question mark methods for variables with boolean values. It is to note that any hash or array of hashes will be transformed into an OpenStruct.
raw_data - An array of hash
Returns an instance of the class.
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/base.rb', line 148 def initialize(raw_data) raw_data.each do |k, v| key = snakify(k) if v.is_a?(Hash) value = OpenStruct.new(v) elsif v.is_a?(Array) && v.first.is_a?(Hash) value = v.map { |arr_val| OpenStruct.new(arr_val) } else value = v end instance_variable_set("@#{key}", value) define_singleton_method key do instance_variable_get("@#{key}") end # If it is a boolean we also create a `key?` method` if [TrueClass, FalseClass].include?(value.class) define_singleton_method "#{key}?" do instance_variable_get("@#{key}") end end end end |
Class Method Details
.__data ⇒ Object
Defines an instance variable that’ll be memoized and used to store Poe Watch data
23 24 25 26 27 28 |
# File 'lib/base.rb', line 23 def __data if not defined? @__data @__data = [] end @__data end |
.__data=(value) ⇒ Object
Setter for instance variable
31 32 33 34 |
# File 'lib/base.rb', line 31 def __data=(value) puts "SETTING @__data" @__data = value end |
.all ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/base.rb', line 54 def all PoeWatch::Api.refresh! return __data if __data.any? puts "PAST DATA" # If data isn't already loaded # puts "Loading #{type} data..." json_data = PoeWatch::Api.send(INFLECTOR[type]) if json_data.any? self.__data = json_data.map { |raw_data| self.new(raw_data) } __data else [] end end |
.count ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/base.rb', line 77 def count PoeWatch::Api.refresh! return __data if __data.any? # If data isn't already loaded # puts "Loading #{type} data..." json_data = PoeWatch::Api.send(INFLECTOR[type]) if json_data.any? __data = json_data.length else 0 end end |
.find(params) ⇒ Object
Public: Gets a specific item You can pass regular expressions for string parameters. It will do a match.
params - A hash of parameters. Eg: { name: /Metamorph/, hardcore: true }
Examples
PoeWatch::League.find({ name: "Metamorph", hardcore: true }) # =>PoeWatch::League item
Returns an instance of the class.
126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/base.rb', line 126 def find(params) PoeWatch::Api.refresh! self.all.find do |element| params.map do |k, v| if v.is_a? Regexp !!element.send(k).match(v) else element.send(k) == v end end.all?(TrueClass) end end |
.type ⇒ Object
43 44 45 |
# File 'lib/base.rb', line 43 def type self.name.split('::').last.downcase.to_sym end |
.where(params) ⇒ Object
Public: Gets items filtered by a query You can pass regular expressions for string parameters. It will do a match.
params - A hash of parameters. Eg: { hardcore: true }
Examples
PoeWatch::League.where({ hardcore: true }) # => array of PoeWatch::League items
PoeWatch::League.where({ name: /metamorph/i, hardcore: true }) # => array of PoeWatch::League items
Returns an Array of class instances.
103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/base.rb', line 103 def where(params) PoeWatch::Api.refresh! self.all.select do |element| params.map do |k, v| if v.is_a? Regexp !!element.send(k).match(v) else element.send(k) == v end end.all?(TrueClass) end end |