Class: Tagtical::Tag::Type::Collection

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

Overview

Instead of subclassing from Array, we make a Proxy, so we actually can use method_missing for catching results and converting them into Collection class if they return Array

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Collection

Returns a new instance of Collection.



475
476
477
# File 'lib/tagtical/tag.rb', line 475

def initialize(*args)
  @array = Array.new(*args)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



500
501
502
503
504
505
506
507
# File 'lib/tagtical/tag.rb', line 500

def method_missing(name, *args)
  result = if block_given?
    @array.send(name, *args)  { |*block_args| yield(*block_args) }
  else
    @array.send(name, *args)
  end
  result.is_a?(@array.class) ? self.class.new(result) : result
end

Instance Method Details

#get(tag_type_names) ⇒ Object

Usage:

tag_types.get(["boy", "girl"]) # => ["boy", "girl"]
tag_types.get("boy")           # => "boy"
# "boy".class === Tagtical::Tag::Type everywhere


483
484
485
486
# File 'lib/tagtical/tag.rb', line 483

def get(tag_type_names)
  results = @array.select { |tag_type| tag_type_names.include?(tag_type) }
  tag_type_names.is_a?(Array) ? results : results.first
end

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


488
489
490
# File 'lib/tagtical/tag.rb', line 488

def respond_to?(*args)
  super || @array.respond_to?(*args)
end

#to_aObject



496
497
498
# File 'lib/tagtical/tag.rb', line 496

def to_a
  @array
end

#to_aryObject



492
493
494
# File 'lib/tagtical/tag.rb', line 492

def to_ary
  @array
end