Class: StoreAsInt::Base
- Inherits:
-
Numeric
- Object
- Numeric
- StoreAsInt::Base
- Includes:
- Comparable
- Defined in:
- lib/store_as_int/base.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#num ⇒ Object
Attributes ============================================================.
Class Method Summary collapse
-
.===(other) ⇒ Object
Class Methods ========================================================.
- .accuracy ⇒ Object
- .base ⇒ Object
- .decimals ⇒ Object
- .matcher ⇒ Object
- .operators ⇒ Object
- .str_format ⇒ Object
- .sym ⇒ Object
Instance Method Summary collapse
-
#<=>(compare) ⇒ Object
Comparison Methods ===================================================.
- #==(compare) ⇒ Object
- #===(compare) ⇒ Object
- #accuracy ⇒ Object
- #as_json(*args) ⇒ Object
- #base ⇒ Object
- #base_float ⇒ Object
- #coerce(other_val) ⇒ Object
- #convert(other_val) ⇒ Object
- #decimals ⇒ Object
- #dup ⇒ Object
-
#duplicable? ⇒ Boolean
Boolean Methods ======================================================.
-
#initialize(new_val = nil, new_sym = nil) ⇒ Base
constructor
Instance Methods =====================================================.
- #inspect ⇒ Object
- #instance_of?(klass) ⇒ Boolean
- #is_a?(klass) ⇒ Boolean
- #is_an?(klass) ⇒ Boolean
- #kind_of?(klass) ⇒ Boolean
- #matcher ⇒ Object
- #method_missing(name, *args, &blk) ⇒ Object
- #negative_sign ⇒ Object
- #operators ⇒ Object
- #present? ⇒ Boolean
- #sym ⇒ Object
- #sym=(new_sym = nil) ⇒ Object
- #to_d ⇒ Object
- #to_f ⇒ Object
- #to_h ⇒ Object
- #to_i ⇒ Object
- #to_json(*args) ⇒ Object
- #to_s(w_sym = false) ⇒ Object
- #value ⇒ Object
Constructor Details
#initialize(new_val = nil, new_sym = nil) ⇒ Base
Instance Methods =====================================================
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
# File 'lib/store_as_int/base.rb', line 110 def initialize(new_val = nil, new_sym = nil) self.sym = new_sym if new_sym return self.num = nil unless new_val && (new_val != '') if new_val.is_a?(self.class) self.num = new_val.value elsif new_val.is_a?(Integer) self.num = new_val else if new_val.is_a?(String) begin new_val = new_val. gsub(/(,|\s)/, ''). match(self.class.matcher)[1..-1].join("") rescue NoMethodError return self.num = 0 end end self.num = (new_val.to_d * self.class.base).to_i end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &blk) ⇒ Object
175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/store_as_int/base.rb', line 175 def method_missing(name, *args, &blk) if self.operators[name.to_sym] if args[0].kind_of?(self.class) convert(value.__send__ name, args[0].value, *args[1..-1]) else convert(value.__send__(name, convert(args[0]), *args[1..-1])) end else ret = value.send(name, *args, &blk) ret.is_a?(Numeric) ? convert(ret) : ret end end |
Instance Attribute Details
#num ⇒ Object
Attributes ============================================================
14 15 16 |
# File 'lib/store_as_int/base.rb', line 14 def num @num end |
Class Method Details
.===(other) ⇒ Object
Class Methods ========================================================
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/store_as_int/base.rb', line 20 def self.===(other) !!( Numeric === other || ( other.is_a?(Class) ? other : other.class ) <= self ) || !!( other.instance_of?(String) && ( other.gsub(/(,|\s)/, '') =~ matcher ) ) end |
.accuracy ⇒ Object
40 41 42 |
# File 'lib/store_as_int/base.rb', line 40 def self.accuracy self::ACCURACY || 0 end |
.base ⇒ Object
44 45 46 |
# File 'lib/store_as_int/base.rb', line 44 def self.base 10 ** accuracy.to_i end |
.decimals ⇒ Object
48 49 50 |
# File 'lib/store_as_int/base.rb', line 48 def self.decimals self::DECIMALS end |
.matcher ⇒ Object
36 37 38 |
# File 'lib/store_as_int/base.rb', line 36 def self.matcher /^(\-|\+)?(?:#{sym.to_s.size > 0 ? Regexp.quote(sym.to_s) : '[^0-9]'}*)([0-9]+)(\.[0-9]+)?$/ end |
.operators ⇒ Object
60 61 62 |
# File 'lib/store_as_int/base.rb', line 60 def self.operators self::OPERATORS end |
.str_format ⇒ Object
56 57 58 |
# File 'lib/store_as_int/base.rb', line 56 def self.str_format self::STR_FORMAT end |
.sym ⇒ Object
52 53 54 |
# File 'lib/store_as_int/base.rb', line 52 def self.sym self::SYM end |
Instance Method Details
#<=>(compare) ⇒ Object
Comparison Methods ===================================================
97 98 99 |
# File 'lib/store_as_int/base.rb', line 97 def <=>(compare) value <=> convert(compare).value end |
#==(compare) ⇒ Object
101 102 103 |
# File 'lib/store_as_int/base.rb', line 101 def ==(compare) value == convert(compare).value end |
#===(compare) ⇒ Object
105 106 107 |
# File 'lib/store_as_int/base.rb', line 105 def ===(compare) self.== compare end |
#accuracy ⇒ Object
135 136 137 |
# File 'lib/store_as_int/base.rb', line 135 def accuracy @accuracy ||= self.class.accuracy || 0 end |
#as_json(*args) ⇒ Object
139 140 141 |
# File 'lib/store_as_int/base.rb', line 139 def as_json(*args) to_h end |
#base ⇒ Object
143 144 145 |
# File 'lib/store_as_int/base.rb', line 143 def base @base ||= 10 ** accuracy end |
#base_float ⇒ Object
147 148 149 |
# File 'lib/store_as_int/base.rb', line 147 def base_float base.to_f end |
#coerce(other_val) ⇒ Object
151 152 153 |
# File 'lib/store_as_int/base.rb', line 151 def coerce(other_val) [convert(other_val), self] end |
#convert(other_val) ⇒ Object
155 156 157 |
# File 'lib/store_as_int/base.rb', line 155 def convert(other_val) self.class.new(other_val, sym) end |
#decimals ⇒ Object
159 160 161 |
# File 'lib/store_as_int/base.rb', line 159 def decimals @decimals ||= self.class.decimals end |
#dup ⇒ Object
163 164 165 |
# File 'lib/store_as_int/base.rb', line 163 def dup self.class.new self.num end |
#duplicable? ⇒ Boolean
Boolean Methods ======================================================
65 66 67 |
# File 'lib/store_as_int/base.rb', line 65 def duplicable? true end |
#inspect ⇒ Object
167 168 169 |
# File 'lib/store_as_int/base.rb', line 167 def inspect to_s(true) end |
#instance_of?(klass) ⇒ Boolean
84 85 86 |
# File 'lib/store_as_int/base.rb', line 84 def instance_of?(klass) self.class == klass end |
#is_a?(klass) ⇒ Boolean
69 70 71 |
# File 'lib/store_as_int/base.rb', line 69 def is_a?(klass) kind_of?(klass) end |
#is_an?(klass) ⇒ Boolean
73 74 75 |
# File 'lib/store_as_int/base.rb', line 73 def is_an?(klass) kind_of?(klass) end |
#kind_of?(klass) ⇒ Boolean
77 78 79 80 81 82 |
# File 'lib/store_as_int/base.rb', line 77 def kind_of?(klass) value.kind_of?(klass) || self.class == klass || StoreAsInt == klass || super(klass) end |
#matcher ⇒ Object
171 172 173 |
# File 'lib/store_as_int/base.rb', line 171 def matcher @matcher ||= self.class.matcher end |
#negative_sign ⇒ Object
188 189 190 |
# File 'lib/store_as_int/base.rb', line 188 def negative_sign value < 0 ? '-' : '' end |
#operators ⇒ Object
192 193 194 |
# File 'lib/store_as_int/base.rb', line 192 def operators @operators ||= self.class.operators.dup end |
#present? ⇒ Boolean
88 89 90 91 92 93 94 |
# File 'lib/store_as_int/base.rb', line 88 def present? begin self.num.present? rescue NoMethodError !self.num.nil? && !(self.num.to_s == "") end end |
#sym ⇒ Object
197 198 199 |
# File 'lib/store_as_int/base.rb', line 197 def sym @sym ||= self.class.sym || '' end |
#sym=(new_sym = nil) ⇒ Object
201 202 203 |
# File 'lib/store_as_int/base.rb', line 201 def sym=(new_sym = nil) @sym = new_sym ? new_sym.to_s : self.class.sym end |
#to_d ⇒ Object
205 206 207 |
# File 'lib/store_as_int/base.rb', line 205 def to_d to_i.to_d/base end |
#to_f ⇒ Object
209 210 211 |
# File 'lib/store_as_int/base.rb', line 209 def to_f to_i/base_float end |
#to_h ⇒ Object
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/store_as_int/base.rb', line 213 def to_h { accuracy: accuracy, base: base, decimal: to_d, decimals: decimals, float: to_f, int: to_i, str: to_s, str_format: str_format, str_matcher: matcher, str_pretty: to_s(true), sym: sym, value: value, } end |
#to_i ⇒ Object
230 231 232 |
# File 'lib/store_as_int/base.rb', line 230 def to_i value.to_i end |
#to_json(*args) ⇒ Object
234 235 236 237 238 239 240 241 242 243 |
# File 'lib/store_as_int/base.rb', line 234 def to_json(*args) h = self.to_h h.delete(:str_format) begin h.to_json rescue NoMethodError require 'json' JSON.unparse(h) end end |
#to_s(w_sym = false) ⇒ Object
245 246 247 248 249 250 251 252 253 |
# File 'lib/store_as_int/base.rb', line 245 def to_s(w_sym = false) begin str_format.call(self, w_sym) rescue puts $!. puts $!.backtrace "" end end |
#value ⇒ Object
255 256 257 |
# File 'lib/store_as_int/base.rb', line 255 def value self.num || 0 end |