class StoreAsInt::Base
Attributes
Public Class Methods
===(other)
click to toggle source
accuracy()
click to toggle source
# File lib/store_as_int/base.rb, line 36 def self.accuracy self::ACCURACY || 0 end
base()
click to toggle source
# File lib/store_as_int/base.rb, line 40 def self.base 10 ** accuracy.to_i end
decimals()
click to toggle source
# File lib/store_as_int/base.rb, line 44 def self.decimals self::DECIMALS end
hash_without_keys(h, keys, indifferent = false)
click to toggle source
# File lib/store_as_int/base.rb, line 48 def self.hash_without_keys(h, keys, indifferent = false) n = h.dup if indifferent new_keys = keys.dup new_keys.each do |k| keys << k.to_sym keys << k.to_s end kys = keys.uniq end keys.each do |k| n.delete(k) end n end
json_create(o)
click to toggle source
# File lib/store_as_int/base.rb, line 64 def self.json_create(o) if o.is_a?(Hash) n = {} for k, v in o n[k.to_sym] = v end created = new((n[:value] || n[:int] || n[:decimal] || n[:float] || n[:str]), (n[:sym] || nil)) hash_without_keys(o, %w(decimal float int json_class str str_pretty value), true).each do |k, v| k = k.dup if created.respond_to?(k, true) || created.respond_to?(k.to_s.sub!('str_', '')) created.instance_variable_set("@#{k}", v) end end created else new(o) end end
matcher()
click to toggle source
# File lib/store_as_int/base.rb, line 84 def self.matcher /^(\-|\+)?(?:#{sym.to_s.size > 0 ? Regexp.quote(sym.to_s) : '[^0-9]'}*)([0-9]+)(\.[0-9]+)?$/ end
new(new_val = nil, new_sym = nil)
click to toggle source
Instance Methods =====================================================¶ ↑
# File lib/store_as_int/base.rb, line 146 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
operators()
click to toggle source
# File lib/store_as_int/base.rb, line 96 def self.operators self::OPERATORS end
str_format()
click to toggle source
# File lib/store_as_int/base.rb, line 92 def self.str_format self::STR_FORMAT end
sym()
click to toggle source
# File lib/store_as_int/base.rb, line 88 def self.sym self::SYM end
Public Instance Methods
<=>(compare)
click to toggle source
==(compare)
click to toggle source
# File lib/store_as_int/base.rb, line 137 def ==(compare) value == convert(compare).value end
===(compare)
click to toggle source
# File lib/store_as_int/base.rb, line 141 def ===(compare) self.== compare end
accuracy()
click to toggle source
# File lib/store_as_int/base.rb, line 171 def accuracy @accuracy ||= self.class.accuracy || 0 end
as_json(*args)
click to toggle source
# File lib/store_as_int/base.rb, line 175 def as_json(*args) to_h end
base()
click to toggle source
# File lib/store_as_int/base.rb, line 179 def base @base ||= 10 ** accuracy end
base_float()
click to toggle source
# File lib/store_as_int/base.rb, line 183 def base_float base.to_f end
coerce(other_val)
click to toggle source
# File lib/store_as_int/base.rb, line 187 def coerce(other_val) [convert(other_val), self] end
convert(other_val)
click to toggle source
# File lib/store_as_int/base.rb, line 191 def convert(other_val) self.class.new(other_val, sym) end
decimals()
click to toggle source
# File lib/store_as_int/base.rb, line 195 def decimals @decimals ||= self.class.decimals end
dup()
click to toggle source
# File lib/store_as_int/base.rb, line 199 def dup self.class.new self.num end
duplicable?()
click to toggle source
inspect()
click to toggle source
# File lib/store_as_int/base.rb, line 203 def inspect to_s(true) end
instance_of?(klass)
click to toggle source
# File lib/store_as_int/base.rb, line 120 def instance_of?(klass) self.class == klass end
is_a?(klass)
click to toggle source
# File lib/store_as_int/base.rb, line 105 def is_a?(klass) kind_of?(klass) end
is_an?(klass)
click to toggle source
# File lib/store_as_int/base.rb, line 109 def is_an?(klass) kind_of?(klass) end
kind_of?(klass)
click to toggle source
Calls superclass method
# File lib/store_as_int/base.rb, line 113 def kind_of?(klass) value.kind_of?(klass) || self.class == klass || StoreAsInt == klass || super(klass) end
matcher()
click to toggle source
# File lib/store_as_int/base.rb, line 207 def matcher @matcher ||= self.class.matcher end
method_missing(name, *args, &blk)
click to toggle source
# File lib/store_as_int/base.rb, line 211 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
negative_sign()
click to toggle source
# File lib/store_as_int/base.rb, line 224 def negative_sign value < 0 ? '-' : '' end
operators()
click to toggle source
# File lib/store_as_int/base.rb, line 228 def operators @operators ||= self.class.operators.dup end
present?()
click to toggle source
# File lib/store_as_int/base.rb, line 124 def present? begin self.num.present? rescue NoMethodError !self.num.nil? && !(self.num.to_s == "") end end
sym()
click to toggle source
# File lib/store_as_int/base.rb, line 232 def sym @sym ||= self.class.sym || '' end
sym=(new_sym = nil)
click to toggle source
# File lib/store_as_int/base.rb, line 236 def sym=(new_sym = nil) @sym = new_sym ? new_sym.to_s : self.class.sym end
to_d()
click to toggle source
# File lib/store_as_int/base.rb, line 240 def to_d to_i.to_d/base end
to_f()
click to toggle source
# File lib/store_as_int/base.rb, line 244 def to_f to_i/base_float end
to_h()
click to toggle source
# File lib/store_as_int/base.rb, line 248 def to_h { accuracy: accuracy, base: base, decimal: to_d, decimals: decimals, float: to_f, int: to_i, json_class: self.class, str: to_s, str_format: str_format, str_matcher: matcher, str_pretty: to_s(true), sym: sym, value: value, } end
to_i()
click to toggle source
# File lib/store_as_int/base.rb, line 266 def to_i value.to_i end
to_json(*args)
click to toggle source
# File lib/store_as_int/base.rb, line 270 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, padding: 0)
click to toggle source
# File lib/store_as_int/base.rb, line 281 def to_s(w_sym = false, padding: 0) begin str_format.call(self, w_sym, padding) rescue puts $!.message puts $!.backtrace "" end end
value()
click to toggle source
# File lib/store_as_int/base.rb, line 291 def value self.num || 0 end
Private Instance Methods
str_format()
click to toggle source
# File lib/store_as_int/base.rb, line 296 def str_format @str_format ||= self.class.str_format || ->(passed, w_sym, padding) do return '' unless w_sym || passed.present? || (padding.to_i > 0) prefix = "#{ passed.negative_sign }#{ w_sym ? passed.sym : '' }" str = "#{ passed.decimals ? sprintf("%0.0#{passed.decimals.to_i}f", passed.to_d.abs) : passed.to_i.to_s }".reverse.split('.') str[-1] = str[-1]. gsub(/(\d{3})(?=\d)/, w_sym ? '\\1,' : '\\1'). ljust(padding.to_i, ' ') "#{prefix}#{str.join('.').reverse}" end end