Class: CustomFragmentCache::Fragment

Inherits:
Object
  • Object
show all
Defined in:
lib/custom_fragment_cache/fragment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, opts) ⇒ Fragment

Returns a new instance of Fragment.



5
6
7
8
9
10
11
# File 'lib/custom_fragment_cache/fragment.rb', line 5

def initialize(name, opts)
  @name = name
  @opts = opts.with_indifferent_access
  @fields = {}
  @fields_method = :none
  set_fields
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



3
4
5
# File 'lib/custom_fragment_cache/fragment.rb', line 3

def fields
  @fields
end

#fields_methodObject (readonly)

Returns the value of attribute fields_method.



3
4
5
# File 'lib/custom_fragment_cache/fragment.rb', line 3

def fields_method
  @fields_method
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/custom_fragment_cache/fragment.rb', line 3

def name
  @name
end

#optsObject (readonly)

Returns the value of attribute opts.



3
4
5
# File 'lib/custom_fragment_cache/fragment.rb', line 3

def opts
  @opts
end

Instance Method Details

#set_fieldsObject



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/custom_fragment_cache/fragment.rb', line 13

def set_fields
  validate_fields_method

  if @opts[:safe_fields].present?
    @fields = @opts[:safe_fields].to_a
    @fields_method = :safe
  elsif @opts[:trigger_fields].present?
    @fields = @opts[:trigger_fields].to_a
    @fields_method = :trigger
  end
end

#validate_fields_methodObject



25
26
27
28
29
# File 'lib/custom_fragment_cache/fragment.rb', line 25

def validate_fields_method
  if @opts.key?(:safe_fields) && @opts.key?(:trigger_fields)
    raise ArgumentError, "Set trigger and safe fields are not allowed together"
  end
end