Class: EacRubyUtils::ObjectsTable

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

Constant Summary collapse

BY_PARSER =
::EacRubyUtils::RegexpParser.new(/\Aby_(.+)\z/) { |m| m[1] }

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



30
31
32
33
34
35
36
# File 'lib/eac_ruby_utils/objects_table.rb', line 30

def method_missing(method_name, *arguments, &block)
  if (attribute = BY_PARSER.parse(method_name))
    return by_attribute(attribute, *arguments, &block)
  end

  super
end

Instance Method Details

#by_attribute(name, value) ⇒ Object?

Parameters:

Returns:



14
15
16
# File 'lib/eac_ruby_utils/objects_table.rb', line 14

def by_attribute(name, value)
  objects.find { |e| e.send(name) == value }
end

#by_attribute!(name, value) ⇒ Object

Parameters:

Returns:



21
22
23
24
# File 'lib/eac_ruby_utils/objects_table.rb', line 21

def by_attribute!(name, value)
  by_attribute(name, value) ||
    raise(::ArgumentError, "No item found with attribute #{name}=#{value}")
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:



26
27
28
# File 'lib/eac_ruby_utils/objects_table.rb', line 26

def respond_to_missing?(method_name, include_private = false)
  super || BY_PARSER.parse?(method_name)
end