Class: FeedMe::FeedData
- Inherits:
-
Object
- Object
- FeedMe::FeedData
- Defined in:
- lib/feedme.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#fm_builder ⇒ Object
readonly
Returns the value of attribute fm_builder.
-
#fm_parent ⇒ Object
readonly
Returns the value of attribute fm_parent.
-
#fm_tag_name ⇒ Object
readonly
Returns the value of attribute fm_tag_name.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#call_virtual_method(sym, args = [], history = []) ⇒ Object
There are several virtual methods for each attribute/tag.
- #delete(key) ⇒ Object
- #each ⇒ Object
- #each_with_index ⇒ Object
-
#id ⇒ Object
special handling for atom id tags, due to conflict with ruby’s Object#id method.
-
#initialize(tag_name, parent, builder) ⇒ FeedData
constructor
A new instance of FeedData.
- #key?(key) ⇒ Boolean
- #keys ⇒ Object
- #method_missing(name, *args) ⇒ Object
- #size ⇒ Object
- #to_indented_s(indent_step = 2) ⇒ Object
- #to_s ⇒ Object
-
#transform(tag, trans) ⇒ Object
Apply transformations to a tag value.
Constructor Details
#initialize(tag_name, parent, builder) ⇒ FeedData
Returns a new instance of FeedData.
304 305 306 307 308 309 |
# File 'lib/feedme.rb', line 304 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
359 360 361 362 363 364 365 366 367 |
# File 'lib/feedme.rb', line 359 def method_missing(name, *args) result = begin call_virtual_method(name, args) rescue NameError raise if fm_builder.[:error_on_missing_key] end result = '' if result.nil? and fm_builder.[:empty_string_for_nil] result end |
Instance Attribute Details
#fm_builder ⇒ Object (readonly)
Returns the value of attribute fm_builder.
302 303 304 |
# File 'lib/feedme.rb', line 302 def fm_builder @fm_builder end |
#fm_parent ⇒ Object (readonly)
Returns the value of attribute fm_parent.
302 303 304 |
# File 'lib/feedme.rb', line 302 def fm_parent @fm_parent end |
#fm_tag_name ⇒ Object (readonly)
Returns the value of attribute fm_tag_name.
302 303 304 |
# File 'lib/feedme.rb', line 302 def fm_tag_name @fm_tag_name end |
Instance Method Details
#[](key) ⇒ Object
335 336 337 |
# File 'lib/feedme.rb', line 335 def [](key) @data[clean_tag(key)] end |
#[]=(key, value) ⇒ Object
339 340 341 |
# File 'lib/feedme.rb', line 339 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.
-
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.
-
Aliases: for tags/attributes with aliases, the alias is a virtual
method that simply forwards to the aliased method.
-
Any name that ends with a ‘?’ returns true if the name without
the ‘?’ is a valid method and has a non-nil value.
-
Any name that ends with a ‘!’ returns the value of the name
without the ‘!’, modified by the currently active set of bang mods
-
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.
-
Tag/attribute name + ‘_count’: shortcut for tag/attribute
array.size.
-
If the tag name is of the form “tag+rel”, the tag having the
specified rel value is returned
388 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 |
# File 'lib/feedme.rb', line 388 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 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] == '!' value = call_virtual_method(name_str[0..-2], args, history) _transform(fm_builder.default_transformation, value) elsif name_str =~ /(.+)_values/ call_virtual_method(arrayize($1), args, history).collect do |value| _resolve_value value end elsif name_str =~ /(.+)_value/ _resolve_value call_virtual_method($1, args, history) elsif name_str =~ /(.+)_count/ call_virtual_method(arrayize($1), args, history).size elsif name_str =~ /(.+)_(.+)/ && fm_builder.transformations.key?($2) value = call_virtual_method($1, args, history) _transform(fm_builder.transformations[$2], value) 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 |
#delete(key) ⇒ Object
319 320 321 |
# File 'lib/feedme.rb', line 319 def delete(key) @data.delete(clean_tag(key)) end |
#each ⇒ Object
323 324 325 |
# File 'lib/feedme.rb', line 323 def each @data.each {|key, value| yield(key, value) } end |
#each_with_index ⇒ Object
327 328 329 |
# File 'lib/feedme.rb', line 327 def each_with_index @data.each_with_index {|key, value, index| yield(key, value, index) } end |
#id ⇒ Object
special handling for atom id tags, due to conflict with ruby’s Object#id method
345 346 347 |
# File 'lib/feedme.rb', line 345 def id key?(:id) ? self[:id] : call_virtual_method(:id) end |
#key?(key) ⇒ Boolean
311 312 313 |
# File 'lib/feedme.rb', line 311 def key?(key) @data.key?(clean_tag(key)) end |
#keys ⇒ Object
315 316 317 |
# File 'lib/feedme.rb', line 315 def keys @data.keys end |
#size ⇒ Object
331 332 333 |
# File 'lib/feedme.rb', line 331 def size @data.size end |
#to_indented_s(indent_step = 2) ⇒ Object
353 354 355 356 357 |
# File 'lib/feedme.rb', line 353 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_s ⇒ Object
349 350 351 |
# File 'lib/feedme.rb', line 349 def to_s to_indented_s end |
#transform(tag, trans) ⇒ Object
Apply transformations to a tag value. Can either accept a transformation name or an array of transformation function names.
463 464 465 466 467 468 |
# File 'lib/feedme.rb', line 463 def transform(tag, trans) value = call_virtual_method(tag) or return nil transformations = trans.is_a?(String) ? fm_builder.transformations[trans] : trans _transform(transformations, value) end |