simple_model 1.2.22 → 1.2.23

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YjQyN2Y0MzE5YjZkODA2MGIwOGYyNzRiNzk5NmUyMjYzYmEwNTExZg==
4
+ MTk1Y2IxMGU5Y2YxYzNhM2ZiZmIzNjVlN2E5MWUxMmQ1ZDkxNWNjYw==
5
5
  data.tar.gz: !binary |-
6
- YzVjNGU4MGI2ZDFlYmE5M2ExODYyZWZkODUwYTZlMzhhMGU4YWE5NQ==
6
+ ZGU2ZjIxNGFkYzVlZTM3YjZjZDI2ODYyZmQzYTYwNzg0NmE0Mzk5MQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- MjAyM2JkMDRhMjdkMjc4NzRiMjE1OTI5MDlmNTJjMGMyNDQwM2E3ZjdhYTcz
10
- YWQwY2RhMmVmNDliYzY0ZmQ5NjhlM2Y1MWVlMzljODQ4MWU5ZTc0OWRiZTgw
11
- MTcwNmNmYjA5NWYyNzhmMjMwZDU0YTkzNGIzMmNlZGRkODU3ZTE=
9
+ Mjg4NzY3ZTViMjcwNjJjNWJhZDkxZmQ4YzViODdiNjVkMTYzZTQxZTU5YmRm
10
+ NDI1MGM2NGRiYTk4ZGQzMDRkYmI3MTg5YmYxMTlhMGI0ZjlkMjMzMzRkZTc1
11
+ YjU0ODM4OWQzN2U1OGI1ODBjYzQ3NGNiMTk3YzQ3NDlhNDVmNTk=
12
12
  data.tar.gz: !binary |-
13
- YWQ2M2MzYTRmNjI5NmQwZWFmNTQ0ODAzZjM1NDdkNGEwNThhMWFjMjRiMzBk
14
- ZTA5ZDhlZjVjNDExYWY0ZTIwMDdmNTVjZDdjOTIzZTZkMmJmMjVkNGRkMjYz
15
- ZjYzY2QyYTI1NDg2OWEyODIzMDY3MGI4OTQzOTYwNzEyZWFiZWY=
13
+ MzE0YzVkOTZkMmFhNTM1OTUyMGFiM2UyOTU5OWIxYWRkZDVjYzgwY2Y2ZWQ3
14
+ YjA3YmY0NWQ0MDA4MTJkZDI4MmVjZTBhODE1OWJlYTk4MjFmOWRjNzQzMzJm
15
+ YWNlZjE4MDJhYmViOTNjN2VjOTNmZDM5ZDMzZmQ3MTFjNTQwZmU=
data/LICENSE.txt CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010-2013 Joshua T. McKinney
1
+ Copyright (c) 2010-2014 Joshua T. McKinney
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -13,12 +13,13 @@ Benchmark.bm do |b|
13
13
  BenchClass.new()
14
14
  end
15
15
  end
16
- BenchClass.new()
16
+
17
17
  b.report("initialize with attrs") do
18
18
  30000.times.each do
19
19
  BenchClass.new(:num => 1, :dec => "12.4")
20
20
  end
21
21
  end
22
+
22
23
  b.report("get") do
23
24
  30000.times.each do
24
25
  klass = BenchClass.new
data/lib/simple_model.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2010-2012 Joshua Thomas Mckinney
2
+ # Copyright (c) 2010-2014 Joshua Thomas Mckinney
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -7,7 +7,7 @@ module SimpleModel
7
7
 
8
8
  def initialize(*attrs)
9
9
  attrs = attrs.extract_options!
10
- attrs = attributes_with_for_init(attrs)
10
+ attrs = attributes_for_init(attrs)
11
11
  attrs = self.class.before_initialize.call(self,attrs) if self.class.before_initialize
12
12
  set(attrs)
13
13
  self.class.after_initialize.call(self) if self.class.after_initialize
@@ -19,11 +19,11 @@ module SimpleModel
19
19
 
20
20
  # Returns true if attribute has been initialized
21
21
  def initialized?(attr)
22
- self.attributes.key?(attr.to_sym)
22
+ attributes.key?(attr)
23
23
  end
24
24
 
25
25
  def get(attr)
26
- self.send(attr)
26
+ send(attr)
27
27
  end
28
28
  alias :read :get
29
29
 
@@ -31,7 +31,7 @@ module SimpleModel
31
31
  # set(:foo => "bar", :dime => 0.1)
32
32
  def set(*attrs)
33
33
  attrs.extract_options!.each do |attr,val|
34
- self.send("#{attr.to_s}=",val)
34
+ send("#{attr}=",val)
35
35
  end
36
36
  end
37
37
  alias :set_attributes :set
@@ -41,17 +41,17 @@ module SimpleModel
41
41
  if allow_attribute_action?(val,options)
42
42
  val = fetch_default_value(options[:default]) if (!options[:allow_blank] && options.key?(:default) && val.blank?)
43
43
  val = options[:on_set].call(self,val) if options[:on_set] #(!options.key?(:on_set) || (val.blank? && !options[:allow_blank]) )
44
- self.send("#{attr}_will_change!") if (initialized?(attr) && val != self.attributes[attr])
45
- self.attributes[attr] = val
44
+ send("#{attr}_will_change!") if (initialized?(attr) && val != attributes[attr])
45
+ attributes[attr] = val
46
46
  options[:after_set].call(self,val) if options[:after_set]
47
47
  end
48
48
  end
49
49
 
50
50
  def get_attribute(attr)
51
- val = self.attributes[attr]
51
+ val = attributes[attr]
52
52
  options = self.class.defined_attributes[attr] || {}
53
- if (options.key?(:default) && (!self.initialized?(attr) || (!options[:allow_blank] && val.blank?)))
54
- val = self.attributes[attr] = fetch_default_value(options[:default])
53
+ if (options.key?(:default) && (!initialized?(attr) || (!options[:allow_blank] && val.blank?)))
54
+ val = attributes[attr] = fetch_default_value(options[:default])
55
55
  end
56
56
  if options[:on_get]
57
57
  options[:on_get].call(self,val)
@@ -77,18 +77,17 @@ module SimpleModel
77
77
  end
78
78
 
79
79
  def fetch_default_value(arg)
80
- return self.send(arg) if (arg.is_a?(Symbol) && self.respond_to?(arg))
80
+ return send(arg) if (arg.is_a?(Symbol) && self.respond_to?(arg))
81
81
  arg
82
82
  end
83
83
 
84
84
  # Returns attribute that have defaults in a hash: {:attribute => "default value"}
85
85
  # Checks for alias attributes to ensure they are not overwritten
86
- def attributes_with_for_init(attrs)
87
- d = attrs.with_indifferent_access
86
+ def attributes_for_init(attrs)
87
+ d = (attrs.is_a?(HashWithIndifferentAccess) ? attrs : attrs.with_indifferent_access )
88
88
  self.class.defined_attributes.each do |k,v|
89
- key = k.to_sym
90
- if allow_init_default?(d,key,v)
91
- d[key] = fetch_default_value(v[:default])
89
+ if allow_init_default?(d,k,v)
90
+ d[k] = fetch_default_value(v[:default])
92
91
  end
93
92
  end
94
93
  d
@@ -101,7 +100,7 @@ module SimpleModel
101
100
  end
102
101
 
103
102
  def attributes_have_alias?(attrs,attr)
104
- !(self.class.alias_attributes.select{ |a, m| (m == attr && attrs.key?(a.to_sym)) }).empty?
103
+ !(self.class.alias_attributes.select{ |a, m| (m == attr.to_sym && attrs.key?(a)) }).empty?
105
104
  end
106
105
 
107
106
  def allow_attribute_action?(val,options)
@@ -131,8 +130,8 @@ module SimpleModel
131
130
  end
132
131
 
133
132
  # Rails 3.2 + required when searching for attributes in from inherited classes/models
134
- def attribute(name)
135
- attributes[name.to_sym]
133
+ def attribute(attr)
134
+ get_attribute(attr)
136
135
  end
137
136
 
138
137
  module ClassMethods
@@ -177,7 +176,7 @@ module SimpleModel
177
176
  def new_with_store(session_hash)
178
177
  nw = self.new()
179
178
  nw.attributes = session_hash
180
- nw.set(nw.send(:attributes_with_for_init,session_hash))
179
+ nw.set(nw.send(:attributes_for_init,session_hash))
181
180
  nw
182
181
  end
183
182
 
@@ -198,7 +197,7 @@ module SimpleModel
198
197
  end
199
198
 
200
199
  def attribute_defined?(attr)
201
- (self.defined_attributes.member?(attr) || self.superclass.respond_to?(:attribute_defined?) && self.superclass.attribute_defined?(attr))
200
+ defined_attributes.key?(attr)
202
201
  end
203
202
 
204
203
  # The default settings for a SimpeModel class
@@ -221,15 +220,15 @@ module SimpleModel
221
220
  # at once, so we must set @attribute_methods_generated to nil to allow the
222
221
  # re-run to occur ONLY IN RAILS 3.0.
223
222
  def add_defined_attribute(attr,options)
224
- self.defined_attributes[attr] = options
223
+ defined_attributes[attr] = options
225
224
  @attribute_methods_generated = nil #if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
226
225
  define_attribute_methods(defined_attributes_keys)
227
226
  end
228
227
 
229
228
  # We don't want to call define_attribute_methods on methods defined in the parent class
230
229
  def defined_attributes_keys
231
- dak = self.defined_attributes.keys
232
- dak = dak - self.superclass.defined_attributes.keys if self.superclass.respond_to?(:defined_attributes)
230
+ dak = defined_attributes.keys
231
+ dak = dak - superclass.defined_attributes.keys if superclass.respond_to?(:defined_attributes)
233
232
  dak
234
233
  end
235
234
 
@@ -276,13 +275,13 @@ module SimpleModel
276
275
  alias_attributes[new_alias] = attr
277
276
 
278
277
  define_method(new_alias) do
279
- self.send(attr)
278
+ send(attr)
280
279
  end
281
280
  define_method("#{new_alias}?") do
282
- self.send("#{attr}?")
281
+ send("#{attr}?")
283
282
  end
284
283
  define_method("#{new_alias.to_s}=") do |*args, &block|
285
- self.send("#{attr.to_s}=",*args, &block)
284
+ send("#{attr}=",*args, &block)
286
285
  end
287
286
  end
288
287
 
@@ -299,7 +298,7 @@ module SimpleModel
299
298
  # should return a hash to be set
300
299
  # EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}
301
300
  def before_initialize=before_initialize
302
- raise TypeError "before_initialize must be a lambda that accepts the attirbutes to be initialize" unless before_initialize.is_a?(Proc)
301
+ raise TypeError "before_initialize must be a lambda that accepts the attributes to be initialize" unless before_initialize.is_a?(Proc)
303
302
  @before_initialize = before_initialize
304
303
  end
305
304
 
@@ -322,8 +321,8 @@ module SimpleModel
322
321
  # hack to keep things working when a class inherits from a super that
323
322
  # has ActiveModel::Dirty included
324
323
  def inherited(base)
325
- base.defined_attributes = self.defined_attributes.merge(base.defined_attributes)
326
- base.alias_attributes = self.alias_attributes.merge(base.alias_attributes)
324
+ base.defined_attributes = defined_attributes.merge(base.defined_attributes)
325
+ base.alias_attributes = alias_attributes.merge(base.alias_attributes)
327
326
  super
328
327
  # Rails 3.0 Hack
329
328
  if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
@@ -1,167 +1,166 @@
1
1
  module SimpleModel
2
2
  # Require all that active support we know and love
3
- require 'active_support/time'
4
- require 'active_support/core_ext/class/attribute'
5
- require 'active_support/core_ext/class/attribute_accessors'
6
- require 'active_support/core_ext/class/delegating_attributes'
7
- require 'active_support/core_ext/class/attribute'
8
- require 'active_support/core_ext/array/extract_options'
9
- require 'active_support/core_ext/hash/deep_merge'
10
- require 'active_support/core_ext/hash/indifferent_access'
11
- require 'active_support/core_ext/hash/slice'
12
- require 'active_support/core_ext/string/behavior'
13
- require 'active_support/core_ext/kernel/singleton_class'
14
- require 'active_support/core_ext/module/delegation'
15
- require 'active_support/core_ext/module/introspection'
16
- require 'active_support/core_ext/object/duplicable'
17
- require 'active_support/core_ext/object/blank'
3
+ require 'active_support/time'
4
+ require 'active_support/core_ext/class/attribute'
5
+ require 'active_support/core_ext/class/attribute_accessors'
6
+ require 'active_support/core_ext/class/delegating_attributes'
7
+ require 'active_support/core_ext/class/attribute'
8
+ require 'active_support/core_ext/array/extract_options'
9
+ require 'active_support/core_ext/hash/deep_merge'
10
+ require 'active_support/core_ext/hash/indifferent_access'
11
+ require 'active_support/core_ext/hash/slice'
12
+ require 'active_support/core_ext/string/behavior'
13
+ require 'active_support/core_ext/kernel/singleton_class'
14
+ require 'active_support/core_ext/module/delegation'
15
+ require 'active_support/core_ext/module/introspection'
16
+ require 'active_support/core_ext/object/duplicable'
17
+ require 'active_support/core_ext/object/blank'
18
18
 
19
19
  # == SimpleModel::Base
20
20
  #
21
21
  # Provides an interface for any class to build table-less models.
22
- #
22
+ #
23
23
  # Implements Validations, Callbacks and Dirty from ActiveModel, and data-type specific
24
24
  # attribute definitions with default options. SimpleModel::Base is intended as
25
25
  # an example, while it may be used in production, which it is on many of my apps
26
26
  # today, it is recommend you use SimpleModel::Base as an example to implement your
27
27
  # own model actions.
28
- #
28
+ #
29
29
  # == SimpleModel Actions:
30
- #
30
+ #
31
31
  # Model actions provide a tool for making use of Active Model callbacks. Each
32
- # action creates an instance method representing the action, which calls the
33
- # method(s) listed as symbols when defining the actions. Model actions also accept
34
- # a rollback option, which is called if the action fails. If you plan to
32
+ # action creates an instance method representing the action, which calls the
33
+ # method(s) listed as symbols when defining the actions. Model actions also accept
34
+ # a rollback option, which is called if the action fails. If you plan to
35
35
  # implement SimpleModel's actions, avoid naming you own methods "save", "destroy",
36
36
  # "create", and "update", as these will override the methods defined by action.
37
- #
37
+ #
38
38
  # Available Actions:
39
39
  # # save
40
40
  # # update
41
41
  # # create
42
42
  # # destroy
43
- #
43
+ #
44
44
  # ==== Example
45
- #
45
+ #
46
46
  # class MyModel < SimpleModel::Base
47
47
  # save :my_save, :rollback => :undo_save
48
48
  # update :my_update, :rollback => :undo_update
49
49
  # destroy :my_destory, :rollback => :undo_destory
50
50
  # end
51
- #
51
+ #
52
52
  # A basic SimpleModel implementation might resemble
53
- #
53
+ #
54
54
  # class MyModel < SimpleModel::Base
55
- #
55
+ #
56
56
  # has_integers :first_int, :second_int, :default => 1
57
57
  # has_times :now, :default => :get_now
58
- #
58
+ #
59
59
  # save :save_record, :rollback => :rollback_save
60
- #
60
+ #
61
61
  # def save_record
62
62
  # puts "saved"
63
63
  # true
64
64
  # end
65
- #
65
+ #
66
66
  # def get_today
67
67
  # Time.now
68
68
  # end
69
- #
69
+ #
70
70
  # def rollback_save
71
71
  # puts "rolled back"
72
72
  # end
73
73
  # end
74
-
74
+
75
75
  class Base
76
76
  include SimpleModel::Attributes
77
77
  include SimpleModel::ErrorHelpers
78
- #Use ActiveModel Resources
79
-
80
78
 
81
79
  define_model_callbacks :save, :update, :create, :destroy
82
-
80
+
83
81
  class << self
84
-
82
+
83
+ # Defines the model action's instance methods and applied defaults. For every
84
+ # action defined, we also define that actions ! method which raises exceptions
85
+ # when the action fails.
86
+ def define_model_action(methods,action,default_options={:validate => true})
87
+ default_options.merge!(methods.extract_options!)
88
+ actions = [action,"#{action}!".to_sym]
89
+ actions.each do |a|
90
+ define_method(a) do |opts = {}|
91
+ options = default_options.merge(opts)
92
+ options[:raise_exception] = a.to_s.match(/\!$/)
93
+ run_callbacks(action) do
94
+ run_model_action(methods,options)
95
+ end
96
+ end
97
+ end
98
+ end
99
+
85
100
  def save(*methods)
86
101
  define_model_action(methods,:save)
87
102
  end
88
-
103
+
89
104
  def create(*methods)
90
105
  define_model_action(methods,:create)
91
106
  end
92
-
107
+
93
108
  def update(*methods)
94
109
  define_model_action(methods,:update)
95
110
  end
96
-
111
+
97
112
  # Destroy does not run normal validation in Rails, but with this we can if we choose to.
98
- def destroy(*methods)
113
+ def destroy(*methods)
99
114
  define_model_action(methods,:destroy, {:validate => false})
100
- end
115
+ end
116
+
101
117
  end
102
-
118
+
103
119
  has_boolean :persisted
104
120
  has_boolean :new_record, :default => true
105
121
  has_attribute :id # may not be an integer
106
122
  alias :saved? :persisted?
107
123
 
108
124
  private
109
-
125
+
110
126
  # Skeleton for action instance methods
111
127
  def run_model_action(methods,options)
112
128
  completed = true
113
- if (!options[:validate] || (options[:validation_methods] && valid_using_other?(options[:validation_methods])) || self.valid?)
129
+ if (!options[:validate] || (options[:validation_methods] && valid_using_other?(options[:validation_methods])) || self.valid?)
114
130
  methods.each do |method|
115
- ran = self.send(method)
131
+ ran = send(method)
116
132
  completed = ran unless ran
117
133
  end
118
134
  if completed
119
135
  self.persisted = true
120
136
  @previously_changed = changes
121
- @changed_attributes.clear
137
+ @changed_attributes.clear
122
138
  else
123
- self.send(options[:rollback]) unless options[:rollback].blank?
124
- end
139
+ send(options[:rollback]) unless options[:rollback].blank?
140
+ end
125
141
  else
126
142
  completed = false
127
143
  end
128
- if !completed && options[:raise_exception]
144
+ if !completed && options[:raise_exception]
129
145
  if !self.errors.blank?
130
146
  raise ValidationError, self.errors.full_messages.join(" ")
131
147
  else
132
148
  raise ActionError, "failed action: #{methods.join(', ')}"
133
149
  end
134
- end
150
+ end
135
151
  completed
136
- end
137
-
152
+ end
153
+
138
154
  # Run supplied methods as validation. Each method should return a boolean
139
155
  # If using this option, to see if errors are present use object_name.errors.blank?,
140
- # otherwise if you run object_name.valid? you will over write the errors
156
+ # otherwise if you run object_name.valid? you will over write the errors
141
157
  # generated here.
142
158
  def valid_using_other?(methods)
143
159
  valid = true
144
160
  methods.each do |method|
145
- valid = false unless self.send(method)
161
+ valid = false unless send(method)
146
162
  end
147
163
  valid
148
- end
149
-
150
- # Defines the model action's instance methods and applied defaults. For every
151
- # action defined, we also define that actions ! method which raises exceptions
152
- # when the action fails.
153
- def self.define_model_action(methods,action,default_options={:validate => true})
154
- default_options.merge!(methods.extract_options!)
155
- actions = [action,"#{action}!".to_sym]
156
- actions.each do |a|
157
- define_method(a) do |opts = {}|
158
- options = default_options.merge(opts)
159
- options[:raise_exception] = a.to_s.match(/\!$/)
160
- self.run_callbacks(action) do
161
- run_model_action(methods,options)
162
- end
163
- end
164
- end
165
164
  end
166
165
  end
167
- end
166
+ end
@@ -1,6 +1,6 @@
1
1
  module SimpleModel
2
2
  module ErrorHelpers
3
-
3
+
4
4
  attr_accessor :errors_count
5
5
 
6
6
  def errors?
@@ -10,13 +10,12 @@ module SimpleModel
10
10
 
11
11
  def errors_for_flash(options={})
12
12
  #set defaults and overwrite
13
- options = {
14
- :failed_action => "saving",
15
- :id => 'errorExplanation',
16
- :classes => ''}.merge!(options)
17
-
13
+ options = {:failed_action => "saving",
14
+ :id => 'errorExplanation',
15
+ :classes => ''}.merge!(options)
16
+
18
17
  error_list = ""
19
-
18
+
20
19
  # The active_model errors object is not a normal hash and the each method
21
20
  # for the active_mode errors object does not perform as expected
22
21
  # so make it a plain hash
@@ -39,7 +38,7 @@ module SimpleModel
39
38
  value.uniq!
40
39
  if value.length == 1
41
40
  self.errors_count = (self.errors_count.to_i + 1)
42
- error_items << "<li>#{key.to_s.titleize} #{value[0]}</li>"
41
+ error_items << "<li>#{key.to_s.titleize} #{value[0]}</li>"
43
42
  else
44
43
  error_items << "<li><ul>#{key.to_s.titleize} errors:"
45
44
  value.each do |item|
@@ -78,6 +77,6 @@ module SimpleModel
78
77
  end
79
78
  error_string
80
79
  end
81
-
80
+
82
81
  end
83
82
  end
@@ -1,16 +1,36 @@
1
1
  module SimpleModel
2
+ module ToCurrencyS
3
+ def to_currency_s(symbol='$',rnd=2)
4
+ cs = self.round(rnd).abs.to_s
5
+ while cs.index('.') != (cs.length-3)
6
+ cs << '0'
7
+ end
8
+ comma = 6
9
+ while cs.length > (comma)
10
+ cs.insert((cs.length - comma), ",")
11
+ comma += 4
12
+ end
13
+ cs.insert(0,symbol) if symbol
14
+ if self < 0
15
+ cs.insert(0, "-")
16
+ end
17
+ cs
18
+ end
19
+ end
20
+
2
21
  module ExtendCore
3
22
  require 'time'
4
23
  require 'date'
5
24
  require 'bigdecimal'
6
25
  require 'bigdecimal/util'
7
-
8
- Float.class_eval do
26
+
27
+ Float.class_eval do
28
+ include ToCurrencyS
9
29
  # that does not equal 0.0 is true
10
30
  def to_b
11
- self != 0.0
31
+ zero?
12
32
  end
13
-
33
+
14
34
  # Rounds float to the precision specified
15
35
  # 100.5235.round_to #=> 101.0
16
36
  # 100.5235.round_to(1) #=> 101.5
@@ -25,26 +45,6 @@ module SimpleModel
25
45
  def to_currency
26
46
  self.to_d.round(2)
27
47
  end
28
-
29
- # Returns a string with representation of currency, rounded to nearest hundredth
30
- def to_currency_s(symbol="$")
31
- num = self.round_to(2).to_s
32
- neg = num.include?("-")
33
- while num.index('.') != (num.length-3)
34
- num << '0'
35
- end
36
- comma = 6
37
- while num.length > (comma)
38
- num.insert((num.length - comma), ",")
39
- comma += 4
40
- end
41
- num.insert(0,symbol)
42
- if neg
43
- num.delete!("-")
44
- num.insert(0, "-")
45
- end
46
- num
47
- end
48
48
 
49
49
  def to_time
50
50
  Time.at(self)
@@ -71,23 +71,19 @@ module SimpleModel
71
71
  # * safe_date_string("12/31/2010") # => '2010-12-31'
72
72
  # * safe_date_string("12/31/2010T23:30:25") # => '2010-12-31T23:30:25'
73
73
  # * safe_date_string("12/31/2010 23:30:25") # => '2010-12-31 23:30:25'
74
- # * safe_date_string("\/Date(1310669017000)\/") # =>
74
+ # * safe_date_string("\/Date(1310669017000)\/") # =>
75
75
  def safe_datetime_string
76
76
  safe_date = nil
77
77
  if self[0..9].match(/^(0[1-9]|[1-9]|1[012])[- \/.]([1-9]|0[1-9]|[12][0-9]|3[01])[- \/.][0-9][0-9][0-9][0-9]/)
78
78
  safe_date = ""
79
- if self.include?("/")
80
- split = self.split("/")
81
- else
82
- split = self.split("-")
83
- end
79
+ splt = split(/\-|\/|\./)
84
80
  time = ""
85
- if split[2].length > 4
86
- time = split[2][4..(split[2].length - 1)]
87
- split[2] = split[2][0..3]
81
+ if splt[2].length > 4
82
+ time = splt[2][4..(splt[2].length - 1)]
83
+ splt[2] = splt[2][0..3]
88
84
  end
89
- if split.length == 3 && split[2].length == 4
90
- safe_date << "#{split[2]}-#{split[0]}-#{split[1]}"
85
+ if splt.length == 3 && splt[2].length == 4
86
+ safe_date << "#{splt[2]}-#{splt[0]}-#{splt[1]}"
91
87
  safe_date << "#{time}" unless time.nil? || time.to_s.length == 0
92
88
  end
93
89
  elsif self.match(/^\/Date\(/)
@@ -97,7 +93,7 @@ module SimpleModel
97
93
  end
98
94
  safe_date
99
95
  end
100
-
96
+
101
97
  # Use safe_datetime_string help with those pesky US date formats in Ruby 1.9
102
98
  # or to change an integer string to date
103
99
  def to_date
@@ -118,44 +114,38 @@ module SimpleModel
118
114
  def to_f
119
115
  gsub(/[^0-9\.\+\-]/, '').core_to_f
120
116
  end
121
-
117
+
122
118
  alias :core_to_d :to_d
123
-
119
+
124
120
  def to_d
125
121
  gsub(/[^0-9\.\+\-]/, '').core_to_d
126
122
  end
127
123
  alias :to_currency :to_d
128
-
124
+
129
125
  end
130
-
126
+
131
127
  BigDecimal.class_eval do
132
- def to_currency_s(symbol="$")
133
- self.to_f.to_currency_s(symbol)
134
- end
135
-
128
+ include ToCurrencyS
129
+
136
130
  def to_b
137
- self != 0.0
131
+ zero?
138
132
  end
139
133
  end
140
-
141
134
  Fixnum.class_eval do
142
- def to_currency_s(symbol="$")
143
- self.to_f.to_currency_s(symbol)
144
- end
145
-
135
+ include ToCurrencyS
136
+
146
137
  unless Fixnum.instance_methods.include?(:to_b)
147
- #Any value greater than 0 is true
148
138
  def to_b
149
- self > 0
139
+ zero?
150
140
  end
151
141
  end
152
-
142
+
153
143
  unless Fixnum.instance_methods.include?(:to_d)
154
144
  def to_d
155
- BigDecimal.new("#{self}.0")
145
+ BigDecimal.new(self)
156
146
  end
157
147
  end
158
-
148
+
159
149
  unless Fixnum.instance_methods.include?(:to_date)
160
150
  def to_date
161
151
  Time.at(self).to_date
@@ -176,7 +166,7 @@ module SimpleModel
176
166
  end
177
167
  unless NilClass.instance_methods.include?(:to_d)
178
168
  def to_d
179
- BigDecimal.new("0.0")
169
+ BigDecimal.new('')
180
170
  end
181
171
  end
182
172
  end
@@ -195,4 +185,4 @@ module SimpleModel
195
185
  end
196
186
  end
197
187
  end
198
- end
188
+ end
@@ -11,4 +11,4 @@ module SimpleModel
11
11
  end
12
12
  end
13
13
  end
14
- end
14
+ end
@@ -25,13 +25,13 @@ module SimpleModel
25
25
  end
26
26
  end
27
27
  end
28
-
28
+
29
29
  def validates_length_of(*attr_names)
30
30
  options = attr_names.extract_options!
31
31
 
32
32
  attr_names.each do |attr|
33
33
  break if conditional?(options)
34
-
34
+
35
35
  att_method = send(attr)
36
36
  unless att_method.blank?
37
37
  errors.add(attr,(options[:message].blank? ? "must equal #{options[:is]} characters in length." : options[:message])) if options[:is] && att_method.to_s.length != options[:is]
@@ -59,8 +59,8 @@ module SimpleModel
59
59
  private
60
60
  def conditional?(options)
61
61
  return true unless ((options[:if].blank? && options[:unless].blank?) ||
62
- !options[:if].blank? && send(options[:if])) ||
62
+ !options[:if].blank? && send(options[:if])) ||
63
63
  (!options[:unless].blank? && !send(options[:unless]))
64
- end
64
+ end
65
65
  end
66
66
  end
@@ -1,3 +1,3 @@
1
1
  module SimpleModel
2
- VERSION = "1.2.22"
2
+ VERSION = "1.2.23"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simple_model
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.22
4
+ version: 1.2.23
5
5
  platform: ruby
6
6
  authors:
7
7
  - Joshua T Mckinney
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-12-10 00:00:00.000000000 Z
11
+ date: 2014-12-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport