Module: LookupBy::Lookup::ClassMethods

Defined in:
lib/lookup_by/lookup.rb

Instance Method Summary collapse

Instance Method Details

#[](*args) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/lookup_by/lookup.rb', line 83

def [](*args)
  case args.size
  when 0 then raise ArgumentError, "#{name}[*args]: at least one argument is required"
  when 1
    case arg = args.first
    when nil     then nil
    when ""      then @lookup.allow_blank? ? @lookup.fetch(arg) : nil
    when String  then @lookup.fetch(arg)
    when Integer then @lookup.fetch(arg)
    when Symbol  then @lookup.fetch(arg.to_s)
    when IPAddr  then @lookup.fetch(arg.to_s)
    when self    then arg
    else raise TypeError, "#{name}[arg]: arg must be at least one String, Symbol, Integer, nil, or #{name}"
    end
  else return args.map { |arg| self[arg] } 
  end
end

#all(*args) ⇒ Object

TODO: Rails 4 needs to return a proxy object here



62
63
64
65
66
67
68
# File 'lib/lookup_by/lookup.rb', line 62

def all(*args)
  return super if Rails::VERSION::MAJOR > 3
  return super if @lookup.read_through?
  return super if args.any?

  @lookup.cache.values
end

#count(column_name = nil, options = {}) ⇒ Object



70
71
72
73
74
75
# File 'lib/lookup_by/lookup.rb', line 70

def count(column_name = nil, options = {})
  return super if @lookup.read_through?
  return super if column_name

  @lookup.cache.size
end

#pluck(column_name) ⇒ Object



77
78
79
80
81
# File 'lib/lookup_by/lookup.rb', line 77

def pluck(column_name)
  return super if @lookup.read_through?

  @lookup.cache.values.map { |o| o.send(column_name) }
end