Class: SmartCollection::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_collection/config.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_config) ⇒ Config

Returns a new instance of Config.



6
7
8
9
# File 'lib/smart_collection/config.rb', line 6

def initialize raw_config
  @raw_config = raw_config
  check_config
end

Instance Attribute Details

#cache_managerObject

Returns the value of attribute cache_manager.



3
4
5
# File 'lib/smart_collection/config.rb', line 3

def cache_manager
  @cache_manager
end

#raw_configObject (readonly)

Returns the value of attribute raw_config.



4
5
6
# File 'lib/smart_collection/config.rb', line 4

def raw_config
  @raw_config
end

Instance Method Details

#cache_configObject



27
28
29
# File 'lib/smart_collection/config.rb', line 27

def cache_config
  @raw_config[:cached_by]
end

#cache_expires_in_procObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/smart_collection/config.rb', line 54

def cache_expires_in_proc
  case @raw_config[:cache_expires_in]
  when Proc
    @raw_config[:cache_expires_in]
  when ActiveSupport::Duration
    -> (owner) { @raw_config[:cache_expires_in] }
  when Symbol
    -> (owner) { owner.send(@raw_config[:cache_expires_in]) }
  else
    raise "cache_expires_in option only accepts a Proc / ActiveSupport::Duration / Symbol"
  end
end

#cache_table_nameObject



46
47
48
49
50
51
52
# File 'lib/smart_collection/config.rb', line 46

def cache_table_name
  if @raw_config[:cache_table] == :default
    :smart_collection_cached_items
  else
    @raw_config[:cache_table]
  end
end

#check_configObject



67
68
69
70
71
72
73
# File 'lib/smart_collection/config.rb', line 67

def check_config
  raise "items option must be provided" if items_name.nil?
  raise "item_class option must be provided" if item_class_name.nil?
  raise "scopes option must be provided" if @raw_config[:scopes].nil?
  raise "cache_table option must be provided" if @raw_config[:cache_table].nil?
  raise "cache_expires_in option must be provided" if @raw_config[:cache_expires_in].nil?
end

#inverse_associationObject



42
43
44
# File 'lib/smart_collection/config.rb', line 42

def inverse_association
  @raw_config[:inverse_association]
end

#item_classObject



23
24
25
# File 'lib/smart_collection/config.rb', line 23

def item_class
  @item_class ||= item_class_name.constantize
end

#item_class_nameObject



19
20
21
# File 'lib/smart_collection/config.rb', line 19

def item_class_name
  @raw_config[:item_class_name]
end

#item_nameObject



15
16
17
# File 'lib/smart_collection/config.rb', line 15

def item_name
  items_name.to_s.singularize.to_sym
end

#items_nameObject



11
12
13
# File 'lib/smart_collection/config.rb', line 11

def items_name
  @raw_config[:item_association]
end

#scopes_procObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/smart_collection/config.rb', line 31

def scopes_proc
  case @raw_config[:scopes]
  when Proc
    @raw_config[:scopes]
  when Symbol
    -> (owner) { owner.send(@raw_config[:scopes]) }
  else
    raise "scopes option only accepts a Proc / Symbol"
  end
end