Skip to content

Commit

Permalink
Merge branch 'relistan/refactor-pr-112'
Browse files Browse the repository at this point in the history
  • Loading branch information
relistan committed May 9, 2015
2 parents 691f308 + c21c3e9 commit f2113e2
Showing 1 changed file with 59 additions and 57 deletions.
116 changes: 59 additions & 57 deletions lib/core_ext/numeric_bytes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copied from ActiveSupport 4.2.1 lib/active_support/core_ext/numeric/bytes.rb
# Modified from ActiveSupport 4.2.1 lib/active_support/core_ext/numeric/bytes.rb
#
# NOTE that THIS LICENSE ONLY APPLIES TO THIS FILE itself, not
# to the rest of the project.
Expand All @@ -25,68 +25,70 @@
# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
unless Numeric.constants.include?(:KILOBYTE)
class Numeric
KILOBYTE = 1024 unless defined? KILOBYTE
MEGABYTE = KILOBYTE * 1024 unless defined? MEGABYTE
GIGABYTE = MEGABYTE * 1024 unless defined? GIGABYTE
TERABYTE = GIGABYTE * 1024 unless defined? TERABYTE
PETABYTE = TERABYTE * 1024 unless defined? PETABYTE
EXABYTE = PETABYTE * 1024 unless defined? EXABYTE

class Numeric
KILOBYTE = 1024
MEGABYTE = KILOBYTE * 1024
GIGABYTE = MEGABYTE * 1024
TERABYTE = GIGABYTE * 1024
PETABYTE = TERABYTE * 1024
EXABYTE = PETABYTE * 1024
# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
#
# 2.bytes # => 2
def bytes
self
end unless method_defined? :bytes
alias :byte :bytes unless method_defined? :byte

# Enables the use of byte calculations and declarations, like 45.bytes + 2.6.megabytes
#
# 2.bytes # => 2
def bytes
self
end
alias :byte :bytes

# Returns the number of bytes equivalent to the kilobytes provided.
#
# 2.kilobytes # => 2048
def kilobytes
self * KILOBYTE
end
alias :kilobyte :kilobytes
# Returns the number of bytes equivalent to the kilobytes provided.
#
# 2.kilobytes # => 2048
def kilobytes
self * KILOBYTE
end unless method_defined? :kilobytes
alias :kilobyte :kilobytes unless method_defined? :kilobyte

# Returns the number of bytes equivalent to the megabytes provided.
#
# 2.megabytes # => 2_097_152
def megabytes
self * MEGABYTE
end
alias :megabyte :megabytes
# Returns the number of bytes equivalent to the megabytes provided.
#
# 2.megabytes # => 2_097_152
def megabytes
self * MEGABYTE
end unless method_defined? :megabytes?
alias :megabyte :megabytes unless method_defined? :megabyte

# Returns the number of bytes equivalent to the gigabytes provided.
#
# 2.gigabytes # => 2_147_483_648
def gigabytes
self * GIGABYTE
end
alias :gigabyte :gigabytes
# Returns the number of bytes equivalent to the gigabytes provided.
#
# 2.gigabytes # => 2_147_483_648
def gigabytes
self * GIGABYTE
end unless method_defined? :gigabytes
alias :gigabyte :gigabytes unless method_defined? :gigabyte

# Returns the number of bytes equivalent to the terabytes provided.
#
# 2.terabytes # => 2_199_023_255_552
def terabytes
self * TERABYTE
end
alias :terabyte :terabytes
# Returns the number of bytes equivalent to the terabytes provided.
#
# 2.terabytes # => 2_199_023_255_552
def terabytes
self * TERABYTE
end unless method_defined? :terabytes
alias :terabyte :terabytes unless method_defined? :terabyte

# Returns the number of bytes equivalent to the petabytes provided.
#
# 2.petabytes # => 2_251_799_813_685_248
def petabytes
self * PETABYTE
end
alias :petabyte :petabytes
# Returns the number of bytes equivalent to the petabytes provided.
#
# 2.petabytes # => 2_251_799_813_685_248
def petabytes
self * PETABYTE
end unless method_defined? :petabytes
alias :petabyte :petabytes unless method_defined? :petabyte

# Returns the number of bytes equivalent to the exabytes provided.
#
# 2.exabytes # => 2_305_843_009_213_693_952
def exabytes
self * EXABYTE
# Returns the number of bytes equivalent to the exabytes provided.
#
# 2.exabytes # => 2_305_843_009_213_693_952
def exabytes
self * EXABYTE
end unless method_defined? :exabytes
alias :exabyte :exabytes unless method_defined? :exabyte
end
alias :exabyte :exabytes
end

0 comments on commit f2113e2

Please sign in to comment.