Class: Oakdex::Pokedex::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/oakdex/pokedex/base.rb

Overview

Base Class for Dex Entries

Direct Known Subclasses

Ability, EggGroup, Generation, Item, Move, Nature, Pokemon, Region, Type

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ Base

Returns a new instance of Base.



70
71
72
# File 'lib/oakdex/pokedex/base.rb', line 70

def initialize(attributes)
  @attributes = attributes
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



74
75
76
77
78
79
80
# 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

Instance Attribute Details

#attributes ⇒ Object (readonly)

Returns the value of attribute attributes.



68
69
70
# File 'lib/oakdex/pokedex/base.rb', line 68

def attributes
  @attributes
end

Class Method Details

.add_to_all(custom_entries) ⇒ Object



24
25
26
27
28
# 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 ⇒ Object



20
21
22
# File 'lib/oakdex/pokedex/base.rb', line 20

def all
  @all ||= map_json_data(json_folder, self)
end

.find(name) ⇒ Object



30
31
32
# File 'lib/oakdex/pokedex/base.rb', line 30

def find(name)
  all[name]
end

.find!(name) ⇒ Object



34
35
36
37
# 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) ⇒ Object



8
9
10
11
# File 'lib/oakdex/pokedex/base.rb', line 8

def json_folder(folder = nil)
  @folder = folder if folder
  @folder
end

.reset! ⇒ Object



39
40
41
# File 'lib/oakdex/pokedex/base.rb', line 39

def reset!
  @all = nil
end

.translate(attribute, name) ⇒ Object



13
14
15
16
17
18
# 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 = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/oakdex/pokedex/base.rb', line 82

def respond_to_missing?(method, *args)
  @attributes.key?(method.to_s) || super
end