simple_model 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +10 -0
- data/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Rakefile +2 -0
- data/lib/simple_model.rb +13 -0
- data/lib/simple_model/attributes.rb +170 -0
- data/lib/simple_model/errors.rb +92 -0
- data/lib/simple_model/extend_core.rb +108 -0
- data/lib/simple_model/validation.rb +66 -0
- data/lib/simple_model/version.rb +3 -0
- data/simple_model.gemspec +23 -0
- data/spec/attributes_spec.rb +58 -0
- data/spec/extend_core_spec.rb +64 -0
- data/spec/simple_model_spec.rb +43 -0
- data/spec/spec.opts +4 -0
- data/spec/spec_helper.rb +13 -0
- metadata +98 -0
data/.autotest
ADDED
data/Gemfile
ADDED
data/Rakefile
ADDED
data/lib/simple_model.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
|
3
|
+
autoload :ExtendCore, "simple_model/extend_core"
|
4
|
+
autoload :Attributes, "simple_model/attributes"
|
5
|
+
autoload :Errors, "simple_model/errors"
|
6
|
+
autoload :Validation, "simple_model/validation"
|
7
|
+
|
8
|
+
class Base
|
9
|
+
include SimpleModel::Errors
|
10
|
+
include SimpleModel::Attributes
|
11
|
+
include SimpleModel::Validation
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,170 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
module Attributes
|
3
|
+
include ExtendCore
|
4
|
+
|
5
|
+
|
6
|
+
#Set attribute values to those supplied at initialization
|
7
|
+
def initialize(*attrs)
|
8
|
+
attrs.extract_options!.each do |attr|
|
9
|
+
self.send(("#{attr[0]}=").to_sym, attr[1])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
############## Pseudo Rails Methods ###############
|
14
|
+
def id
|
15
|
+
@id
|
16
|
+
end
|
17
|
+
|
18
|
+
def id=(id)
|
19
|
+
@id=id
|
20
|
+
end
|
21
|
+
|
22
|
+
# Defaults to true so rails will create a "new" form
|
23
|
+
# Set to false and rails will produce an "edit" form
|
24
|
+
def new_record
|
25
|
+
@new_record = true if @new_record.nil?
|
26
|
+
@new_record
|
27
|
+
end
|
28
|
+
|
29
|
+
def new_record?
|
30
|
+
new_record
|
31
|
+
end
|
32
|
+
|
33
|
+
def new_record=(new_record)
|
34
|
+
@new_record = new_record.to_b
|
35
|
+
end
|
36
|
+
|
37
|
+
############### End Pseudo Rails Methods ##############
|
38
|
+
|
39
|
+
# Place to store set attributes and their values
|
40
|
+
def attributes
|
41
|
+
@attributes ||= {}
|
42
|
+
@attributes
|
43
|
+
end
|
44
|
+
|
45
|
+
|
46
|
+
def self.included(base)
|
47
|
+
base.extend(ClassMethods)
|
48
|
+
end
|
49
|
+
|
50
|
+
module ClassMethods
|
51
|
+
def has_attributes(*attrs)
|
52
|
+
attrs.each do |attr|
|
53
|
+
attr_reader attr
|
54
|
+
|
55
|
+
define_method("#{attr.to_s}=") do |val|
|
56
|
+
instance_variable_set("@#{attr}", val)
|
57
|
+
attributes[attr] = val
|
58
|
+
val
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Creates setter and getter methods for boolean attributes
|
64
|
+
def has_booleans(*attrs)
|
65
|
+
options = attrs.extract_options!
|
66
|
+
attrs.each do |attr|
|
67
|
+
attr_reader attr
|
68
|
+
define_reader_with_options(attr,options)
|
69
|
+
define_method("#{attr.to_s}=") do |val|
|
70
|
+
instance_variable_set("@#{attr}", val.to_s.to_b)
|
71
|
+
attributes[attr] = val
|
72
|
+
val
|
73
|
+
end
|
74
|
+
|
75
|
+
define_method ("#{attr.to_s}?") do
|
76
|
+
send("#{attr.to_s}".to_sym).to_s.to_b
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Creates setter and getter methods for integer attributes
|
82
|
+
def has_ints(*attrs)
|
83
|
+
options = attrs.extract_options!
|
84
|
+
attrs.each do |attr|
|
85
|
+
attr_accessor attr
|
86
|
+
define_reader_with_options(attr,options)
|
87
|
+
define_method("#{attr.to_s}=") do |val|
|
88
|
+
instance_variable_set("@#{attr}", val.to_i)
|
89
|
+
attributes[attr] = val
|
90
|
+
val
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
# Creates setter and getter methods for float attributes
|
96
|
+
def has_currency(*attrs)
|
97
|
+
options = attrs.extract_options!
|
98
|
+
attrs.each do |attr|
|
99
|
+
define_reader_with_options(attr,options)
|
100
|
+
define_method("#{attr.to_s}=") do |val|
|
101
|
+
instance_variable_set("@#{attr}", val.to_currency)
|
102
|
+
attributes[attr] = val
|
103
|
+
val
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
# Creates setter and getter methods for float attributes
|
108
|
+
def has_floats(*attrs)
|
109
|
+
options = attrs.extract_options!
|
110
|
+
attrs.each do |attr|
|
111
|
+
attr_reader attr
|
112
|
+
define_reader_with_options(attr,options)
|
113
|
+
|
114
|
+
define_method("#{attr.to_s}=") do |val|
|
115
|
+
instance_variable_set("@#{attr}", val.to_f)
|
116
|
+
attributes[attr] = val
|
117
|
+
val
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Creates setter and getter methods for date attributes
|
123
|
+
def has_dates(*attrs)
|
124
|
+
options = attrs.extract_options!
|
125
|
+
attrs.each do |attr|
|
126
|
+
attr_reader
|
127
|
+
define_reader_with_options(attr,options)
|
128
|
+
define_method("#{attr.to_s}=") do |val|
|
129
|
+
instance_variable_set("@#{attr}", val.to_date)
|
130
|
+
attributes[attr] = val
|
131
|
+
val
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Creates setter and getter methods for time attributes
|
137
|
+
def has_times(*attrs)
|
138
|
+
options = attrs.extract_options!
|
139
|
+
attrs.each do |attr|
|
140
|
+
attr_reader
|
141
|
+
define_reader_with_options(attr,options)
|
142
|
+
define_method("#{attr.to_s}=") do |val|
|
143
|
+
|
144
|
+
instance_variable_set("@#{attr}", val.to_time)
|
145
|
+
attributes[attr] = val
|
146
|
+
val
|
147
|
+
end
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
def fetch_alias_name(attr)
|
152
|
+
alias_name = (attr.to_s << "_old=").to_sym
|
153
|
+
self.module_eval("alias #{alias_name} #{attr}")
|
154
|
+
alias_name
|
155
|
+
end
|
156
|
+
|
157
|
+
# Defines a reader method that returns a default value if current value
|
158
|
+
# is nil, if :default is present in the options hash
|
159
|
+
def define_reader_with_options(attr,options)
|
160
|
+
unless options[:default].blank?
|
161
|
+
define_method (attr.to_s) do
|
162
|
+
val = instance_variable_get("@#{attr.to_s}")
|
163
|
+
val = options[:default] if val.nil?
|
164
|
+
val
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
@@ -0,0 +1,92 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
|
3
|
+
module Errors
|
4
|
+
def errors
|
5
|
+
@errors ||= ErrorsHash.new
|
6
|
+
@errors
|
7
|
+
end
|
8
|
+
|
9
|
+
|
10
|
+
def valid?
|
11
|
+
self.errors.clear if errors
|
12
|
+
validate
|
13
|
+
self.errors.blank? || self.errors.empty?
|
14
|
+
end
|
15
|
+
|
16
|
+
def errors?
|
17
|
+
!self.errors.nil? && !errors.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
|
21
|
+
def errors_on(attr)
|
22
|
+
self.valid?
|
23
|
+
[self.errors.on(attr.to_s)].flatten.compact
|
24
|
+
end
|
25
|
+
|
26
|
+
alias :error_on :errors_on
|
27
|
+
|
28
|
+
def errors_to_s
|
29
|
+
error_string = ""
|
30
|
+
self.errors.full_messages.each do |m|
|
31
|
+
error_string << "#{m} "
|
32
|
+
end
|
33
|
+
error_string
|
34
|
+
end
|
35
|
+
|
36
|
+
def validate
|
37
|
+
# Override to implement validation
|
38
|
+
end
|
39
|
+
|
40
|
+
def errors_for_flash
|
41
|
+
error_string = ""
|
42
|
+
errors.full_messages.each do |m|
|
43
|
+
error_string << "<div>#{m}</div>"
|
44
|
+
end
|
45
|
+
error_string
|
46
|
+
end
|
47
|
+
|
48
|
+
class ErrorsHash
|
49
|
+
attr_accessor :errors
|
50
|
+
def initialize
|
51
|
+
errors
|
52
|
+
end
|
53
|
+
|
54
|
+
def errors
|
55
|
+
@errors ||= {}
|
56
|
+
@errors
|
57
|
+
end
|
58
|
+
|
59
|
+
def clear
|
60
|
+
self.errors = {}
|
61
|
+
end
|
62
|
+
|
63
|
+
def add(attr, message)
|
64
|
+
errors[attr.to_s] ||= []
|
65
|
+
errors[attr.to_s] << message
|
66
|
+
end
|
67
|
+
def count
|
68
|
+
errors.length
|
69
|
+
end
|
70
|
+
def empty?
|
71
|
+
errors.empty?
|
72
|
+
end
|
73
|
+
def full_messages
|
74
|
+
full_messages = []
|
75
|
+
errors.each do |error|
|
76
|
+
error[1].each do |message|
|
77
|
+
full_messages << "#{error[0].titleize} #{message}"
|
78
|
+
end
|
79
|
+
end
|
80
|
+
full_messages
|
81
|
+
end
|
82
|
+
|
83
|
+
|
84
|
+
def on(attr)
|
85
|
+
errors[attr.to_s] if errors[attr.to_s]
|
86
|
+
end
|
87
|
+
|
88
|
+
alias :[] :on
|
89
|
+
end
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
@@ -0,0 +1,108 @@
|
|
1
|
+
module ExtendCore
|
2
|
+
require 'time'
|
3
|
+
require 'date'
|
4
|
+
Float.class_eval do
|
5
|
+
def round_to(precision)
|
6
|
+
split = precision.to_s.split(".")
|
7
|
+
mulitplier = 10.0 ** (split[1].length)
|
8
|
+
(((((self)*mulitplier).round).to_f)/mulitplier)
|
9
|
+
end
|
10
|
+
|
11
|
+
# Round to nearest cent
|
12
|
+
def to_currency
|
13
|
+
round_to(0.01)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
Object.class_eval do
|
18
|
+
# Borrowed from Rails activesupport/lib/active_support/core_ext/object/blank.rb
|
19
|
+
def blank?
|
20
|
+
respond_to?(:empty?) ? empty? : !self
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
|
25
|
+
#Extend Ruby Array.rb
|
26
|
+
Array.class_eval do
|
27
|
+
# Borrowed from Rails: activesupport/lib/active_support/core_ext/array/extract_options.rb
|
28
|
+
# Extracts options from a set of arguments. Removes and returns the last
|
29
|
+
# element in the array if it's a hash, otherwise returns a blank hash.
|
30
|
+
#
|
31
|
+
# def options(*args)
|
32
|
+
# args.extract_options!
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# options(1, 2) # => {}
|
36
|
+
# options(1, 2, :a => :b) # => {:a=>:b}
|
37
|
+
def extract_options!
|
38
|
+
last.is_a?(::Hash) ? pop : {}
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
#Extend Ruby String.rb
|
43
|
+
String.class_eval do
|
44
|
+
|
45
|
+
# to_b => to_boolean
|
46
|
+
def to_b
|
47
|
+
['1',"true", "t"].include?(self)
|
48
|
+
end
|
49
|
+
|
50
|
+
alias :to_boolean :to_b
|
51
|
+
|
52
|
+
|
53
|
+
def to_date
|
54
|
+
Date.parse(safe_datetime_string)
|
55
|
+
end
|
56
|
+
|
57
|
+
|
58
|
+
def to_time
|
59
|
+
Time.parse(safe_datetime_string)
|
60
|
+
end
|
61
|
+
|
62
|
+
# Takes known US formatted date/time strings (MM/DD/YYYY TIME) and converts
|
63
|
+
# them to international format (YYYY/MM/DD TIME)
|
64
|
+
#
|
65
|
+
# * safe_date_string("12/31/2010") # => '2010-12-31'
|
66
|
+
# * safe_date_string("12/31/2010T23:30:25") # => '2010-12-31T23:30:25'
|
67
|
+
# * safe_date_string("12/31/2010 23:30:25") # => '2010-12-31 23:30:25'
|
68
|
+
def safe_datetime_string
|
69
|
+
date = self
|
70
|
+
date_string = ""
|
71
|
+
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]/)
|
72
|
+
if date.include?("/")
|
73
|
+
split = date.split("/")
|
74
|
+
else
|
75
|
+
split = date.split("-")
|
76
|
+
end
|
77
|
+
time = ""
|
78
|
+
if split[2].length > 4
|
79
|
+
time = split[2][4..(split[2].length - 1)]
|
80
|
+
split[2] = split[2][0..3]
|
81
|
+
end
|
82
|
+
if split.length == 3 && split[2].length == 4
|
83
|
+
date_string << "#{split[2]}-#{split[0]}-#{split[1]}"
|
84
|
+
date_string << "#{time}" unless time.blank?
|
85
|
+
end
|
86
|
+
end
|
87
|
+
date_string = date if date_string.blank?
|
88
|
+
date_string
|
89
|
+
end
|
90
|
+
|
91
|
+
alias :old_to_f :to_f
|
92
|
+
# Remove none numeric characters the run default ruby float cast
|
93
|
+
def to_f
|
94
|
+
gsub(/[^0-9\.\+\-]/, '').old_to_f
|
95
|
+
end
|
96
|
+
|
97
|
+
def to_currency
|
98
|
+
to_f.to_currency
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
Fixnum.class_eval do
|
103
|
+
#Any value greater than 0 is true
|
104
|
+
def to_b
|
105
|
+
self > 0
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module SimpleModel
|
2
|
+
module Validation
|
3
|
+
def validates_presence_of(*attr_names)
|
4
|
+
options = attr_names.extract_options!
|
5
|
+
#set defaults
|
6
|
+
options[:message] = "cannot be blank." if options[:message].blank?
|
7
|
+
attr_names.each do |attr|
|
8
|
+
break if conditional?(options)
|
9
|
+
|
10
|
+
errors.add(attr, options[:message]) if send(attr).blank?
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def validates_format_of(*attr_names)
|
16
|
+
options = attr_names.extract_options!
|
17
|
+
#set defaults
|
18
|
+
options[:message] = "is an invalid format." if options[:message].blank?
|
19
|
+
|
20
|
+
attr_names.each do |attr|
|
21
|
+
break if conditional?(options)
|
22
|
+
|
23
|
+
method = send(attr)
|
24
|
+
unless method.blank?
|
25
|
+
errors.add(attr, options[:message]) unless method.to_s.match(options[:with])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
def validates_length_of(*attr_names)
|
30
|
+
options = attr_names.extract_options!
|
31
|
+
|
32
|
+
attr_names.each do |attr|
|
33
|
+
break if conditional?(options)
|
34
|
+
|
35
|
+
att_method = send(attr)
|
36
|
+
unless att_method.blank?
|
37
|
+
errors.add(attr,(options[:message].blank? ? "must equal #{options[:equal]} characters in length." : options[:message])) if options[:equal] && att_method.to_s.length != options[:equal]
|
38
|
+
errors.add(attr,(options[:message].blank? ? "cannot have more than #{options[:max]} characters in length." : options[:message])) if options[:max] && att_method.to_s.length > options[:max]
|
39
|
+
errors.add(attr,(options[:message].blank? ? "cannot have less than #{options[:min]} characters in length." : options[:message])) if options[:min] && att_method.to_s.length < options[:min]
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def validates_inclusion_of(*attr_names)
|
45
|
+
options = attr_names.extract_options!
|
46
|
+
|
47
|
+
first = options[:in].first
|
48
|
+
last = options[:in].last
|
49
|
+
options[:message] = "must be greater than or equal to #{first} and less than or equal to #{last}" if options[:message].blank?
|
50
|
+
attr_names.each do |attr|
|
51
|
+
break if conditional?(options)
|
52
|
+
attr_method = send(attr).to_f
|
53
|
+
unless attr_method.blank?
|
54
|
+
errors.add(attr,options[:message]) if attr_method < first || attr_method > last
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
def conditional?(options)
|
61
|
+
return true unless ((options[:if].blank? && options[:unless].blank?) ||
|
62
|
+
!options[:if].blank? && send(options[:if])) ||
|
63
|
+
(!options[:unless].blank? && !send(options[:unless]))
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "simple_model/version"
|
4
|
+
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "simple_model"
|
8
|
+
s.version = SimpleModel::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["Joshua T Mckinney"]
|
11
|
+
s.email = ["joshmckin@gmail.com"]
|
12
|
+
s.homepage = ""
|
13
|
+
s.summary = %q{Simpifies building tableless models or models backed by webservices}
|
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
|
+
|
16
|
+
s.add_development_dependency 'rspec', ' 1.3.1'
|
17
|
+
s.rubyforge_project = "simple_model"
|
18
|
+
|
19
|
+
s.files = `git ls-files`.split("\n")
|
20
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
21
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
22
|
+
s.require_paths = ["lib"]
|
23
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe SimpleModel::Attributes do
|
4
|
+
before(:all) do
|
5
|
+
class TestInit
|
6
|
+
include SimpleModel::Attributes
|
7
|
+
has_attributes :test1,:test2
|
8
|
+
end
|
9
|
+
@init = TestInit.new(:test1 => "1", :test2 => '2')
|
10
|
+
end
|
11
|
+
|
12
|
+
it "should set provided attributes on initialize" do
|
13
|
+
@init.test1.should eql("1")
|
14
|
+
@init.test2.should eql("2")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should include set attributes in attributes hash" do
|
18
|
+
@init.attributes.class.should eql(Hash)
|
19
|
+
@init.attributes[:test1].should eql("1")
|
20
|
+
@init.attributes[:test2].should eql("2")
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
describe SimpleModel::Attributes, 'define_reader_with_options' do
|
25
|
+
before(:all) do
|
26
|
+
class TestDefault
|
27
|
+
include SimpleModel::Attributes
|
28
|
+
attr_accessor :test
|
29
|
+
define_reader_with_options :test, :default => "test"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
it "should define setter method with default value" do
|
34
|
+
default = TestDefault.new
|
35
|
+
default.test.should eql("test")
|
36
|
+
end
|
37
|
+
it "should not intefer with setting" do
|
38
|
+
default = TestDefault.new
|
39
|
+
default.test = "New"
|
40
|
+
default.test.should eql("New")
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
describe SimpleModel::Attributes, 'has_booleans' do
|
45
|
+
before(:all) do
|
46
|
+
class TestBoolean
|
47
|
+
include SimpleModel::Attributes
|
48
|
+
has_booleans :test
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should add setter=, getter and getter? methods" do
|
53
|
+
methods = TestBoolean.new.methods
|
54
|
+
union = methods | [:test, :test=, :test?]
|
55
|
+
union.should eql(methods)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
|
2
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
|
+
describe ExtendCore, 'Float.rb' do
|
4
|
+
before(:all) do
|
5
|
+
include ExtendCore
|
6
|
+
end
|
7
|
+
describe ExtendCore, 'round_to' do
|
8
|
+
it "should return float rounded to specified precision" do
|
9
|
+
0.3333.round_to(0.01).should eql(0.33)
|
10
|
+
0.3333.round_to(0.001).should eql(0.333)
|
11
|
+
0.33335.round_to(0.0001).should eql(0.3334)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
describe ExtendCore, 'String.rb' do
|
18
|
+
before(:all) do
|
19
|
+
include ExtendCore
|
20
|
+
end
|
21
|
+
describe ExtendCore, 'safe_datetime_string' do
|
22
|
+
it "should set US formated datetime string to international" do
|
23
|
+
"12/31/2010".safe_datetime_string.should eql("2010-12-31")
|
24
|
+
"12/31/2010T23:31:59".safe_datetime_string.should eql("2010-12-31T23:31:59")
|
25
|
+
"12/31/2010 23:31:59".safe_datetime_string.should eql("2010-12-31 23:31:59")
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
describe ExtendCore, 'to_b' do
|
30
|
+
it "should return a Boolean" do
|
31
|
+
"1".to_b.class.should eql(TrueClass)
|
32
|
+
"".to_b.class.should eql(FalseClass)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return true if string is '1' or 't' or 'true'"do
|
36
|
+
['1','t','true'].each do |s|
|
37
|
+
s.to_b.should be_true
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
describe ExtendCore, 'to_date' do
|
43
|
+
it "return a Date object" do
|
44
|
+
"12/31/2010".to_date.class.should eql(Date)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
describe ExtendCore, 'to_date' do
|
48
|
+
it "return a Time object" do
|
49
|
+
"12/31/2010".to_time.class.should eql(Time)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
describe ExtendCore, 'to_f' do
|
53
|
+
it "return a Foat from a string that may contain non-numeric values" do
|
54
|
+
"$5,000.006".to_f.should eql(5000.006)
|
55
|
+
end
|
56
|
+
end
|
57
|
+
describe ExtendCore, 'to_currency' do
|
58
|
+
it "return a Foat from a string that may contain non-numeric values" do
|
59
|
+
"$5,000.006".to_currency.should eql(5000.01)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
end
|
64
|
+
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
+
|
3
|
+
describe SimpleModel do
|
4
|
+
it 'Should add a boolean setter' do
|
5
|
+
class TestStuff < SimpleModel::Base
|
6
|
+
has_booleans :test_boolean
|
7
|
+
end
|
8
|
+
TestStuff.new.methods.include?(:test_boolean).should be_true
|
9
|
+
#a.test.should be_false
|
10
|
+
end
|
11
|
+
it 'Should add a boolean setter' do
|
12
|
+
class TestStuff < SimpleModel::Base
|
13
|
+
has_booleans :test_boolean
|
14
|
+
end
|
15
|
+
t = TestStuff.new
|
16
|
+
t.methods.include?(:test_boolean).should be_true
|
17
|
+
t.test_boolean = true
|
18
|
+
t.test_boolean.should be_true
|
19
|
+
#a.test.should be_false
|
20
|
+
end
|
21
|
+
it 'Should add a error setter' do
|
22
|
+
class TestStuff < SimpleModel::Base
|
23
|
+
has_attributes :test_attr
|
24
|
+
end
|
25
|
+
a = TestStuff.new
|
26
|
+
a.errors.add(:test_attr, "test")
|
27
|
+
a.errors?.should be_true
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
describe SimpleModel::Errors do
|
32
|
+
it 'Should add a error setter' do
|
33
|
+
class TestError
|
34
|
+
include SimpleModel::Errors
|
35
|
+
attr_accessor :test_attr
|
36
|
+
end
|
37
|
+
a = TestError.new(self)
|
38
|
+
a.errors.add(:test_attr, "test")
|
39
|
+
a.errors?.should be_true
|
40
|
+
|
41
|
+
#a.test.should be_false
|
42
|
+
end
|
43
|
+
end
|
data/spec/spec.opts
ADDED
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'simple_model'
|
4
|
+
require 'spec'
|
5
|
+
|
6
|
+
|
7
|
+
# Requires supporting files with custom matchers and macros, etc,
|
8
|
+
# in ./support/ and its subdirectories.
|
9
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
10
|
+
|
11
|
+
Spec::Runner.configure do |config|
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Joshua T Mckinney
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-12-27 00:00:00 -06:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rspec
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - "="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 1
|
30
|
+
- 3
|
31
|
+
- 1
|
32
|
+
version: 1.3.1
|
33
|
+
type: :development
|
34
|
+
version_requirements: *id001
|
35
|
+
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.
|
36
|
+
email:
|
37
|
+
- joshmckin@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files: []
|
43
|
+
|
44
|
+
files:
|
45
|
+
- .autotest
|
46
|
+
- .gitignore
|
47
|
+
- Gemfile
|
48
|
+
- Rakefile
|
49
|
+
- lib/simple_model.rb
|
50
|
+
- lib/simple_model/attributes.rb
|
51
|
+
- lib/simple_model/errors.rb
|
52
|
+
- lib/simple_model/extend_core.rb
|
53
|
+
- lib/simple_model/validation.rb
|
54
|
+
- lib/simple_model/version.rb
|
55
|
+
- simple_model.gemspec
|
56
|
+
- spec/attributes_spec.rb
|
57
|
+
- spec/extend_core_spec.rb
|
58
|
+
- spec/simple_model_spec.rb
|
59
|
+
- spec/spec.opts
|
60
|
+
- spec/spec_helper.rb
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: ""
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options: []
|
67
|
+
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
none: false
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
segments:
|
76
|
+
- 0
|
77
|
+
version: "0"
|
78
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
none: false
|
80
|
+
requirements:
|
81
|
+
- - ">="
|
82
|
+
- !ruby/object:Gem::Version
|
83
|
+
segments:
|
84
|
+
- 0
|
85
|
+
version: "0"
|
86
|
+
requirements: []
|
87
|
+
|
88
|
+
rubyforge_project: simple_model
|
89
|
+
rubygems_version: 1.3.7
|
90
|
+
signing_key:
|
91
|
+
specification_version: 3
|
92
|
+
summary: Simpifies building tableless models or models backed by webservices
|
93
|
+
test_files:
|
94
|
+
- spec/attributes_spec.rb
|
95
|
+
- spec/extend_core_spec.rb
|
96
|
+
- spec/simple_model_spec.rb
|
97
|
+
- spec/spec.opts
|
98
|
+
- spec/spec_helper.rb
|