Class: SparkleFormation::FunctionStruct
- Inherits:
-
AttributeStruct
- Object
- AttributeStruct
- SparkleFormation::FunctionStruct
- Defined in:
- lib/sparkle_formation/function_struct.rb
Overview
SparkleFormation customized AttributeStruct targeted at defining strings of code for remote evaulation
Direct Known Subclasses
GoogleStruct, JinjaExpressionStruct, JinjaStatementStruct, TerraformStruct
Instance Attribute Summary collapse
-
#_fn_args ⇒ Array<Object>
readonly
Function argument list.
-
#_fn_name ⇒ String
readonly
Name of function.
Instance Method Summary collapse
-
#[](val) ⇒ FunctionStruct
Set accessor directly into table data.
-
#__anchor_start ⇒ String
Start character(s) used to anchor function call.
-
#__anchor_stop ⇒ String
Stop character(s) used to anchor function call.
-
#__empty_argument_list ⇒ String
Value to use when argument list is empty.
- #__quote_nested_funcs? ⇒ Boolean
-
#__single_anchor? ⇒ TrueClass
Wrap in single anchor.
-
#__single_quote_strings ⇒ TrueClass
Enable single quote string generation.
-
#_dump ⇒ String
Override of the dump to properly format eval string.
-
#_eval_join(*args) ⇒ String
Join arguments into a string for remote evaluation.
- #_klass ⇒ Class
-
#initialize(f_name = nil, *args) ⇒ self
constructor
Create a new FunctionStruct instance.
-
#inspect ⇒ String
Dump from root.
-
#method_missing(name, *args) ⇒ Object
Override to provide expected behavior when arguments are passed to a function call.
-
#nil? ⇒ False
Functions are never nil.
-
#root? ⇒ TrueClass, FalseClass
Is root struct.
-
#to_s ⇒ String
Dump from root.
Constructor Details
#initialize(f_name = nil, *args) ⇒ self
Create a new FunctionStruct instance
19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/sparkle_formation/function_struct.rb', line 19 def initialize(f_name=nil, *args) super() @_fn_name = f_name.to_s @_fn_args = args @_fn_args.map! do |l_arg| if(l_arg.is_a?(_klass)) l_arg = l_arg._root l_arg._parent(self) end l_arg end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
Override to provide expected behavior when arguments are passed to a function call
48 49 50 51 52 53 54 |
# File 'lib/sparkle_formation/function_struct.rb', line 48 def method_missing(name, *args) if(args.empty?) super else @table['_function_'] = _klass_new(name, *args) end end |
Instance Attribute Details
#_fn_args ⇒ Array<Object> (readonly)
Returns function argument list.
12 13 14 |
# File 'lib/sparkle_formation/function_struct.rb', line 12 def _fn_args @_fn_args end |
#_fn_name ⇒ String (readonly)
Returns name of function.
10 11 12 |
# File 'lib/sparkle_formation/function_struct.rb', line 10 def _fn_name @_fn_name end |
Instance Method Details
#[](val) ⇒ FunctionStruct
Set accessor directly into table data
60 61 62 63 64 65 66 |
# File 'lib/sparkle_formation/function_struct.rb', line 60 def [](val) if(val.is_a?(::String) && __single_quote_strings) _set("['#{val}']") else _set("[#{val.inspect}]") end end |
#__anchor_start ⇒ String
Returns start character(s) used to anchor function call.
147 148 149 |
# File 'lib/sparkle_formation/function_struct.rb', line 147 def __anchor_start '[' end |
#__anchor_stop ⇒ String
Returns stop character(s) used to anchor function call.
152 153 154 |
# File 'lib/sparkle_formation/function_struct.rb', line 152 def __anchor_stop ']' end |
#__empty_argument_list ⇒ String
Returns value to use when argument list is empty.
157 158 159 |
# File 'lib/sparkle_formation/function_struct.rb', line 157 def __empty_argument_list '()' end |
#__quote_nested_funcs? ⇒ Boolean
132 133 134 |
# File 'lib/sparkle_formation/function_struct.rb', line 132 def __quote_nested_funcs? false end |
#__single_anchor? ⇒ TrueClass
Returns wrap in single anchor.
137 138 139 |
# File 'lib/sparkle_formation/function_struct.rb', line 137 def __single_anchor? true end |
#__single_quote_strings ⇒ TrueClass
Returns enable single quote string generation.
172 173 174 |
# File 'lib/sparkle_formation/function_struct.rb', line 172 def __single_quote_strings true end |
#_dump ⇒ String
Override of the dump to properly format eval string
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/sparkle_formation/function_struct.rb', line 71 def _dump unless(@table.empty?) key, value = @table.first suffix = _eval_join( *[ key == '_function_' ? nil : key, !value.nil? ? value._dump : nil ].compact ) end if(_fn_name) args = _fn_args.map do |arg| if(arg.respond_to?(:_dump)) arg._dump elsif(arg.is_a?(::Symbol)) quote = __single_quote_strings ? "'" : '"' "#{quote}#{::Bogo::Utility.camel(arg.to_s, false)}#{quote}" elsif(arg.is_a?(::String) && __single_quote_strings) "'#{arg}'" else arg.inspect end end.join(', ') unless(_fn_name.to_s.empty?) function_name = args.empty? ? "#{_fn_name}#{__empty_argument_list}" : "#{_fn_name}(#{args})" end internal = _eval_join( *[ function_name, suffix ].compact ) if(root? || (!__single_anchor? && function_name)) if(!root? && __quote_nested_funcs?) quote = __single_quote_strings ? "'" : '"' end "#{quote}#{__anchor_start}#{internal}#{__anchor_stop}#{quote}" else internal end else suffix end end |
#_eval_join(*args) ⇒ String
Join arguments into a string for remote evaluation
120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/sparkle_formation/function_struct.rb', line 120 def _eval_join(*args) args = args.compact args.delete_if(&:empty?) args.slice(1, args.size).to_a.inject(args.first) do |memo, item| if(item.start_with?('[')) memo += item else memo += ".#{item}" end end end |
#_klass ⇒ Class
142 143 144 |
# File 'lib/sparkle_formation/function_struct.rb', line 142 def _klass ::SparkleFormation::FunctionStruct end |
#inspect ⇒ String
Returns dump from root.
167 168 169 |
# File 'lib/sparkle_formation/function_struct.rb', line 167 def inspect _root._dump end |
#nil? ⇒ False
Returns functions are never nil.
33 34 35 |
# File 'lib/sparkle_formation/function_struct.rb', line 33 def nil? false end |
#root? ⇒ TrueClass, FalseClass
Returns is root struct.
38 39 40 |
# File 'lib/sparkle_formation/function_struct.rb', line 38 def root? _parent.nil? end |
#to_s ⇒ String
Returns dump from root.
162 163 164 |
# File 'lib/sparkle_formation/function_struct.rb', line 162 def to_s _root._dump end |