Module: Tracksperanto::Casts

Included in:
Import::Base, Keyframe, Tool::Base, Tracker
Defined in:
lib/tracksperanto/casts.rb

Overview

Helps to define things that will forcibly become floats, integers or strings

Instance Method Summary collapse

Instance Method Details

#cast_to_bool(*attributes) ⇒ Object



32
33
34
35
36
37
# 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) ⇒ Object

Same as attr_accessor but will always convert to Float internally



9
10
11
12
13
14
# 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) ⇒ Object

Same as attr_accessor but will always convert to Integer/Bignum internally



17
18
19
20
21
22
# 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) ⇒ Object

Same as attr_accessor but will always convert to String internally



25
26
27
28
29
30
# 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