Class: Brainstem::DSL::Conditional

Inherits:
Object
  • Object
show all
Defined in:
lib/brainstem/dsl/conditional.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, action, options = {}) ⇒ Conditional

Returns a new instance of Conditional.



6
7
8
9
10
11
# File 'lib/brainstem/dsl/conditional.rb', line 6

def initialize(name, type, action, options = {})
  @name = name
  @type = type
  @action = action
  @options = options
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



4
5
6
# File 'lib/brainstem/dsl/conditional.rb', line 4

def action
  @action
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/brainstem/dsl/conditional.rb', line 4

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/brainstem/dsl/conditional.rb', line 4

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



4
5
6
# File 'lib/brainstem/dsl/conditional.rb', line 4

def type
  @type
end

Instance Method Details

#descriptionObject



13
14
15
# File 'lib/brainstem/dsl/conditional.rb', line 13

def description
  options[:info].presence
end

#matches?(model, helper_instance = Object.new, conditional_cache = { model: {}, request: {} }) ⇒ Boolean

Returns:

  • (Boolean)


17
18
19
20
21
22
23
24
25
26
# File 'lib/brainstem/dsl/conditional.rb', line 17

def matches?(model, helper_instance = Object.new, conditional_cache = { model: {}, request: {} })
  case type
    when :model
      conditional_cache[:model].fetch(name) { conditional_cache[:model][name] = helper_instance.instance_exec(model, &action) }
    when :request
      conditional_cache[:request].fetch(name) { conditional_cache[:request][name] = helper_instance.instance_exec(&action) }
    else
      raise "Unknown Brainstem Conditional type #{type}"
  end
end