class Oakdex::Pokedex::Base

Base Class for Dex Entries

Attributes

attributes[R]

Public Class Methods

add_to_all(custom_entries) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 24
def add_to_all(custom_entries)
  @all = @all.merge(custom_entries.map do |data|
    [data['names']['en'], new(data)]
  end.to_h)
end
all() click to toggle source
# File lib/oakdex/pokedex/base.rb, line 20
def all
  @all ||= map_json_data(json_folder, self)
end
find(name) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 30
def find(name)
  all[name]
end
find!(name) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 34
def find!(name)
  find(name) ||
    (raise NotFound, "#{name} (#{json_folder}) could not be found")
end
json_folder(folder = nil) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 8
def json_folder(folder = nil)
  @folder = folder if folder
  @folder
end
new(attributes) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 70
def initialize(attributes)
  @attributes = attributes
end
reset!() click to toggle source
# File lib/oakdex/pokedex/base.rb, line 39
def reset!
  @all = nil
end
translate(attribute, name) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 13
def translate(attribute, name)
  define_method(name) do |locale = 'en'|
    translations = public_send(attribute)
    translations[locale] || translations['en']
  end
end
where(conditions = {}) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 43
def where(conditions = {})
  all.values.select do |entry|
    conditions.all? do |name, value|
      if entry.public_send(name).is_a?(Array)
        entry.public_send(name).include?(value)
      else
        entry.public_send(name) == value
      end
    end
  end
end

Private Class Methods

json_data_path(type) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 63
def json_data_path(type)
  "#{Oakdex::Pokedex.data_dir}/#{type}.json"
end
map_json_data(type, klass) click to toggle source
# File lib/oakdex/pokedex/base.rb, line 57
def map_json_data(type, klass)
  Hash[JSON.parse(File.read(json_data_path(type))).map do |_, data|
    [data['names']['en'], klass.new(data)]
  end]
end

Public Instance Methods

method_missing(method, *args, &block) click to toggle source
Calls superclass method
# File lib/oakdex/pokedex/base.rb, line 74
def method_missing(method, *args, &block)
  if @attributes.key?(method.to_s)
    @attributes[method.to_s]
  else
    super
  end
end
respond_to_missing?(method, *args) click to toggle source
Calls superclass method
# File lib/oakdex/pokedex/base.rb, line 82
def respond_to_missing?(method, *args)
  @attributes.key?(method.to_s) || super
end