Class: Brainstem::DSL::Field

Inherits:
Object
  • Object
show all
Includes:
Concerns::Lookup
Defined in:
lib/brainstem/dsl/field.rb

Direct Known Subclasses

BlockField

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::Lookup

#run_on_with_lookup

Constructor Details

#initialize(name, type, options) ⇒ Field

Returns a new instance of Field.



10
11
12
13
14
15
# File 'lib/brainstem/dsl/field.rb', line 10

def initialize(name, type, options)
  @name = name.to_s
  @type = type.to_s
  @conditionals = [options[:if]].flatten.compact
  @options = options
end

Instance Attribute Details

#conditionalsObject (readonly)

Returns the value of attribute conditionals.



8
9
10
# File 'lib/brainstem/dsl/field.rb', line 8

def conditionals
  @conditionals
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/brainstem/dsl/field.rb', line 8

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



8
9
10
# File 'lib/brainstem/dsl/field.rb', line 8

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/brainstem/dsl/field.rb', line 8

def type
  @type
end

Instance Method Details

#conditional?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/brainstem/dsl/field.rb', line 21

def conditional?
  conditionals.length > 0
end

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

Returns:

  • (Boolean)


70
71
72
73
74
75
76
# File 'lib/brainstem/dsl/field.rb', line 70

def conditionals_match?(model, presenter_conditionals, helper_instance = Object.new, conditional_cache = { model: {}, request: {} })
  return true unless conditional?

  conditionals.all? { |conditional|
    presenter_conditionals[conditional].matches?(model, helper_instance, conditional_cache)
  }
end

#descriptionObject



17
18
19
# File 'lib/brainstem/dsl/field.rb', line 17

def description
  options[:info].presence
end

#evaluate_value_on(model, context, helper_instance = Object.new) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/brainstem/dsl/field.rb', line 46

def evaluate_value_on(model, context, helper_instance = Object.new)
  if options[:lookup]
    run_on_with_lookup(model, context, helper_instance)
  elsif options[:dynamic]
    proc = options[:dynamic]
    if proc.arity == 1
      helper_instance.instance_exec(model, &proc)
    else
      helper_instance.instance_exec(&proc)
    end
  else
    model.send(method_name)
  end
end

#method_nameObject



25
26
27
28
29
30
31
# File 'lib/brainstem/dsl/field.rb', line 25

def method_name
  if options[:dynamic] || options[:lookup]
    nil
  else
    (options[:via].presence || name).to_s
  end
end

#optional?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/brainstem/dsl/field.rb', line 37

def optional?
  options[:optional]
end

#optioned?(requested_optional_fields) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/brainstem/dsl/field.rb', line 33

def optioned?(requested_optional_fields)
  !optional? || requested_optional_fields.include?(@name)
end

#presentable?(model, context) ⇒ Boolean

Returns:

  • (Boolean)


61
62
63
64
65
66
67
68
# File 'lib/brainstem/dsl/field.rb', line 61

def presentable?(model, context)
  optioned?(context[:optional_fields]) && conditionals_match?(
    model,
    context[:conditionals],
    context[:helper_instance],
    context[:conditional_cache]
  )
end

#run_on(model, context, helper_instance = Object.new) ⇒ Object

Please override in sub classes to compute value of field with the given arguments.



42
43
44
# File 'lib/brainstem/dsl/field.rb', line 42

def run_on(model, context, helper_instance = Object.new)
  evaluate_value_on(model, context, helper_instance)
end