timcase/echonest

Echowrap::Base
A

Complex method in .attr_reader

    def self.attr_reader(*attrs)
      #@attr_readers ||= []
      #@attr_readers.concat attrs

      mod = Module.new do
Found in Echowrap::Base - About 2 hrs to fix

Long or complex methods can make code harder to understand. In most circumstances, methods are best as a small chunk of code (the "how") with a clear, understandable name (the "what"). Long methods can also lead to duplication, as it's harder to reuse logic that is tightly coupled to surrounding code.

Refactorings

Read More

There are no issues that match your filters.

module Echowrap
  class Base
    # Define methods that retrieve the value from an initialized instance variable Hash, using the attribute as a key
    #
    # @param attrs [Array, Set, Symbol]
    def self.attr_reader(*attrs)
      #@attr_readers ||= []
      #@attr_readers.concat attrs

      mod = Module.new do
        attrs.each do |attribute|
          define_method attribute do
            @attrs[attribute.to_sym] if @attrs
          end
          define_method "#{attribute}?" do
            !!@attrs[attribute.to_sym]
          end
        end
      end
      const_set(:Attributes, mod)
      include mod
    end

    # @param other [Echowrap::Base]
    # @return [Boolean]
    def ==(other)
      super || attr_equal(:id, other) || attrs_equal(other)
    end

    # Initializes a new object
    #
    # @param attrs [Hash]
    # @return [Echowrap::Base]
    def initialize(attrs={})
      @attrs = attrs
    end

    # Retrieve the attributes of an object
    #
    # @return [Hash]
    def attrs
      @attrs
    end
    alias to_hash attrs

    # Update the attributes of an object
    #
    # @param attrs [Hash]
    # @return [Echowrap::Base]
    def update(attrs)
      @attrs.update(attrs)
      self
    end

    #def self.attr_readers
      #@attr_readers
    #end

    #def attr_readers
      #self.class.attr_readers
    #end

  protected

    # @param attr [Symbol]
    # @param other [Echowrap::Base]
    # @return [Boolean]
    def attr_equal(attr, other)
      self.class == other.class && !other.send(attr).nil? && send(attr) == other.send(attr)
    end

    # @param other [Echowrap::Base]
    # @return [Boolean]
    def attrs_equal(other)
      self.class == other.class && !other.attrs.empty? && attrs == other.attrs
    end

    # @param klass [Class]
    # @param key [Symbol]
    # @return [Array]
    def map_collection(klass, key)
      Array(@attrs[key.to_sym]).map do |entity|
        klass.new(entity)
      end
    end

  end
end

Size

Lines of code
44