Class: Tequila::Node

Inherits:
Object
  • Object
show all
Defined in:
lib/tree.rb

Defined Under Namespace

Classes: Attribute, CodeBlock, ImpreciseAttributesDeclarationError, Method, NoAttributeError, Static

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, type, bounded = false) ⇒ Node

#
main class             #
#


197
198
199
200
201
202
203
204
205
206
# File 'lib/tree.rb', line 197

def initialize(name, type, bounded = false)
  @name = name
  @label = @name.gsub(/[@\$]/,'')
  @type = type
  @methods = []
  @attributes = { :only => [], :except => []}
  @code_blocks = []
  @statics = []
  @suppress_label = false
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



115
116
117
# File 'lib/tree.rb', line 115

def attributes
  @attributes
end

#code_blocksObject

Returns the value of attribute code_blocks.



116
117
118
# File 'lib/tree.rb', line 116

def code_blocks
  @code_blocks
end

#contentObject (readonly)

Returns the value of attribute content.



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

def content
  @content
end

#drop_allObject (readonly)

Returns the value of attribute drop_all.



126
127
128
# File 'lib/tree.rb', line 126

def drop_all
  @drop_all
end

#labelObject

Returns the value of attribute label.



117
118
119
# File 'lib/tree.rb', line 117

def label
  @label
end

#methodsObject

Returns the value of attribute methods.



114
115
116
# File 'lib/tree.rb', line 114

def methods
  @methods
end

#nameObject (readonly)

Returns the value of attribute name.



120
121
122
# File 'lib/tree.rb', line 120

def name
  @name
end

#pick_allObject (readonly)

it is used for attributes if ‘all’ keyword was specified



125
126
127
# File 'lib/tree.rb', line 125

def pick_all
  @pick_all
end

#staticsObject

Returns the value of attribute statics.



119
120
121
# File 'lib/tree.rb', line 119

def statics
  @statics
end

#suppress_labelObject

Returns the value of attribute suppress_label.



118
119
120
# File 'lib/tree.rb', line 118

def suppress_label
  @suppress_label
end

#typeObject (readonly)

Returns the value of attribute type.



122
123
124
# File 'lib/tree.rb', line 122

def type
  @type
end

Instance Method Details

#add_attribute(key, attr) ⇒ Object



272
273
274
275
276
277
# File 'lib/tree.rb', line 272

def add_attribute(key, attr)
  raise ImpreciseAttributesDeclarationError if (pick_all || drop_all)
  unless @attributes[key].include?(attr)
    @attributes[key] << attr
  end
end

#all_attributes!Object



287
288
289
290
291
292
293
# File 'lib/tree.rb', line 287

def all_attributes!
  if drop_all
    raise ImpreciseAttributesDeclarationError, "declaration conflict"
  else
    @pick_all = true
  end
end

#apply(context) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# File 'lib/tree.rb', line 218

def apply(context)
  mapping = {}
  if context.respond_to?(:attributes) && !drop_all

    if (pick_all || drop_all)
      unless attributes[:only].empty? && attributes[:except].empty?
        raise ImpreciseAttributesDeclarationError, "declaration conflict"
      end
    end

    context_attributes = context.attributes
    #p "All: #{context_attributes.keys.inspect}"
    if attributes[:only].size > 0
      chosen_attributes = attributes[:only].map(&:name)
      #p "Chosen: #{chosen_attributes}"
      unless (foreign_attributes = (chosen_attributes.to_set - context_attributes.keys.to_set)).empty?
        raise NoAttributeError, "can't find attributes: #{foreign_attributes.to_a.join(', ')}"
      end
      attributes[:only].inject({}) do |res, att|
        res[att.label] = context_attributes[att.name]
        res
      end
    elsif attributes[:except].size > 0

      ignored_attributes = attributes[:except].map(&:name)
      #p "Ignored: #{ignored_attributes.inspect}"
      unless (foreign_attributes = (ignored_attributes.to_set - context_attributes.keys.to_set)).empty?
        raise NoAttributeError, "can't find attributes: #{foreign_attributes.to_a.join(', ')}"
      end
      context_attributes.delete_if { |att_name, _| ignored_attributes.include?(att_name) }
    else
      # use all variables by default if they are supported
      context_attributes
    end
  else
    {}
  end.merge(
    (methods || []).inject({}) do |res, m|
      res[m.label] = context.send(m.name.intern, *(m.params.map {|p| context.instance_eval p}))
      res
    end
  ).merge(
    code_blocks.inject({}) do |res, cb|
      res[cb.label] = context.instance_eval cb.code
      res
    end
  ).merge(
    statics.inject({}) do |res, s|
      res[s.label] = s.value
      res
    end
  )
end

#bounded?Boolean

Returns:

  • (Boolean)


295
296
297
# File 'lib/tree.rb', line 295

def bounded?
  :bounded == type
end

#eval(vars) ⇒ Object



208
209
210
211
212
213
214
215
216
# File 'lib/tree.rb', line 208

def eval(vars)
  @content = case type
  when :variable
    lambda { Kernel.eval name, vars }
  when :association, :bounded
    lambda { |context| context.send(name.intern)}
  end
  self
end

#no_attributes!Object



279
280
281
282
283
284
285
# File 'lib/tree.rb', line 279

def no_attributes!
  if pick_all
    raise ImpreciseAttributesDeclarationError, "declaration conflict"
  else
    @drop_all = true
  end
end

#to_sObject



299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/tree.rb', line 299

def to_s
  "Node: " <<
    ((name == label) ? name : "#{name}(#{label})") <<
    (methods.empty? ? ' ' : " Methods: #{methods.map(&:to_s).join(',')} ")  <<
    (if attributes[:except].size > 0
      "Except: #{attributes[:except].to_s}"
    elsif attributes[:only].size > 0
      "Only: #{attributes[:only].to_s}"
    else
      ''
    end) <<
    ((code_blocks.size > 0) ? "Code blocks: \n" << code_blocks.map(&:to_s).join : '')
end