Class: StoreAsInt::Base

Inherits:
Numeric
  • Object
show all
Includes:
Comparable
Defined in:
lib/store_as_int/base.rb

Direct Known Subclasses

ExchangeRate, Money

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_val = nil, new_sym = nil) ⇒ Base

Instance Methods =====================================================



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &blk) ⇒ Object



211
212
213
214
215
216
217
218
219
220
221
222
# 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

Instance Attribute Details

#numObject

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

.accuracyObject



36
37
38
# File 'lib/store_as_int/base.rb', line 36

def self.accuracy
  self::ACCURACY || 0
end

.baseObject



40
41
42
# File 'lib/store_as_int/base.rb', line 40

def self.base
  10 ** accuracy.to_i
end

.decimalsObject



44
45
46
# File 'lib/store_as_int/base.rb', line 44

def self.decimals
  self::DECIMALS
end

.hash_without_keys(h, keys, indifferent = false) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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

.matcherObject



84
85
86
# 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

.operatorsObject



96
97
98
# File 'lib/store_as_int/base.rb', line 96

def self.operators
  self::OPERATORS
end

.str_formatObject



92
93
94
# File 'lib/store_as_int/base.rb', line 92

def self.str_format
  self::STR_FORMAT
end

.symObject



88
89
90
# File 'lib/store_as_int/base.rb', line 88

def self.sym
  self::SYM
end

Instance Method Details

#<=>(compare) ⇒ Object

Comparison Methods ===================================================



133
134
135
# File 'lib/store_as_int/base.rb', line 133

def <=>(compare)
  value <=> convert(compare).value
end

#==(compare) ⇒ Object



137
138
139
# File 'lib/store_as_int/base.rb', line 137

def ==(compare)
  value == convert(compare).value
end

#===(compare) ⇒ Object



141
142
143
# File 'lib/store_as_int/base.rb', line 141

def ===(compare)
  self.== compare
end

#accuracyObject



171
172
173
# File 'lib/store_as_int/base.rb', line 171

def accuracy
  @accuracy ||= self.class.accuracy || 0
end

#as_json(*args) ⇒ Object



175
176
177
# File 'lib/store_as_int/base.rb', line 175

def as_json(*args)
  to_h
end

#baseObject



179
180
181
# File 'lib/store_as_int/base.rb', line 179

def base
  @base ||= 10 ** accuracy
end

#base_floatObject



183
184
185
# File 'lib/store_as_int/base.rb', line 183

def base_float
  base.to_f
end

#coerce(other_val) ⇒ Object



187
188
189
# File 'lib/store_as_int/base.rb', line 187

def coerce(other_val)
  [convert(other_val), self]
end

#convert(other_val) ⇒ Object



191
192
193
# File 'lib/store_as_int/base.rb', line 191

def convert(other_val)
  self.class.new(other_val, sym)
end

#decimalsObject



195
196
197
# File 'lib/store_as_int/base.rb', line 195

def decimals
  @decimals ||= self.class.decimals
end

#dupObject



199
200
201
# File 'lib/store_as_int/base.rb', line 199

def dup
  self.class.new self.num
end

#duplicable?Boolean

Boolean Methods ======================================================

Returns:

  • (Boolean)


101
102
103
# File 'lib/store_as_int/base.rb', line 101

def duplicable?
  true
end

#inspectObject



203
204
205
# File 'lib/store_as_int/base.rb', line 203

def inspect
  to_s(true)
end

#instance_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/store_as_int/base.rb', line 120

def instance_of?(klass)
  self.class == klass
end

#is_a?(klass) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/store_as_int/base.rb', line 105

def is_a?(klass)
  kind_of?(klass)
end

#is_an?(klass) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/store_as_int/base.rb', line 109

def is_an?(klass)
  kind_of?(klass)
end

#kind_of?(klass) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
# 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

#matcherObject



207
208
209
# File 'lib/store_as_int/base.rb', line 207

def matcher
  @matcher ||= self.class.matcher
end

#negative_signObject



224
225
226
# File 'lib/store_as_int/base.rb', line 224

def negative_sign
  value < 0 ? '-' : ''
end

#operatorsObject



228
229
230
# File 'lib/store_as_int/base.rb', line 228

def operators
  @operators ||= self.class.operators.dup
end

#present?Boolean

Returns:

  • (Boolean)


124
125
126
127
128
129
130
# 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

#symObject



232
233
234
# File 'lib/store_as_int/base.rb', line 232

def sym
  @sym ||= self.class.sym || ''
end

#sym=(new_sym = nil) ⇒ Object



236
237
238
# 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_dObject



240
241
242
# File 'lib/store_as_int/base.rb', line 240

def to_d
  to_i.to_d/base
end

#to_fObject



244
245
246
# File 'lib/store_as_int/base.rb', line 244

def to_f
  to_i/base_float
end

#to_hObject



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
# 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_iObject



266
267
268
# File 'lib/store_as_int/base.rb', line 266

def to_i
  value.to_i
end

#to_json(*args) ⇒ Object



270
271
272
273
274
275
276
277
278
279
# 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) ⇒ Object



281
282
283
284
285
286
287
288
289
# 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

#valueObject



291
292
293
# File 'lib/store_as_int/base.rb', line 291

def value
  self.num || 0
end