class SparkleFormation::FunctionStruct
SparkleFormation
customized AttributeStruct targeted at defining strings of code for remote evaulation
Attributes
@return [Array<Object>] function argument list
@return [String] name of function
Public Class Methods
Create a new FunctionStruct
instance
@param f_name [String] name of function @param args [Array<Object>] argument list @return [self]
# 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
Public Instance Methods
@return [TrueClass, FalseClass]
# File lib/sparkle_formation/function_struct.rb, line 70 def ==(_other) eql?(_other) end
Set accessor directly into table data
@param val [Integer, String] @return [FunctionStruct]
# File lib/sparkle_formation/function_struct.rb, line 102 def [](val) if val.is_a?(::String) && __single_quote_strings _set("['#{val}']") else _set("[#{val.inspect}]") end end
@return [String] start character(s) used to anchor function call
# File lib/sparkle_formation/function_struct.rb, line 191 def __anchor_start "[" end
@return [String] stop character(s) used to anchor function call
# File lib/sparkle_formation/function_struct.rb, line 196 def __anchor_stop "]" end
@return [String] value to use when argument list is empty
# File lib/sparkle_formation/function_struct.rb, line 201 def __empty_argument_list "()" end
# File lib/sparkle_formation/function_struct.rb, line 176 def __quote_nested_funcs? false end
@return [TrueClass] wrap in single anchor
# File lib/sparkle_formation/function_struct.rb, line 181 def __single_anchor? true end
@return [TrueClass] enable single quote string generation
# File lib/sparkle_formation/function_struct.rb, line 216 def __single_quote_strings true end
Create a clone of this instance
# File lib/sparkle_formation/function_struct.rb, line 33 def _clone(*_) new_inst = _klass_new(_fn_name, *_fn_args) new_inst._data.replace(__hashish[ @table.map { |_key, _value| if _key.is_a?(::AttributeStruct) _key = _key._clone else _key = _do_dup(_key) end if _value.is_a?(::AttributeStruct) _value = _value._clone else _value = _do_dup(_value) end [_key, _value] } ]) new_inst end
Override of the dump to properly format eval string
@return [String]
# File lib/sparkle_formation/function_struct.rb, line 113 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
Join arguments into a string for remote evaluation
@param args [Array<String>] @return [String]
# File lib/sparkle_formation/function_struct.rb, line 164 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
@return [Class]
# File lib/sparkle_formation/function_struct.rb, line 186 def _klass ::SparkleFormation::FunctionStruct end
@return [TrueClass, FalseClass]
# File lib/sparkle_formation/function_struct.rb, line 59 def eql?(_other) if _other.respond_to?(:_dump) && _other.respond_to?(:_parent) ::MultiJson.dump(_dump).hash == ::MultiJson.dump(_other._dump).hash && _parent == _other._parent else false end end
@return [Numeric] hash value for instance
# File lib/sparkle_formation/function_struct.rb, line 54 def hash _dump.hash end
@return [String] dump from root
# File lib/sparkle_formation/function_struct.rb, line 211 def inspect _root._dump end
Override to provide expected behavior when arguments are passed to a function call
@param name [String, Symbol] method name @param args [Object<Array>] argument list @return [Object]
# File lib/sparkle_formation/function_struct.rb, line 90 def method_missing(name, *args) if args.empty? super else @table["_function_"] = _klass_new(name, *args) end end
@return [False] functions are never nil
# File lib/sparkle_formation/function_struct.rb, line 75 def nil? false end
@return [TrueClass, FalseClass] is root struct
# File lib/sparkle_formation/function_struct.rb, line 80 def root? _parent.nil? end
@return [String] dump from root
# File lib/sparkle_formation/function_struct.rb, line 206 def to_s _root._dump end