Sha256: 4bb5ec8ab7aa742bae5b7b691b8ad81ab013c9b64846a63ed145e05cc3c3fa4c

Contents?: true

Size: 1.32 KB

Versions: 1

Compression:

Stored size: 1.32 KB

Contents

class Protod
  class RubyIdent
    include ActiveModel::Model
    include ActiveModel::Attributes

    attribute :const_name, :string
    attribute :method_name, :string
    attribute :singleton, :boolean, default: false

    class << self
      def build_from(string)
        return if string.blank?

        string = string.gsub('__', '::')

        const_name, method_name, singleton = case
                                             when string.include?('.') 
                                               [*string.split('.'), true]
                                             when string.include?('#')
                                               [*string.split('#'), false]
                                             else
                                               [string, nil, false]
                                             end

        return unless const_name.safe_constantize

        new(const_name: const_name, method_name: method_name, singleton: singleton)
      end

      def absolute_of(ruby_ident)
        return if ruby_ident.blank?

        "::#{ruby_ident.to_s.delete_prefix('::')}"
      end
    end

    def const_name
      self.class.absolute_of(super)
    end

    def ==(other)
      to_s == other.to_s
    end

    def to_s
      [const_name, method_name].compact.join(singleton ? '.' : '#')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
protod-0.1.0 lib/protod/ruby_ident.rb