Class: SparkleFormation::FunctionStruct

Inherits:
AttributeStruct
  • Object
show all
Defined in:
lib/sparkle_formation/function_struct.rb

Overview

SparkleFormation customized AttributeStruct targeted at defining strings of code for remote evaulation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(f_name = nil, *args) ⇒ self

Create a new FunctionStruct instance

Parameters:

  • f_name (String) (defaults to: nil)

    name of function

  • args (Array<Object>)

    argument list



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

Parameters:

  • name (String, Symbol)

    method name

  • args (Object<Array>)

    argument list

Returns:

  • (Object)


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_argsArray<Object> (readonly)

Returns function argument list.

Returns:

  • (Array<Object>)

    function argument list



12
13
14
# File 'lib/sparkle_formation/function_struct.rb', line 12

def _fn_args
  @_fn_args
end

#_fn_nameString (readonly)

Returns name of function.

Returns:

  • (String)

    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

Parameters:

  • val (Integer, String)

Returns:



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_startString

Returns start character(s) used to anchor function call.

Returns:

  • (String)

    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_stopString

Returns stop character(s) used to anchor function call.

Returns:

  • (String)

    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_listString

Returns value to use when argument list is empty.

Returns:

  • (String)

    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

Returns:

  • (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.

Returns:

  • (TrueClass)

    wrap in single anchor



137
138
139
# File 'lib/sparkle_formation/function_struct.rb', line 137

def __single_anchor?
  true
end

#__single_quote_stringsTrueClass

Returns enable single quote string generation.

Returns:

  • (TrueClass)

    enable single quote string generation



172
173
174
# File 'lib/sparkle_formation/function_struct.rb', line 172

def __single_quote_strings
  true
end

#_dumpString

Override of the dump to properly format eval string

Returns:

  • (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

Parameters:

  • args (Array<String>)

Returns:

  • (String)


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

#_klassClass

Returns:

  • (Class)


142
143
144
# File 'lib/sparkle_formation/function_struct.rb', line 142

def _klass
  ::SparkleFormation::FunctionStruct
end

#inspectString

Returns dump from root.

Returns:

  • (String)

    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.

Returns:

  • (False)

    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.

Returns:

  • (TrueClass, FalseClass)

    is root struct



38
39
40
# File 'lib/sparkle_formation/function_struct.rb', line 38

def root?
  _parent.nil?
end

#to_sString

Returns dump from root.

Returns:

  • (String)

    dump from root



162
163
164
# File 'lib/sparkle_formation/function_struct.rb', line 162

def to_s
  _root._dump
end