Sha256: 417fdbd69f93805aae1be32d6a939fdeb0a46c94dab10dd418a9f811ca6aebfe

Contents?: true

Size: 1.12 KB

Versions: 71

Compression:

Stored size: 1.12 KB

Contents

# Helps to define things that will forcibly become floats, integers or strings
module Tracksperanto::Casts
  def self.included(into)
    into.extend(self)
    super
  end
  
  # Same as attr_accessor but will always convert to Float internally
  def cast_to_float(*attributes)
    attributes.each do | an_attr |
      define_method(an_attr) { instance_variable_get("@#{an_attr}").to_f }
      define_method("#{an_attr}=") { |to| instance_variable_set("@#{an_attr}", to.to_f) }
    end
  end
  
  # Same as attr_accessor but will always convert to Integer/Bignum internally
  def cast_to_int(*attributes)
    attributes.each do | an_attr |
      define_method(an_attr) { instance_variable_get("@#{an_attr}").to_i }
      define_method("#{an_attr}=") { |to| instance_variable_set("@#{an_attr}", to.to_i) }
    end
  end
  
  # Same as attr_accessor but will always convert to String internally
  def cast_to_string(*attributes)
    attributes.each do | an_attr |
      define_method(an_attr) { instance_variable_get("@#{an_attr}").to_s }
      define_method("#{an_attr}=") { |to| instance_variable_set("@#{an_attr}", to.to_s) }
    end
  end
end

Version data entries

71 entries across 71 versions & 1 rubygems

Version Path
tracksperanto-1.7.3 lib/tracksperanto/casts.rb
tracksperanto-1.7.2 lib/tracksperanto/casts.rb
tracksperanto-1.7.1 lib/tracksperanto/casts.rb
tracksperanto-1.7.0 lib/tracksperanto/casts.rb
tracksperanto-1.6.9 lib/tracksperanto/casts.rb
tracksperanto-1.6.8 lib/tracksperanto/casts.rb
tracksperanto-1.6.7 lib/tracksperanto/casts.rb
tracksperanto-1.6.6 lib/tracksperanto/casts.rb
tracksperanto-1.6.5 lib/tracksperanto/casts.rb
tracksperanto-1.6.4 lib/tracksperanto/casts.rb
tracksperanto-1.6.3 lib/tracksperanto/casts.rb
tracksperanto-1.6.2 lib/tracksperanto/casts.rb
tracksperanto-1.6.1 lib/tracksperanto/casts.rb
tracksperanto-1.6.0 lib/tracksperanto/casts.rb
tracksperanto-1.5.7 lib/tracksperanto/casts.rb
tracksperanto-1.5.6 lib/tracksperanto/casts.rb
tracksperanto-1.5.5 lib/tracksperanto/casts.rb
tracksperanto-1.5.4 lib/tracksperanto/casts.rb
tracksperanto-1.5.3 lib/tracksperanto/casts.rb
tracksperanto-1.5.2 lib/tracksperanto/casts.rb