Module: LookupTable

Defined in:
lib/lookup_table.rb,
lib/lookup_table/class_methods.rb

Overview

lookup table to optimize complex card systems

TODO: make this a class and have lookup classes inherit from it

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



49
50
51
52
53
54
55
# File 'lib/lookup_table.rb', line 49

def method_missing method_name, *args, &block
  if card.respond_to? method_name
    card.send method_name, *args, &block
  else
    super
  end
end

Class Method Details

.included(host_class) ⇒ Object



5
6
7
8
# File 'lib/lookup_table.rb', line 5

def self.included host_class
  host_class.extend LookupTable::ClassMethods
  host_class.define_main_fetcher
end

Instance Method Details

#cardObject



14
15
16
# File 'lib/lookup_table.rb', line 14

def card
  @card ||= Card.fetch send(card_column), look_in_trash: true
end

#card_columnObject



10
11
12
# File 'lib/lookup_table.rb', line 10

def card_column
  self.class.card_column
end

#card_idObject



18
19
20
# File 'lib/lookup_table.rb', line 18

def card_id
  send card_column
end

#card_id=(id) ⇒ Object



22
23
24
# File 'lib/lookup_table.rb', line 22

def card_id= id
  send "#{card_column}=", id
end

#delete_on_refresh?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/lookup_table.rb', line 26

def delete_on_refresh?
  !card || card.trash
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
# File 'lib/lookup_table.rb', line 61

def is_a? klass
  klass == Card || super
end

#refresh(*fields) ⇒ Object

Raises:

  • (Card::Error)


30
31
32
33
34
35
36
37
# File 'lib/lookup_table.rb', line 30

def refresh *fields
  return delete if delete_on_refresh?

  refresh_fields fields
  raise Card::Error, "invalid #{self.class} lookup" if invalid?

  save!
end

#refresh_fields(fields = nil) ⇒ Object



39
40
41
42
43
# File 'lib/lookup_table.rb', line 39

def refresh_fields fields=nil
  keys = fields.present? ? fields : attribute_names
  keys.delete("id")
  keys.each { |method_name| refresh_value method_name }
end

#refresh_value(method_name) ⇒ Object



45
46
47
# File 'lib/lookup_table.rb', line 45

def refresh_value method_name
  send "#{method_name}=", send("fetch_#{method_name}")
end

#respond_to_missing?(*args) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/lookup_table.rb', line 57

def respond_to_missing? *args
  card.respond_to?(*args) || super
end