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



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



90
91
92
93
94
95
96
# 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

Instance Attribute Details

#_fn_argsArray<Object> (readonly)



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

def _fn_args
  @_fn_args
end

#_fn_nameString (readonly)



10
11
12
# File 'lib/sparkle_formation/function_struct.rb', line 10

def _fn_name
  @_fn_name
end

Instance Method Details

#==(_other) ⇒ TrueClass, FalseClass



70
71
72
# File 'lib/sparkle_formation/function_struct.rb', line 70

def ==(_other)
  eql?(_other)
end

#[](val) ⇒ FunctionStruct

Set accessor directly into table data



102
103
104
105
106
107
108
# 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

#__anchor_startString



191
192
193
# File 'lib/sparkle_formation/function_struct.rb', line 191

def __anchor_start
  "["
end

#__anchor_stopString



196
197
198
# File 'lib/sparkle_formation/function_struct.rb', line 196

def __anchor_stop
  "]"
end

#__empty_argument_listString



201
202
203
# File 'lib/sparkle_formation/function_struct.rb', line 201

def __empty_argument_list
  "()"
end

#__quote_nested_funcs?Boolean



176
177
178
# File 'lib/sparkle_formation/function_struct.rb', line 176

def __quote_nested_funcs?
  false
end

#__single_anchor?TrueClass



181
182
183
# File 'lib/sparkle_formation/function_struct.rb', line 181

def __single_anchor?
  true
end

#__single_quote_stringsTrueClass



216
217
218
# File 'lib/sparkle_formation/function_struct.rb', line 216

def __single_quote_strings
  true
end

#_clone(*_) ⇒ Object

Create a clone of this instance



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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

#_dumpString Also known as: _sparkle_dump

Override of the dump to properly format eval string



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# 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

#_eval_join(*args) ⇒ String

Join arguments into a string for remote evaluation



164
165
166
167
168
169
170
171
172
173
174
# 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

#_klassClass



186
187
188
# File 'lib/sparkle_formation/function_struct.rb', line 186

def _klass
  ::SparkleFormation::FunctionStruct
end

#eql?(_other) ⇒ TrueClass, FalseClass



59
60
61
62
63
64
65
66
67
# 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

#hashNumeric



54
55
56
# File 'lib/sparkle_formation/function_struct.rb', line 54

def hash
  _dump.hash
end

#inspectString



211
212
213
# File 'lib/sparkle_formation/function_struct.rb', line 211

def inspect
  _root._dump
end

#nil?False



75
76
77
# File 'lib/sparkle_formation/function_struct.rb', line 75

def nil?
  false
end

#root?TrueClass, FalseClass



80
81
82
# File 'lib/sparkle_formation/function_struct.rb', line 80

def root?
  _parent.nil?
end

#to_sString



206
207
208
# File 'lib/sparkle_formation/function_struct.rb', line 206

def to_s
  _root._dump
end