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
Class Method Summary collapse
Instance Method Summary collapse
- #cast_to_bool(*attributes) ⇒ Object
-
#cast_to_float(*attributes) ⇒ Object
Same as attr_accessor but will always convert to Float internally.
-
#cast_to_int(*attributes) ⇒ Object
Same as attr_accessor but will always convert to Integer/Bignum internally.
-
#cast_to_string(*attributes) ⇒ Object
Same as attr_accessor but will always convert to String internally.
Class Method Details
.included(into) ⇒ Object
4 5 6 7 |
# File 'lib/tracksperanto/casts.rb', line 4 def self.included(into) into.extend(self) super end |
Instance Method Details
#cast_to_bool(*attributes) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/tracksperanto/casts.rb', line 33 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
10 11 12 13 14 15 |
# File 'lib/tracksperanto/casts.rb', line 10 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
18 19 20 21 22 23 |
# File 'lib/tracksperanto/casts.rb', line 18 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
26 27 28 29 30 31 |
# File 'lib/tracksperanto/casts.rb', line 26 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 |