Sha256: 8fe5a66da681574bb680206bd7593fb0fc8bb972301379b9e4aef008085a00af

Contents?: true

Size: 1.27 KB

Versions: 79

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

require 'active_support/hash_with_indifferent_access'
require 'active_support/core_ext/string/inflections'
require_relative 'integer_list'
require_relative 'string_list'
require_relative 'symbol_list'

module EacRubyUtils
  module Listable
    class Lists
      attr_reader :source

      def initialize(source)
        @source = source
      end

      %w[integer string symbol].each do |list_type|
        define_method "add_#{list_type}" do |item, *labels|
          add(::EacRubyUtils::Listable.const_get("#{list_type}_list".camelize), item, labels)
        end
      end

      def method_missing(name, *args, &block)
        list = find_list_by_method(name)
        list || super
      end

      def respond_to_missing?(name, include_all = false)
        find_list_by_method(name) || super
      end

      def acts_as_listable_items
        @acts_as_listable_items ||= ActiveSupport::HashWithIndifferentAccess.new
      end

      private

      def add(list_class, item, labels)
        acts_as_listable_items[item] = list_class.new(self, item, labels)
      end

      def find_list_by_method(method)
        acts_as_listable_items.each do |item, list|
          return list if method.to_sym == item.to_sym
        end
        nil
      end
    end
  end
end

Version data entries

79 entries across 79 versions & 2 rubygems

Version Path
eac_ruby_utils-0.123.0 lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.121.0 lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.120.0 lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.76.1 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.76.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.75.2 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.119.2 lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.75.1 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.75.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.119.1 lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.74.1 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.74.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.73.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.119.0 lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.72.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.118.1 lib/eac_ruby_utils/listable/lists.rb
eac_ruby_utils-0.118.0 lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.70.1 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.70.0 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb
eac_tools-0.69.1 sub/eac_ruby_utils/lib/eac_ruby_utils/listable/lists.rb