Module: LookupTable

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

Constant Summary collapse

VERSION =
'0.0.1'

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.prefetchObject Also known as: prefetch?

Returns the value of attribute prefetch.



16
17
18
# File 'lib/lookup_table.rb', line 16

def prefetch
  @prefetch
end

.prefetch_limitObject

Returns the value of attribute prefetch_limit.



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

def prefetch_limit
  @prefetch_limit
end

Instance Attribute Details

#key_columnsObject (readonly)

Returns the value of attribute key_columns.



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

def key_columns
  @key_columns
end

#prefetch=(value) ⇒ Object (writeonly)

Sets the attribute prefetch

Parameters:

  • value

    the value to set the attribute prefetch to.



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

def prefetch=(value)
  @prefetch = value
end

#value_columnObject (readonly)

Returns the value of attribute value_column.



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

def value_column
  @value_column
end

Class Method Details

.create(*args, &block) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/lookup_table.rb', line 7

def self.create *args, &block
  Class.new(ActiveRecord::Base) do
    extend LookupTable
    act_as_lookup_table *args
    class_eval &block if block_given?
  end
end

Instance Method Details

#[](*keys) ⇒ Object



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

def [] *keys
  lookup_table[ keys.flatten ]
end

#act_as_lookup_table(table, key, value, options = {}) ⇒ Object



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

def act_as_lookup_table table, key, value, options = {}
  self.table_name = table
  @key_columns = [key].flatten
  @value_column = value

  self.default = options[:default]
  self.prefetch = options.fetch(:prefetch, true)
end

#create_lookup_tableObject



99
100
101
102
103
# File 'lib/lookup_table.rb', line 99

def create_lookup_table
  Hash.new do |lookup_table, keys|
    lookup_table[keys] = db_lookup(keys) || default(keys)
  end
end

#db_lookup(keys) ⇒ Object



48
49
50
51
# File 'lib/lookup_table.rb', line 48

def db_lookup keys
  record = lookup_domain.where( quoted_where keys ).first
  record_value record
end

#default(keys) ⇒ Object



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

def default keys
  @default.call *keys
end

#default=(default) ⇒ Object



65
66
67
# File 'lib/lookup_table.rb', line 65

def default= default
  @default = (Proc === default) ? default : proc { default }
end

#lookup_columnsObject



69
70
71
# File 'lib/lookup_table.rb', line 69

def lookup_columns
  key_columns + [value_column]
end

#lookup_domainObject



77
78
79
# File 'lib/lookup_table.rb', line 77

def lookup_domain
  select quoted_lookup_columns
end

#lookup_tableObject



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

def lookup_table
  @lookup_table ||= prefetch_if_requested( create_lookup_table )
end

#prefetch?Boolean

Returns:

  • (Boolean)


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

def prefetch?
  (!LookupTable.prefetch_limit or
   (lookup_domain.count <= LookupTable.prefetch_limit)) and
   LookupTable.prefetch? and
   @prefetch
end

#prefetch_if_requested(hash) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/lookup_table.rb', line 89

def prefetch_if_requested hash
  if prefetch?
    lookup_domain.inject(hash) do |hash,record|
      hash[ record_key(record) ] = record_value(record)
      hash
    end
  end
  hash
end

#quoted_key_columnsObject



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

def quoted_key_columns
  key_columns.map{|col| connection.quote_column_name col}
end

#quoted_lookup_columnsObject



73
74
75
# File 'lib/lookup_table.rb', line 73

def quoted_lookup_columns
  lookup_columns.map{|col| connection.quote_column_name col}
end

#quoted_where(key_values) ⇒ Object



53
54
55
# File 'lib/lookup_table.rb', line 53

def quoted_where key_values
  Hash[ key_columns.zip(key_values) ]
end

#record_key(record) ⇒ Object



81
82
83
# File 'lib/lookup_table.rb', line 81

def record_key record
  key_columns.map{|column| record[column]}
end

#record_value(record) ⇒ Object



85
86
87
# File 'lib/lookup_table.rb', line 85

def record_value record
  record.try :[], value_column
end