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
# File 'lib/sparkle_formation/function_struct.rb', line 60

def [](val)
  _set("[#{val}]")
end

#_dumpString

Override of the dump to properly format eval string

Returns:

  • (String)


67
68
69
70
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
# File 'lib/sparkle_formation/function_struct.rb', line 67

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))
        "'#{::Bogo::Utility.camel(arg.to_s, false)}'"
      elsif(arg.is_a?(::String))
        "'#{arg}'"
      else
        arg.inspect
      end
    end.join(', ')
    unless(_fn_name.to_s.empty?)
      function_name = args.empty? ? "#{_fn_name}()" : "#{_fn_name}(#{args})"
    end
    internal = _eval_join(
      *[
        function_name,
        suffix
      ].compact
    )
    root? ? "[#{internal}]" : internal
  else
    suffix
  end
end

#_eval_join(*args) ⇒ String

Join arguments into a string for remote evaluation

Parameters:

  • args (Array<String>)

Returns:

  • (String)


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/sparkle_formation/function_struct.rb', line 108

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)


121
122
123
# File 'lib/sparkle_formation/function_struct.rb', line 121

def _klass
  ::SparkleFormation::FunctionStruct
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