Class: Halogen::Definition
- Inherits:
-
Object
- Object
- Halogen::Definition
- Defined in:
- lib/halogen/definition.rb
Overview
Stores instructions for how to render a value for a given representer instance
Direct Known Subclasses
Embeds::Definition, Links::Definition, Properties::Definition
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#procedure ⇒ Object
Returns the value of attribute procedure.
Instance Method Summary collapse
-
#enabled?(instance) ⇒ true, false
Whether this definition should be included based on its conditional guard, if any.
-
#initialize(name, options, procedure) ⇒ Halogen::Definition
constructor
Construct a new Definition instance.
-
#validate ⇒ true
If nothing is raised.
- #value(instance) ⇒ Object
Constructor Details
#initialize(name, options, procedure) ⇒ Halogen::Definition
Construct a new Definition instance
17 18 19 20 21 |
# File 'lib/halogen/definition.rb', line 17 def initialize(name, , procedure) @name = name.to_sym @options = Halogen::HashUtil.symbolize_keys!() @procedure = procedure end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/halogen/definition.rb', line 6 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
6 7 8 |
# File 'lib/halogen/definition.rb', line 6 def @options end |
#procedure ⇒ Object
Returns the value of attribute procedure.
8 9 10 |
# File 'lib/halogen/definition.rb', line 8 def procedure @procedure end |
Instance Method Details
#enabled?(instance) ⇒ true, false
Returns whether this definition should be included based on its conditional guard, if any.
35 36 37 38 39 40 41 42 43 |
# File 'lib/halogen/definition.rb', line 35 def enabled?(instance) if .key?(:if) !!eval_guard(instance, .fetch(:if)) elsif .key?(:unless) !eval_guard(instance, .fetch(:unless)) else true end end |
#validate ⇒ true
Returns if nothing is raised.
49 50 51 52 53 54 |
# File 'lib/halogen/definition.rb', line 49 def validate return true unless .key?(:value) && procedure fail InvalidDefinition, "Cannot specify both value and procedure for #{name}" end |
#value(instance) ⇒ Object
26 27 28 29 30 |
# File 'lib/halogen/definition.rb', line 26 def value(instance) .fetch(:value) do procedure ? instance.instance_eval(&procedure) : instance.send(name) end end |