Class: Brainstem::DSL::BlockField

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

Direct Known Subclasses

ArrayBlockField, HashBlockField

Instance Attribute Summary collapse

Attributes inherited from Field

#conditionals, #name, #options, #type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Field

#conditional?, #conditionals_match?, #description, #method_name, #optional?, #optioned?, #presentable?

Methods included from Concerns::Lookup

#key_for_lookup, #run_on_with_lookup

Constructor Details

#initialize(name, type, options, parent_field = nil) ⇒ BlockField

Returns a new instance of BlockField.



11
12
13
14
# File 'lib/brainstem/dsl/block_field.rb', line 11

def initialize(name, type, options, parent_field = nil)
  super(name, type, options)
  @parent_field = parent_field
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



7
8
9
# File 'lib/brainstem/dsl/block_field.rb', line 7

def configuration
  @configuration
end

Class Method Details

.for(name, type, options, parent_field = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
# File 'lib/brainstem/dsl/block_field.rb', line 34

def self.for(name, type, options, parent_field = nil)
  case type.to_s
    when 'array'
      DSL::ArrayBlockField.new(name, type, options, parent_field)
    when 'hash'
      DSL::HashBlockField.new(name, type, options, parent_field)
    else
      raise "Unknown Brainstem Block Field type encountered: #{type}"
  end
end

Instance Method Details

#[](key) ⇒ Object



26
27
28
# File 'lib/brainstem/dsl/block_field.rb', line 26

def [](key)
  configuration[key]
end

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



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

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
  elsif options[:via]
    model.send(options[:via])
  else
    raise "Block field #{name} can only be evaluated if :dynamic, :lookup, :via options are specified."
  end
end

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

Raises:

  • (NotImplementedError)


30
31
32
# File 'lib/brainstem/dsl/block_field.rb', line 30

def run_on(model, context, helper_instance = Object.new)
  raise NotImplementedError.new("Override this method in a sub class")
end

#use_parent_value?(field) ⇒ Boolean

Returns:

  • (Boolean)


62
63
64
65
66
# File 'lib/brainstem/dsl/block_field.rb', line 62

def use_parent_value?(field)
  return true unless field.options.has_key?(:use_parent_value)

  field.options[:use_parent_value]
end