simple_model 0.2.0 → 0.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +18 -9
- data/lib/simple_model/attributes.rb +34 -16
- data/lib/simple_model/base.rb +28 -0
- data/lib/simple_model/extend_core.rb +150 -135
- data/lib/simple_model/simple_model_railtie.rb +12 -0
- data/lib/simple_model/version.rb +1 -1
- data/lib/simple_model.rb +10 -21
- data/simple_model.gemspec +5 -1
- data/spec/extend_core_spec.rb +33 -32
- data/spec/simple_model_spec.rb +13 -13
- metadata +29 -6
- data/lib/simple_model/errors.rb +0 -73
data/Gemfile.lock
CHANGED
@@ -1,24 +1,33 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
simple_model (0.
|
4
|
+
simple_model (0.2.0)
|
5
|
+
activemodel (= 3.0.5)
|
6
|
+
activesupport (= 3.0.5)
|
5
7
|
|
6
8
|
GEM
|
7
9
|
remote: http://rubygems.org/
|
8
10
|
specs:
|
11
|
+
activemodel (3.0.5)
|
12
|
+
activesupport (= 3.0.5)
|
13
|
+
builder (~> 2.1.2)
|
14
|
+
i18n (~> 0.4)
|
15
|
+
activesupport (3.0.5)
|
16
|
+
builder (2.1.2)
|
9
17
|
diff-lcs (1.1.2)
|
10
|
-
|
11
|
-
|
12
|
-
rspec-
|
13
|
-
rspec-
|
14
|
-
|
15
|
-
rspec-
|
18
|
+
i18n (0.5.0)
|
19
|
+
rspec (2.5.0)
|
20
|
+
rspec-core (~> 2.5.0)
|
21
|
+
rspec-expectations (~> 2.5.0)
|
22
|
+
rspec-mocks (~> 2.5.0)
|
23
|
+
rspec-core (2.5.1)
|
24
|
+
rspec-expectations (2.5.0)
|
16
25
|
diff-lcs (~> 1.1.2)
|
17
|
-
rspec-mocks (2.
|
26
|
+
rspec-mocks (2.5.0)
|
18
27
|
|
19
28
|
PLATFORMS
|
20
29
|
ruby
|
21
30
|
|
22
31
|
DEPENDENCIES
|
23
|
-
rspec
|
32
|
+
rspec
|
24
33
|
simple_model!
|
@@ -1,37 +1,34 @@
|
|
1
1
|
module SimpleModel
|
2
|
+
# require all that active support we know and love
|
3
|
+
require 'active_support/core_ext/array/extract_options'
|
4
|
+
require 'active_support/core_ext/object/blank'
|
5
|
+
|
2
6
|
module Attributes
|
3
7
|
include ExtendCore
|
4
8
|
|
9
|
+
attr_accessor :id, :saved
|
5
10
|
#Set attribute values to those supplied at initialization
|
6
11
|
def initialize(*attrs)
|
7
12
|
set_attributes(attrs.extract_options!)
|
8
13
|
end
|
9
14
|
|
10
|
-
def id
|
11
|
-
@id
|
12
|
-
end
|
13
|
-
|
14
|
-
def id=(id)
|
15
|
-
@id=id
|
16
|
-
end
|
17
|
-
|
18
15
|
def new_record
|
19
16
|
@new_record = true if @new_record.nil?
|
20
17
|
@new_record
|
21
18
|
end
|
22
19
|
|
23
|
-
def persisted?
|
24
|
-
!new_record
|
25
|
-
end
|
26
|
-
|
27
20
|
def new_record?
|
28
21
|
new_record
|
29
22
|
end
|
30
|
-
|
23
|
+
|
31
24
|
def new_record=(new_record)
|
32
25
|
@new_record = new_record
|
33
26
|
end
|
34
27
|
|
28
|
+
def persisted?
|
29
|
+
saved.to_b
|
30
|
+
end
|
31
|
+
|
35
32
|
# Place to store set attributes and their values
|
36
33
|
def attributes
|
37
34
|
@attributes ||= {}
|
@@ -52,6 +49,8 @@ module SimpleModel
|
|
52
49
|
end
|
53
50
|
|
54
51
|
module ClassMethods
|
52
|
+
|
53
|
+
#creates setter and getter datatype special attribute
|
55
54
|
def has_attributes(*attrs)
|
56
55
|
options = attrs.extract_options!
|
57
56
|
attrs.each do |attr|
|
@@ -86,6 +85,7 @@ module SimpleModel
|
|
86
85
|
end
|
87
86
|
end
|
88
87
|
end
|
88
|
+
alias :has_boolean :has_booleans
|
89
89
|
|
90
90
|
# Creates setter and getter methods for integer attributes
|
91
91
|
def has_ints(*attrs)
|
@@ -102,8 +102,12 @@ module SimpleModel
|
|
102
102
|
end
|
103
103
|
end
|
104
104
|
end
|
105
|
+
alias :has_int :has_ints
|
105
106
|
|
106
|
-
# Creates setter and getter methods for
|
107
|
+
# Creates setter and getter methods for currency attributes
|
108
|
+
# attributes are cast to BigDecimal and rounded to nearest cent
|
109
|
+
# Warning, rounding occurs on all sets, so if you need to keep higher prescsion
|
110
|
+
# use has_decimals
|
107
111
|
def has_currency(*attrs)
|
108
112
|
options = attrs.extract_options!
|
109
113
|
attrs.each do |attr|
|
@@ -113,10 +117,24 @@ module SimpleModel
|
|
113
117
|
instance_variable_set("@#{attr}", val.to_s.to_currency)
|
114
118
|
attributes[attr] = val
|
115
119
|
val
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
116
123
|
|
124
|
+
def has_decimals(*attrs)
|
125
|
+
options = attrs.extract_options!
|
126
|
+
attrs.each do |attr|
|
127
|
+
attr_reader attr
|
128
|
+
define_reader_with_options(attr,options)
|
129
|
+
define_method("#{attr.to_s}=") do |val|
|
130
|
+
instance_variable_set("@#{attr}", BigDecimal("#{val}"))
|
131
|
+
attributes[attr] = val
|
132
|
+
val
|
117
133
|
end
|
118
134
|
end
|
119
135
|
end
|
136
|
+
alias :has_decimal :has_decimals
|
137
|
+
|
120
138
|
# Creates setter and getter methods for float attributes
|
121
139
|
def has_floats(*attrs)
|
122
140
|
options = attrs.extract_options!
|
@@ -133,7 +151,7 @@ module SimpleModel
|
|
133
151
|
end
|
134
152
|
end
|
135
153
|
end
|
136
|
-
|
154
|
+
alias :has_float :has_floats
|
137
155
|
# Creates setter and getter methods for date attributes
|
138
156
|
def has_dates(*attrs)
|
139
157
|
options = attrs.extract_options!
|
@@ -149,7 +167,7 @@ module SimpleModel
|
|
149
167
|
end
|
150
168
|
end
|
151
169
|
end
|
152
|
-
|
170
|
+
alias :has_date :has_dates
|
153
171
|
# Creates setter and getter methods for time attributes
|
154
172
|
def has_times(*attrs)
|
155
173
|
options = attrs.extract_options!
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module SimpleModel
|
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'
|
18
|
+
|
19
|
+
class Base
|
20
|
+
include SimpleModel::Attributes
|
21
|
+
include SimpleModel::ErrorHelpers
|
22
|
+
|
23
|
+
#User ActiveModel Resources
|
24
|
+
include ActiveModel::Validations
|
25
|
+
include ActiveModel::Conversion
|
26
|
+
extend ActiveModel::Naming
|
27
|
+
end
|
28
|
+
end
|
@@ -1,160 +1,175 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
module ExtendCore
|
3
|
+
require 'time'
|
4
|
+
require 'date'
|
5
|
+
require 'bigdecimal'
|
6
|
+
require 'bigdecimal/util'
|
7
|
+
|
8
|
+
Float.class_eval do
|
9
|
+
|
10
|
+
# any value greater than 0.0 is true
|
11
|
+
def to_b
|
12
|
+
self > 0.0
|
13
|
+
end
|
14
|
+
# Rounds float to the precision specified
|
15
|
+
# 100.5235.round_to #=> 101.0
|
16
|
+
# 100.5235.round_to(1) #=> 101.5
|
17
|
+
# 100.5235.round_to(3) #=> 101.524
|
18
|
+
def round_to(precision=0)
|
19
|
+
mulitplier = 10.0 ** (precision)
|
20
|
+
(((((self)*mulitplier).round).to_f)/mulitplier)
|
21
|
+
end
|
1
22
|
|
2
|
-
|
3
|
-
|
4
|
-
|
23
|
+
# Returns a BigDecimal object rounded to the nearest hundredth
|
24
|
+
# Why decimal?..because precision matters when dealing with money ;)
|
25
|
+
def to_currency
|
26
|
+
self.to_d.round(2)
|
27
|
+
end
|
5
28
|
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
+
while num.index('.') != (num.length-3)
|
33
|
+
num << '0'
|
34
|
+
end
|
35
|
+
comma = 6
|
36
|
+
while num.length > (comma)
|
37
|
+
num.insert((num.length - comma), ",")
|
38
|
+
comma += 4
|
39
|
+
end
|
40
|
+
num.insert(0,symbol)
|
41
|
+
num
|
42
|
+
end
|
11
43
|
end
|
12
44
|
|
13
|
-
# Round to nearest cent
|
14
|
-
def to_currency
|
15
|
-
round_to(0.01)
|
16
|
-
end
|
17
45
|
|
18
|
-
#
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
comma = 6
|
26
|
-
while num.length > (comma)
|
27
|
-
num.insert((num.length - comma), ",")
|
28
|
-
comma += 4
|
46
|
+
#Extend Ruby String.rb
|
47
|
+
String.class_eval do
|
48
|
+
|
49
|
+
# returns boolean value for common boolean string values
|
50
|
+
def to_b
|
51
|
+
['1',"true", "t"].include?(self)
|
29
52
|
end
|
30
|
-
num.insert(0,symbol)
|
31
|
-
num
|
32
|
-
end
|
33
|
-
end
|
34
53
|
|
35
|
-
Object.class_eval do
|
36
|
-
# Borrowed from Rails activesupport/lib/active_support/core_ext/object/blank.rb
|
37
|
-
def blank?
|
38
|
-
respond_to?(:empty?) ? empty? : !self
|
39
|
-
end
|
40
|
-
end
|
41
54
|
|
55
|
+
# Takes known US formatted date/time strings (MM/DD/YYYY TIME) and converts
|
56
|
+
# them to international format (YYYY/MM/DD TIME)
|
57
|
+
#
|
58
|
+
# * safe_date_string("12/31/2010") # => '2010-12-31'
|
59
|
+
# * safe_date_string("12/31/2010T23:30:25") # => '2010-12-31T23:30:25'
|
60
|
+
# * safe_date_string("12/31/2010 23:30:25") # => '2010-12-31 23:30:25'
|
61
|
+
def safe_datetime_string
|
62
|
+
date = self
|
63
|
+
date_string = ""
|
64
|
+
if date[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]/)
|
65
|
+
if date.include?("/")
|
66
|
+
split = date.split("/")
|
67
|
+
else
|
68
|
+
split = date.split("-")
|
69
|
+
end
|
70
|
+
time = ""
|
71
|
+
if split[2].length > 4
|
72
|
+
time = split[2][4..(split[2].length - 1)]
|
73
|
+
split[2] = split[2][0..3]
|
74
|
+
end
|
75
|
+
if split.length == 3 && split[2].length == 4
|
76
|
+
date_string << "#{split[2]}-#{split[0]}-#{split[1]}"
|
77
|
+
date_string << "#{time}" unless time.nil? || time.to_s.length == 0
|
78
|
+
end
|
79
|
+
end
|
80
|
+
date_string = date if date_string.length == 0
|
81
|
+
date_string
|
82
|
+
end
|
83
|
+
|
84
|
+
# Use safe_datetime_string help with those pesky US date formats in Ruby 1.9
|
85
|
+
def to_date
|
86
|
+
Date.parse(safe_datetime_string)
|
87
|
+
end
|
42
88
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
# element in the array if it's a hash, otherwise returns a blank hash.
|
48
|
-
#
|
49
|
-
# def options(*args)
|
50
|
-
# args.extract_options!
|
51
|
-
# end
|
52
|
-
#
|
53
|
-
# options(1, 2) # => {}
|
54
|
-
# options(1, 2, :a => :b) # => {:a=>:b}
|
55
|
-
def extract_options!
|
56
|
-
last.is_a?(::Hash) ? pop : {}
|
57
|
-
end
|
58
|
-
end
|
89
|
+
# Use safe_datetime_string help with those pesky US date formats in Ruby 1.9
|
90
|
+
def to_time
|
91
|
+
Time.parse(safe_datetime_string)
|
92
|
+
end
|
59
93
|
|
60
|
-
#Extend Ruby String.rb
|
61
|
-
String.class_eval do
|
62
94
|
|
63
|
-
|
64
|
-
def to_b
|
65
|
-
['1',"true", "t"].include?(self)
|
66
|
-
end
|
95
|
+
alias :old_to_f :to_f
|
67
96
|
|
68
|
-
|
97
|
+
# Remove none numeric characters then run default ruby float cast
|
98
|
+
def to_f
|
99
|
+
gsub(/[^0-9\.\+\-]/, '').old_to_f
|
100
|
+
end
|
69
101
|
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
102
|
+
# Cleans all none pertinent characters and returns a BigDecimal rounded to nearest hundredth
|
103
|
+
# Why decimal?..because precision matters when dealing with money ;)
|
104
|
+
def to_currency
|
105
|
+
gsub(/[^0-9\.\+\-]/, '').to_d.round(2)
|
106
|
+
end
|
74
107
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
108
|
+
|
109
|
+
# Parse a full name into it's parts. http://pastie.org/867415
|
110
|
+
# Based on :http://artofmission.com/articles/2009/5/31/parse-full-names-with-ruby
|
111
|
+
#
|
112
|
+
# Options:
|
113
|
+
# +name+
|
114
|
+
# +seperate_middle_name+ defaults to true. if false, will combine middle name into last name.
|
115
|
+
|
116
|
+
def parse_name(seperate_middle_name=true)
|
117
|
+
str = self
|
118
|
+
if str.include?(',') # Rearrange names formatted as Doe, John C. to John C. Doe
|
119
|
+
temp = str.split(',')
|
120
|
+
temp << temp[0]
|
121
|
+
temp.delete_at(0)
|
122
|
+
str = temp.join(" ")
|
79
123
|
|
80
|
-
# Takes known US formatted date/time strings (MM/DD/YYYY TIME) and converts
|
81
|
-
# them to international format (YYYY/MM/DD TIME)
|
82
|
-
#
|
83
|
-
# * safe_date_string("12/31/2010") # => '2010-12-31'
|
84
|
-
# * safe_date_string("12/31/2010T23:30:25") # => '2010-12-31T23:30:25'
|
85
|
-
# * safe_date_string("12/31/2010 23:30:25") # => '2010-12-31 23:30:25'
|
86
|
-
def safe_datetime_string
|
87
|
-
date = self
|
88
|
-
date_string = ""
|
89
|
-
if date[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]/)
|
90
|
-
if date.include?("/")
|
91
|
-
split = date.split("/")
|
92
|
-
else
|
93
|
-
split = date.split("-")
|
94
|
-
end
|
95
|
-
time = ""
|
96
|
-
if split[2].length > 4
|
97
|
-
time = split[2][4..(split[2].length - 1)]
|
98
|
-
split[2] = split[2][0..3]
|
99
|
-
end
|
100
|
-
if split.length == 3 && split[2].length == 4
|
101
|
-
date_string << "#{split[2]}-#{split[0]}-#{split[1]}"
|
102
|
-
date_string << "#{time}" unless time.blank?
|
103
124
|
end
|
125
|
+
parts = str.split # First, split the name into an array
|
126
|
+
|
127
|
+
parts.each_with_index do |part, i|
|
128
|
+
# If any part is "and", then put together the two parts around it
|
129
|
+
# For example, "Mr. and Mrs." or "Mickey and Minnie"
|
130
|
+
if part=~/^(and|&)$/i && i > 0
|
131
|
+
parts[i-1] = [parts.delete_at(i+1), parts.at(i).downcase, parts.delete_at(i-1)].reverse * " "
|
132
|
+
end
|
133
|
+
end if self=~/\s(and|&)\s/i # optimize
|
134
|
+
|
135
|
+
{ :prefix => (parts.shift if parts[0]=~/^\w+\./),
|
136
|
+
:first_name => parts.shift || "", # if name is "", then atleast first_name should be ""
|
137
|
+
:suffix => (parts.pop if parts[-1]=~/(\w+\.|[IVXLM]+|[A-Z]+\.|(?i)jr|(?i)sr )$/),
|
138
|
+
:last_name => (seperate_middle_name ? parts.pop : parts.slice!(0..-1) * " "),
|
139
|
+
:middle_name => (parts * " " unless parts.empty?) }
|
104
140
|
end
|
105
|
-
date_string = date if date_string.blank?
|
106
|
-
date_string
|
107
141
|
end
|
142
|
+
Fixnum.class_eval do
|
108
143
|
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
144
|
+
#Any value greater than 0 is true
|
145
|
+
def to_b
|
146
|
+
self > 0
|
147
|
+
end
|
148
|
+
|
149
|
+
def to_d
|
150
|
+
BigDecimal.new("#{self}.0")
|
151
|
+
end
|
113
152
|
end
|
114
153
|
|
115
|
-
|
116
|
-
|
117
|
-
|
154
|
+
NilClass.class_eval do
|
155
|
+
def to_b
|
156
|
+
false
|
157
|
+
end
|
118
158
|
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
# +name+
|
125
|
-
# +seperate_middle_name+ defaults to true. if false, will combine middle name into last name.
|
126
|
-
|
127
|
-
def parse_name(seperate_middle_name=true)
|
128
|
-
str = self
|
129
|
-
if str.include?(',') # Rearrange names formatted as Doe, John C. to John C. Doe
|
130
|
-
temp = str.split(',')
|
131
|
-
temp << temp[0]
|
132
|
-
temp.delete_at(0)
|
133
|
-
str = temp.join(" ")
|
134
|
-
|
135
|
-
end
|
136
|
-
parts = str.split # First, split the name into an array
|
137
|
-
|
138
|
-
parts.each_with_index do |part, i|
|
139
|
-
# If any part is "and", then put together the two parts around it
|
140
|
-
# For example, "Mr. and Mrs." or "Mickey and Minnie"
|
141
|
-
if part=~/^(and|&)$/i && i > 0
|
142
|
-
parts[i-1] = [parts.delete_at(i+1), parts.at(i).downcase, parts.delete_at(i-1)].reverse * " "
|
143
|
-
end
|
144
|
-
end if self=~/\s(and|&)\s/i # optimize
|
159
|
+
def to_d
|
160
|
+
BigDecimal.new("0.0")
|
161
|
+
end
|
162
|
+
end
|
163
|
+
TrueClass.class_eval do
|
145
164
|
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
:last_name => (seperate_middle_name ? parts.pop : parts.slice!(0..-1) * " "),
|
150
|
-
:middle_name => (parts * " " unless parts.empty?) }
|
165
|
+
def to_b
|
166
|
+
self
|
167
|
+
end
|
151
168
|
end
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
self > 0
|
169
|
+
FalseClass.class_eval do
|
170
|
+
def to_b
|
171
|
+
self
|
172
|
+
end
|
157
173
|
end
|
158
|
-
|
159
|
-
|
160
|
-
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
class SimpleModelRailtie < ::Rails::Railtie
|
3
|
+
initializer "simple_model.extend_core" do
|
4
|
+
extend SimpleModel::ExtendCore
|
5
|
+
end
|
6
|
+
initializer "simple_model.ar_error_helper" do
|
7
|
+
class ActiveRecord::Base
|
8
|
+
include SimpleModel::ErrorHelpers
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
data/lib/simple_model/version.rb
CHANGED
data/lib/simple_model.rb
CHANGED
@@ -1,29 +1,18 @@
|
|
1
1
|
module SimpleModel
|
2
2
|
|
3
|
+
#Get those rails goodies
|
4
|
+
require 'active_support'
|
5
|
+
require 'active_support/i18n'
|
6
|
+
require 'active_model'
|
7
|
+
|
8
|
+
# Load as necessary
|
3
9
|
autoload :ExtendCore, "simple_model/extend_core"
|
4
10
|
autoload :Attributes, "simple_model/attributes"
|
5
|
-
autoload :Errors, "simple_model/errors"
|
6
11
|
autoload :ErrorHelpers, "simple_model/error_helpers"
|
7
|
-
autoload :Validation, "simple_model/validation"
|
8
|
-
|
9
|
-
# require active_model if available
|
10
|
-
begin
|
11
|
-
require 'active_model'
|
12
|
-
rescue LoadError
|
13
|
-
#Don't fail
|
14
|
-
end
|
12
|
+
autoload :Validation, "simple_model/validation"
|
13
|
+
autoload :Base, "simple_model/base"
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
include SimpleModel::ErrorHelpers
|
19
|
-
begin
|
20
|
-
include ActiveModel::Validations
|
21
|
-
include ActiveModel::Conversion
|
22
|
-
extend ActiveModel::Naming
|
23
|
-
rescue NameError
|
24
|
-
include SimpleModel::Errors
|
25
|
-
include SimpleModel::Validation
|
26
|
-
end
|
27
|
-
end
|
15
|
+
#Railtie
|
16
|
+
require 'simple_model/simple_model_railtie.rb' if defined?(Rails)
|
28
17
|
|
29
18
|
end
|
data/simple_model.gemspec
CHANGED
@@ -13,7 +13,11 @@ Gem::Specification.new do |s|
|
|
13
13
|
s.summary = %q{Simpifies building tableless models or models backed by webservices}
|
14
14
|
s.description = %q{Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values. Also, provides a simple error and validation api for non-rails 3 apps.}
|
15
15
|
|
16
|
-
s.
|
16
|
+
s.add_runtime_dependency 'activesupport','3.0.5'
|
17
|
+
s.add_runtime_dependency 'activemodel',"3.0.5"
|
18
|
+
|
19
|
+
s.add_development_dependency 'rspec'
|
20
|
+
|
17
21
|
s.rubyforge_project = "simple_model"
|
18
22
|
|
19
23
|
s.files = `git ls-files`.split("\n")
|
data/spec/extend_core_spec.rb
CHANGED
@@ -1,46 +1,47 @@
|
|
1
1
|
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
-
describe ExtendCore, 'Float.rb' do
|
3
|
+
describe SimpleModel::ExtendCore, 'Float.rb' do
|
4
4
|
before(:all) do
|
5
|
-
include ExtendCore
|
5
|
+
include SimpleModel::ExtendCore
|
6
6
|
end
|
7
7
|
|
8
|
-
describe ExtendCore, '
|
8
|
+
describe SimpleModel::ExtendCore, 'round' do
|
9
9
|
it "should return float rounded to specified precision" do
|
10
|
-
0.
|
11
|
-
0.3333.round_to(
|
12
|
-
0.33335.round_to(
|
10
|
+
0.5122.round_to.should eql(1.0)
|
11
|
+
0.3333.round_to(1).should eql(0.3)
|
12
|
+
0.33335.round_to(2).should eql(0.33)
|
13
|
+
0.33335.round_to(4).should eql(0.3334)
|
13
14
|
end
|
14
15
|
end
|
15
16
|
|
16
|
-
describe ExtendCore, 'to_currency_s' do
|
17
|
+
describe SimpleModel::ExtendCore, 'to_currency_s' do
|
17
18
|
it "should return a string" do
|
18
19
|
0.333.to_currency_s.class.should eql(String)
|
19
20
|
end
|
20
|
-
it "should prefix string with currency symbol" do
|
21
|
-
5.12.to_currency_s.include?("$").should be_true
|
22
|
-
end
|
23
|
-
it "should padd with zeros for cents" do
|
24
|
-
5.0.to_currency_s.should eql("$5.00")
|
25
|
-
end
|
26
|
-
it "should round string to nearest tenth" do
|
27
|
-
0.333.to_currency_s.should eql("$0.33")
|
28
|
-
end
|
29
|
-
it "should add commas to long numbers" do
|
30
|
-
500000000000.0.to_currency_s.should eql("$500,000,000,000.00")
|
31
|
-
50000000000.0.to_currency_s.should eql("$50,000,000,000.00")
|
32
|
-
5000000000.0.to_currency_s.should eql("$5,000,000,000.00")
|
33
|
-
end
|
21
|
+
# it "should prefix string with currency symbol" do
|
22
|
+
# 5.12.to_currency_s.include?("$").should be_true
|
23
|
+
# end
|
24
|
+
# it "should padd with zeros for cents" do
|
25
|
+
# 5.0.to_currency_s.should eql("$5.00")
|
26
|
+
# end
|
27
|
+
# it "should round string to nearest tenth" do
|
28
|
+
# 0.333.to_currency_s.should eql("$0.33")
|
29
|
+
# end
|
30
|
+
# it "should add commas to long numbers" do
|
31
|
+
# 500000000000.0.to_currency_s.should eql("$500,000,000,000.00")
|
32
|
+
# 50000000000.0.to_currency_s.should eql("$50,000,000,000.00")
|
33
|
+
# 5000000000.0.to_currency_s.should eql("$5,000,000,000.00")
|
34
|
+
# end
|
34
35
|
end
|
35
36
|
|
36
37
|
end
|
37
38
|
|
38
39
|
|
39
|
-
describe ExtendCore, 'String.rb' do
|
40
|
+
describe SimpleModel::ExtendCore, 'String.rb' do
|
40
41
|
before(:all) do
|
41
|
-
include ExtendCore
|
42
|
+
include SimpleModel::ExtendCore
|
42
43
|
end
|
43
|
-
describe ExtendCore, 'safe_datetime_string' do
|
44
|
+
describe SimpleModel::ExtendCore, 'safe_datetime_string' do
|
44
45
|
it "should set US formated datetime string to international" do
|
45
46
|
"12/31/2010".safe_datetime_string.should eql("2010-12-31")
|
46
47
|
"12/31/2010T23:31:59".safe_datetime_string.should eql("2010-12-31T23:31:59")
|
@@ -49,7 +50,7 @@ describe ExtendCore, 'String.rb' do
|
|
49
50
|
end
|
50
51
|
end
|
51
52
|
|
52
|
-
describe ExtendCore, 'to_b' do
|
53
|
+
describe SimpleModel::ExtendCore, 'to_b' do
|
53
54
|
it "should return a Boolean" do
|
54
55
|
"1".to_b.class.should eql(TrueClass)
|
55
56
|
"".to_b.class.should eql(FalseClass)
|
@@ -62,28 +63,28 @@ describe ExtendCore, 'String.rb' do
|
|
62
63
|
end
|
63
64
|
end
|
64
65
|
|
65
|
-
describe ExtendCore, 'to_date' do
|
66
|
+
describe SimpleModel::ExtendCore, 'to_date' do
|
66
67
|
it "return a Date object" do
|
67
68
|
"12/31/2010".to_date.class.should eql(Date)
|
68
69
|
end
|
69
70
|
end
|
70
|
-
describe ExtendCore, 'to_date' do
|
71
|
+
describe SimpleModel::ExtendCore, 'to_date' do
|
71
72
|
it "return a Time object" do
|
72
73
|
"12/31/2010".to_time.class.should eql(Time)
|
73
74
|
end
|
74
75
|
end
|
75
|
-
describe ExtendCore, 'to_f' do
|
76
|
+
describe SimpleModel::ExtendCore, 'to_f' do
|
76
77
|
it "return a Foat from a string that may contain non-numeric values" do
|
77
78
|
"$5,000.006".to_f.should eql(5000.006)
|
78
79
|
end
|
79
80
|
end
|
80
|
-
describe ExtendCore, 'to_currency' do
|
81
|
-
it "return a
|
82
|
-
"$5,000.006".to_currency.should eql(5000.01)
|
81
|
+
describe SimpleModel::ExtendCore, 'to_currency' do
|
82
|
+
it "return a BigDecimal from a string that may contain non-numeric values" do
|
83
|
+
"$5,000.006".to_currency.should eql(BigDecimal("5000.01"))
|
83
84
|
end
|
84
85
|
end
|
85
86
|
|
86
|
-
describe ExtendCore, 'parse_name' do
|
87
|
+
describe SimpleModel::ExtendCore, 'parse_name' do
|
87
88
|
it "return return hash with name elements properly keyed" do
|
88
89
|
hash = "Doe, John C.".parse_name
|
89
90
|
hash[:first_name].should eql("John")
|
data/spec/simple_model_spec.rb
CHANGED
@@ -30,16 +30,16 @@ describe SimpleModel do
|
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
|
-
describe SimpleModel::Errors do
|
34
|
-
it 'Should add a error setter' do
|
35
|
-
class TestError
|
36
|
-
include SimpleModel::Errors
|
37
|
-
attr_accessor :test_attr
|
38
|
-
end
|
39
|
-
a = TestError.new(self)
|
40
|
-
a.errors.add(:test_attr, "test")
|
41
|
-
a.errors?.should be_true
|
42
|
-
|
43
|
-
#a.test.should be_false
|
44
|
-
end
|
45
|
-
end
|
33
|
+
#describe SimpleModel::Errors do
|
34
|
+
# it 'Should add a error setter' do
|
35
|
+
# class TestError
|
36
|
+
# include SimpleModel::Errors
|
37
|
+
# attr_accessor :test_attr
|
38
|
+
# end
|
39
|
+
# a = TestError.new(self)
|
40
|
+
# a.errors.add(:test_attr, "test")
|
41
|
+
# a.errors?.should be_true
|
42
|
+
#
|
43
|
+
# #a.test.should be_false
|
44
|
+
# end
|
45
|
+
#end
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.1
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Joshua T Mckinney
|
@@ -10,20 +10,42 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-04-03 00:00:00 -05:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
|
-
name:
|
17
|
+
name: activesupport
|
18
18
|
prerelease: false
|
19
19
|
requirement: &id001 !ruby/object:Gem::Requirement
|
20
20
|
none: false
|
21
21
|
requirements:
|
22
22
|
- - "="
|
23
23
|
- !ruby/object:Gem::Version
|
24
|
-
version:
|
25
|
-
type: :
|
24
|
+
version: 3.0.5
|
25
|
+
type: :runtime
|
26
26
|
version_requirements: *id001
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activemodel
|
29
|
+
prerelease: false
|
30
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
31
|
+
none: false
|
32
|
+
requirements:
|
33
|
+
- - "="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: 3.0.5
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: *id002
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: rspec
|
40
|
+
prerelease: false
|
41
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: "0"
|
47
|
+
type: :development
|
48
|
+
version_requirements: *id003
|
27
49
|
description: Simpifies building tableless models or models backed by webservices. Create data type specific attributes with default if values. Also, provides a simple error and validation api for non-rails 3 apps.
|
28
50
|
email:
|
29
51
|
- joshmckin@gmail.com
|
@@ -43,9 +65,10 @@ files:
|
|
43
65
|
- Rakefile
|
44
66
|
- lib/simple_model.rb
|
45
67
|
- lib/simple_model/attributes.rb
|
68
|
+
- lib/simple_model/base.rb
|
46
69
|
- lib/simple_model/error_helpers.rb
|
47
|
-
- lib/simple_model/errors.rb
|
48
70
|
- lib/simple_model/extend_core.rb
|
71
|
+
- lib/simple_model/simple_model_railtie.rb
|
49
72
|
- lib/simple_model/validation.rb
|
50
73
|
- lib/simple_model/version.rb
|
51
74
|
- simple_model.gemspec
|
data/lib/simple_model/errors.rb
DELETED
@@ -1,73 +0,0 @@
|
|
1
|
-
module SimpleModel
|
2
|
-
|
3
|
-
module Errors
|
4
|
-
include ErrorHelpers
|
5
|
-
|
6
|
-
def errors
|
7
|
-
@errors ||= ErrorsHash.new
|
8
|
-
@errors
|
9
|
-
end
|
10
|
-
|
11
|
-
def valid?
|
12
|
-
self.errors.clear if errors
|
13
|
-
validate
|
14
|
-
self.errors.blank? || self.errors.empty?
|
15
|
-
end
|
16
|
-
|
17
|
-
|
18
|
-
def errors_on(attr)
|
19
|
-
self.valid?
|
20
|
-
[self.errors.on(attr.to_s)].flatten.compact
|
21
|
-
end
|
22
|
-
|
23
|
-
alias :error_on :errors_on
|
24
|
-
|
25
|
-
def validate
|
26
|
-
# Override to implement validation
|
27
|
-
end
|
28
|
-
|
29
|
-
class ErrorsHash
|
30
|
-
attr_accessor :errors
|
31
|
-
def initialize
|
32
|
-
errors
|
33
|
-
end
|
34
|
-
|
35
|
-
def errors
|
36
|
-
@errors ||= {}
|
37
|
-
@errors
|
38
|
-
end
|
39
|
-
|
40
|
-
def clear
|
41
|
-
self.errors = {}
|
42
|
-
end
|
43
|
-
|
44
|
-
def add(attr, message)
|
45
|
-
errors[attr.to_s] ||= []
|
46
|
-
errors[attr.to_s] << message
|
47
|
-
end
|
48
|
-
def count
|
49
|
-
errors.length
|
50
|
-
end
|
51
|
-
def empty?
|
52
|
-
errors.empty?
|
53
|
-
end
|
54
|
-
def full_messages
|
55
|
-
full_messages = []
|
56
|
-
errors.each do |error|
|
57
|
-
error[1].each do |message|
|
58
|
-
full_messages << "#{error[0].to_s} #{message}"
|
59
|
-
end
|
60
|
-
end
|
61
|
-
full_messages
|
62
|
-
end
|
63
|
-
|
64
|
-
|
65
|
-
def on(attr)
|
66
|
-
errors[attr.to_s] if errors[attr.to_s]
|
67
|
-
end
|
68
|
-
|
69
|
-
alias :[] :on
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
|
-
|