Class: Wrapture::ActionSpec
- Inherits:
-
Object
- Object
- Wrapture::ActionSpec
- Defined in:
- lib/wrapture/action_spec.rb
Overview
An action to take within a generated program.
Class Method Summary collapse
-
.normalize_spec_hash(spec) ⇒ Object
Normalizes a hash specification of a rule.
Instance Method Summary collapse
-
#includes ⇒ Object
A list of includes needed for the action.
-
#initialize(spec) ⇒ ActionSpec
constructor
Creates an action spec based on the provided spec hash.
-
#take ⇒ Object
A string containing the invocation of this action.
Constructor Details
#initialize(spec) ⇒ ActionSpec
Creates an action spec based on the provided spec hash.
The hash must have the following keys:
- name
-
the type of action to take (currently only throw-exception is
supported)
- constructor
-
the function to use to create the exception, described as a
wrapped function call
56 57 58 |
# File 'lib/wrapture/action_spec.rb', line 56 def initialize(spec) @spec = ActionSpec.normalize_spec_hash(spec) end |
Class Method Details
.normalize_spec_hash(spec) ⇒ Object
Normalizes a hash specification of a rule. Normalization checks for invalid keys and unrecognized conditions.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/wrapture/action_spec.rb', line 26 def self.normalize_spec_hash(spec) normalized = spec.dup required_keys = %w[name constructor] missing_keys = required_keys - spec.keys unless missing_keys.empty? missing_msg = "required keys are missing: #{missing_keys.join(', ')}" raise(MissingSpecKey, missing_msg) end extra_keys = spec.keys - required_keys unless extra_keys.empty? extra_msg = "these keys are unrecognized: #{extra_keys.join(', ')}" raise(InvalidSpecKey, extra_msg) end func_spec = WrappedFunctionSpec.normalize_spec_hash(spec['constructor']) normalized['constructor'] = func_spec normalized end |
Instance Method Details
#includes ⇒ Object
A list of includes needed for the action.
61 62 63 |
# File 'lib/wrapture/action_spec.rb', line 61 def includes @spec['constructor']['includes'].dup end |
#take ⇒ Object
A string containing the invocation of this action.
66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/wrapture/action_spec.rb', line 66 def take call_spec = @spec['constructor'] params = call_spec['params'].map do |param_spec| if param_spec['value'] == RETURN_VALUE_KEYWORD 'return_val' else param_spec['value'] end end "throw #{call_spec['name']}( #{params.join(', ')} )" end |