Class: Brainstem::DSL::Field
- Inherits:
-
Object
- Object
- Brainstem::DSL::Field
- Includes:
- Concerns::Lookup
- Defined in:
- lib/brainstem/dsl/field.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#conditionals ⇒ Object
readonly
Returns the value of attribute conditionals.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
- #conditional? ⇒ Boolean
- #conditionals_match?(model, presenter_conditionals, helper_instance = Object.new, conditional_cache = { model: {}, request: {} }) ⇒ Boolean
- #description ⇒ Object
- #evaluate_value_on(model, context, helper_instance = Object.new) ⇒ Object
-
#initialize(name, type, options) ⇒ Field
constructor
A new instance of Field.
- #method_name ⇒ Object
- #optional? ⇒ Boolean
- #optioned?(requested_optional_fields) ⇒ Boolean
- #presentable?(model, context) ⇒ Boolean
-
#run_on(model, context, helper_instance = Object.new) ⇒ Object
Please override in sub classes to compute value of field with the given arguments.
Methods included from Concerns::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, ) @name = name.to_s @type = type.to_s @conditionals = [[:if]].flatten.compact @options = end |
Instance Attribute Details
#conditionals ⇒ Object (readonly)
Returns the value of attribute conditionals.
8 9 10 |
# File 'lib/brainstem/dsl/field.rb', line 8 def conditionals @conditionals end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
8 9 10 |
# File 'lib/brainstem/dsl/field.rb', line 8 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/brainstem/dsl/field.rb', line 8 def @options end |
#type ⇒ Object (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
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
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 |
#description ⇒ Object
17 18 19 |
# File 'lib/brainstem/dsl/field.rb', line 17 def description [: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 [:lookup] run_on_with_lookup(model, context, helper_instance) elsif [:dynamic] proc = [: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_name ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/brainstem/dsl/field.rb', line 25 def method_name if [:dynamic] || [:lookup] nil else ([:via].presence || name).to_s end end |
#optional? ⇒ Boolean
37 38 39 |
# File 'lib/brainstem/dsl/field.rb', line 37 def optional? [:optional] end |
#optioned?(requested_optional_fields) ⇒ 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
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 |