module Tracksperanto::Casts
Helps to define things that will forcibly become floats, integers or strings
Public Class Methods
included(into)
click to toggle source
Calls superclass method
# File lib/tracksperanto/casts.rb, line 3 def self.included(into) into.extend(self) super end
Public Instance Methods
cast_to_bool(*attributes)
click to toggle source
# File lib/tracksperanto/casts.rb, line 32 def cast_to_bool(*attributes) attributes.each do | an_attr | define_method(an_attr) { !!instance_variable_get("@#{an_attr}") } define_method("#{an_attr}=") { |to| instance_variable_set("@#{an_attr}", !!to) } end end
cast_to_float(*attributes)
click to toggle source
Same as attr_accessor but will always convert to Float internally
# File lib/tracksperanto/casts.rb, line 9 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
cast_to_int(*attributes)
click to toggle source
Same as attr_accessor but will always convert to Integer/Bignum internally
# File lib/tracksperanto/casts.rb, line 17 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
cast_to_string(*attributes)
click to toggle source
Same as attr_accessor but will always convert to String internally
# File lib/tracksperanto/casts.rb, line 25 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