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 =====================================================



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/store_as_int/base.rb', line 99

def initialize(new_val = nil)
  return self.num = nil unless 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(/(\-|\+)?#{Regexp.quote("\\#{sym}" || '[^0-9]')}?([0-9]+)(\.[0-9]+)?$/)[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



154
155
156
157
158
159
160
161
# File 'lib/store_as_int/base.rb', line 154

def method_missing(name, *args, &blk)
  if self.class.operators[name.to_sym]
    self.class.new(value.__send__(name, self.class.new(*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)/, '') =~ /^(\-|\+)?#{Regexp.quote("\\#{sym}" || '[^0-9]')}?([0-9]+)(\.[0-9]+)?$/
    )
  )
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

.operatorsObject



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

def self.operators
  self::OPERATORS
end

.str_formatObject



52
53
54
# File 'lib/store_as_int/base.rb', line 52

def self.str_format
  self::STR_FORMAT
end

.symObject



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

def self.sym
  self::SYM
end

Instance Method Details

#<=>(compare) ⇒ Object

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



86
87
88
# File 'lib/store_as_int/base.rb', line 86

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

#==(compare) ⇒ Object



90
91
92
# File 'lib/store_as_int/base.rb', line 90

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

#===(compare) ⇒ Object



94
95
96
# File 'lib/store_as_int/base.rb', line 94

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

#accuracyObject



122
123
124
# File 'lib/store_as_int/base.rb', line 122

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

#as_json(*args) ⇒ Object



126
127
128
# File 'lib/store_as_int/base.rb', line 126

def as_json(*args)
  self.num.as_json(*args)
end

#baseObject



130
131
132
# File 'lib/store_as_int/base.rb', line 130

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

#base_floatObject



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

def base_float
  base.to_f
end

#coerce(other_val) ⇒ Object



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

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

#convert(other_val) ⇒ Object



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

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

#decimalsObject



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

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

#inspectObject



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

def inspect
  to_s(true)
end

#instance_of?(klass) ⇒ Boolean



73
74
75
# File 'lib/store_as_int/base.rb', line 73

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

#is_a?(klass) ⇒ Boolean

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



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

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

#is_an?(klass) ⇒ Boolean



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

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

#kind_of?(klass) ⇒ Boolean



69
70
71
# File 'lib/store_as_int/base.rb', line 69

def kind_of?(klass)
  value.kind_of?(klass) || self.class == klass || super(klass)
end

#negative_signObject



163
164
165
# File 'lib/store_as_int/base.rb', line 163

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

#present?Boolean



77
78
79
80
81
82
83
# File 'lib/store_as_int/base.rb', line 77

def present?
  begin
    self.num.present?
  rescue NoMethodError
    !self.num.nil? && !(self.num.to_s == "")
  end
end

#symObject



167
168
169
# File 'lib/store_as_int/base.rb', line 167

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

#sym=(new_sym) ⇒ Object



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

def sym=(new_sym)
  @sym = new_sym
end

#to_dObject



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

def to_d
  to_i.to_d/base
end

#to_fObject



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

def to_f
  to_i/base_float
end

#to_iObject



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

def to_i
  self.num.to_i
end

#to_s(w_sym = false) ⇒ Object



187
188
189
190
191
192
193
194
195
# File 'lib/store_as_int/base.rb', line 187

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

#valueObject



197
198
199
# File 'lib/store_as_int/base.rb', line 197

def value
  self.num || 0
end