Class: FeedMe::FeedData

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

Direct Known Subclasses

Parser

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tag_name, parent, builder) ⇒ FeedData



309
310
311
312
313
314
# File 'lib/feedme.rb', line 309

def initialize(tag_name, parent, builder)
  @fm_tag_name = tag_name
  @fm_parent = parent
  @fm_builder = builder
  @data = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



360
361
362
363
364
365
366
367
368
# File 'lib/feedme.rb', line 360

def method_missing(name, *args)
  result = begin
    call_virtual_method(name, args)
  rescue NameError
    raise if fm_builder.options[:error_on_missing_key]
  end
  result = '' if result.nil? and fm_builder.options[:empty_string_for_nil]
  result
end

Instance Attribute Details

#fm_builderObject (readonly)

Returns the value of attribute fm_builder.



307
308
309
# File 'lib/feedme.rb', line 307

def fm_builder
  @fm_builder
end

#fm_parentObject (readonly)

Returns the value of attribute fm_parent.



307
308
309
# File 'lib/feedme.rb', line 307

def fm_parent
  @fm_parent
end

#fm_tag_nameObject (readonly)

Returns the value of attribute fm_tag_name.



307
308
309
# File 'lib/feedme.rb', line 307

def fm_tag_name
  @fm_tag_name
end

Instance Method Details

#[](key) ⇒ Object



336
337
338
# File 'lib/feedme.rb', line 336

def [](key)
  @data[clean_tag(key)]
end

#[]=(key, value) ⇒ Object



340
341
342
# File 'lib/feedme.rb', line 340

def []=(key, value)
  @data[clean_tag(key)] = value
end

#call_virtual_method(sym, args = [], history = []) ⇒ Object

There are several virtual methods for each attribute/tag.

  1. Tag/attribute name: since tags/attributes are stored as arrays,

the instance variable name is the tag/attribute name followed by ‘_array’. The tag/attribute name is actually a virtual method that returns the first element in the array.

  1. Aliases: for tags/attributes with aliases, the alias is a virtual

method that simply forwards to the aliased method.

  1. Any name that ends with a ‘?’ returns true if the name without

the ‘?’ is a valid method and has a non-nil value.

  1. Any name that ends with a ‘!’ returns the value of the name

without the ‘!’, modified by the currently active set of bang mods

  1. Tag/attribute name + ‘_value’: returns the content portion of

an element if it has both attributes and content, , or to return the default attribute (defined by the value_tags property). Otherwise equivalent to just the tag/attribute name.

  1. Tag/attribute name + ‘_count’: shortcut for tag/attribute

array.size.

  1. If the tag name is of the form “tag+rel”, the tag having the

specified rel value is returned

Raises:

  • (NameError)


389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/feedme.rb', line 389

def call_virtual_method(sym, args=[], history=[])
  # make sure we don't get stuck in an infinite loop
  history.each do |call|
    if call[0] == fm_tag_name and call[1] == sym
      raise FeedMe::InfiniteCallLoopError.new(sym, history) 
    end
  end
  history << [ fm_tag_name, sym ]
          
  name = clean_tag(sym)
  name_str = name.to_s
  array_key = arrayize(name.to_s)

  result = if key? name
    self[name]
  elsif key? array_key
    (array_key == :items_array) ? self[:item_array] : self[array_key].first
  elsif name_str[-1,1] == '?'
    !call_virtual_method(name_str[0..-2], args, history).nil? rescue false
  elsif name_str[-1,1] == '!'
    transform(fm_builder.default_transformation, name_str[0..-2], args, history)
  elsif name_str =~ /(.+)_value/
    obj = call_virtual_method($1, args, history)
    value = obj
    if obj.is_a?(FeedData)
      fm_builder.value_tags.each do |tag|
        value = obj.call_virtual_method(tag, args, history) rescue next
        break unless value.nil?
      end
    end
    value
  elsif name_str =~ /(.+)_count/
    call_virtual_method(arrayize($1), args, history).size
  elsif name_str =~ /(.+)_(.+)/ && fm_builder.transformations.key?($2)
    transform(fm_builder.transformations[$2], $1, args, history)
  elsif name_str.include?('/')    # this is only intended to be used internally 
    value = self
    name_str.split('/').each do |p|
      parts = p.split('_')
      name = clean_tag(parts[0])
      new_args = parts.size > 1 ? parts[1..-1] : args
      value = (value.method(name).call(*new_args) rescue value.call_virtual_method(name, new_args, history)) rescue nil
      break if value.nil?
    end
    value
  elsif name_str.include?('+')
  name_data = name_str.split('+')
  rel = name_data[1]
  value = nil
  call_virtual_method(arrayize(name_data[0]), args, history).each do |elt|
    next unless elt.is_a?(FeedData) and elt.rel?
    value = elt if elt.rel.casecmp(rel) == 0
    break unless value.nil?
  end
  value
elsif fm_builder.aliases.key? name
    names = fm_builder.aliases[name]
    names = [names] unless names.is_a? Array
    value = nil
    names.each do |name|
      value = (method(name).call(*args) rescue call_virtual_method(name, args, history)) rescue next
      break unless value.nil?
    end
    value
  else
    nil
  end

  raise NameError.new("No such method #{name}", name) if result.nil?

  result
end

#eachObject



324
325
326
# File 'lib/feedme.rb', line 324

def each
  @data.each {|key, value| yield(key, value) }
end

#each_with_indexObject



328
329
330
# File 'lib/feedme.rb', line 328

def each_with_index
  @data.each_with_index {|key, value, index| yield(key, value, index) }
end

#idObject

special handling for atom id tags, due to conflict with ruby’s Object#id method



346
347
348
# File 'lib/feedme.rb', line 346

def id
  key?(:id) ? self[:id] : call_virtual_method(:id)
end

#key?(key) ⇒ Boolean



316
317
318
# File 'lib/feedme.rb', line 316

def key?(key)
  @data.key?(clean_tag(key))
end

#keysObject



320
321
322
# File 'lib/feedme.rb', line 320

def keys
  @data.keys
end

#sizeObject



332
333
334
# File 'lib/feedme.rb', line 332

def size
  @data.size
end

#to_indented_s(indent_step = 2) ⇒ Object



354
355
356
357
358
# File 'lib/feedme.rb', line 354

def to_indented_s(indent_step=2)
  FeedMe.pretty_to_s(self, indent_step, 0, Proc.new do |key, value| 
    (value.is_a?(Array) && value.size == 1) ? [unarrayize(key), value.first] : [key, value]
  end)
end

#to_sObject



350
351
352
# File 'lib/feedme.rb', line 350

def to_s
  to_indented_s
end