100% free for Open Source, forever. Get another set of eyes on your code.

Take a look. Our new design is currently in beta, but you can start using it today!

A
 

Echowrap::Base

Updated more than 3 months ago.
  • Complex method .attr_reader (complexity = 29)

        def self.attr_reader(*attrs)
          #@attr_readers ||= []
          #@attr_readers.concat attrs
    
          mod = Module.new do
    
    View more
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
77
Complexity
0
Duplication
88
Lines
8
Methods
9.7
Complexity / M
0
Churn
44
Lines of Code
6
LOC / Method