Sha256: d2d47887a244ecd365df916bee14b6f180d2e3aaa2e03537d00b6c71b269df15

Contents?: true

Size: 1.5 KB

Versions: 3

Compression:

Stored size: 1.5 KB

Contents

# encoding: UTF-8

module BEncodr
  module Integer
    module Generic
      module InstanceMethods
        # Encodes object into a bencoded integer. BEncoded strings are length-prefixed base ten followed by a colon and
        # the string. Object must implement to_i or to_int.
        #
        #   1.bencodr #=> "i1e"
        #
        # @return [::String] the bencoded integer
        def bencode
          (respond_to?(:to_i) ? to_i : to_int).bencode
        end
      end
    end

    # Registers a class as an object that can be converted into a bencoded integer. Class must have instance method to_i
    # or to_int.
    #
    #   class MyClass
    #     def to_i
    #       1
    #     end
    #   end
    #
    #   BEncodr::Integer.register MyClass
    #   my_class = MyClass.new
    #   my_class.bencodr  #=> "i1e"
    #
    # @param [Class#to_i, Class#to_int] type the class to add the bencodr instance method to
    def self.register(type)
      type.send :include, Generic::InstanceMethods
    end

    register Numeric
    register Time

    module Integer
      module InstanceMethods
        # Encodes an integer into a bencoded integer. Bencoded integers are represented by an 'i' followed by the number
        # in base 10 followed by an 'e'.
        #
        #   3.bencodr #=> "i3e"
        #   -3.bencodr #=> "i-3e"
        #
        # @return [::String] the bencoded integer
        def bencode
          [:i, self, :e].join
        end
      end

      ::Integer.send :include, InstanceMethods
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
bencodr-1.2.0 lib/bencodr/integer.rb
bencodr-1.1.0 lib/bencodr/integer.rb
bencodr-1.0.1 lib/bencodr/integer.rb