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) ⇒ 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
# File 'lib/store_as_int/base.rb', line 110

def initialize(new_val = nil)
  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



173
174
175
176
177
178
179
180
# File 'lib/store_as_int/base.rb', line 173

def method_missing(name, *args, &blk)
  if self.operators[name.to_sym]
    self.class.new(value.__send__(name, convert(*args).value))
  else
    ret = value.send(name, *args, &blk)
    ret.is_a?(Numeric) ? self.class.new(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



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

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

.baseObject



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

def self.base
  10 ** accuracy.to_i
end

.decimalsObject



48
49
50
# File 'lib/store_as_int/base.rb', line 48

def self.decimals
  self::DECIMALS
end

.matcherObject



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

.operatorsObject



60
61
62
# File 'lib/store_as_int/base.rb', line 60

def self.operators
  self::OPERATORS
end

.str_formatObject



56
57
58
# File 'lib/store_as_int/base.rb', line 56

def self.str_format
  self::STR_FORMAT
end

.symObject



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

#accuracyObject



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

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

#as_json(*args) ⇒ Object



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

def as_json(*args)
  to_h
end

#baseObject



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

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

#base_floatObject



145
146
147
# File 'lib/store_as_int/base.rb', line 145

def base_float
  base.to_f
end

#coerce(other_val) ⇒ Object



149
150
151
# File 'lib/store_as_int/base.rb', line 149

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

#convert(other_val) ⇒ Object



153
154
155
# File 'lib/store_as_int/base.rb', line 153

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

#decimalsObject



157
158
159
# File 'lib/store_as_int/base.rb', line 157

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

#dupObject



161
162
163
# File 'lib/store_as_int/base.rb', line 161

def dup
  self.class.new self.num
end

#duplicable?Boolean

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

Returns:

  • (Boolean)


65
66
67
# File 'lib/store_as_int/base.rb', line 65

def duplicable?
  true
end

#inspectObject



165
166
167
# File 'lib/store_as_int/base.rb', line 165

def inspect
  to_s(true)
end

#instance_of?(klass) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

#matcherObject



169
170
171
# File 'lib/store_as_int/base.rb', line 169

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

#negative_signObject



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

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

#operatorsObject



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

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

#present?Boolean

Returns:

  • (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

#symObject



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

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

#sym=(new_sym) ⇒ Object



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

def sym=(new_sym)
  @sym = new_sym
end

#to_dObject



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

def to_d
  to_i.to_d/base
end

#to_fObject



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

def to_f
  to_i/base_float
end

#to_hObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/store_as_int/base.rb', line 207

def to_h
  {
    accuracy: accuracy,
    base: base,
    decimal: to_d,
    decimals: decimals,
    float: to_f,
    str: to_s,
    str_format: str_format,
    str_matcher: matcher,
    str_pretty: to_s(true),
    sym: sym,
    value: value,
  }
end

#to_iObject



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

def to_i
  value
end

#to_json(*args) ⇒ Object



227
228
229
230
231
232
233
234
235
236
# File 'lib/store_as_int/base.rb', line 227

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



238
239
240
241
242
243
244
245
246
# File 'lib/store_as_int/base.rb', line 238

def to_s(w_sym = false)
  begin
    str_format.call(self, w_sym)
  rescue
    puts $!.message
    puts $!.backtrace
    ""
  end
end

#valueObject



248
249
250
# File 'lib/store_as_int/base.rb', line 248

def value
  self.num || 0
end