simple_model 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +4 -0
- data/lib/simple_model/attributes.rb +16 -12
- data/lib/simple_model/errors.rb +1 -1
- data/lib/simple_model/version.rb +1 -1
- data/lib/simple_model.rb +15 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -1,4 +1,8 @@
|
|
1
1
|
# SimpleModel
|
2
|
+
A collection of convenience methods for building table-less models. If ActiveModel
|
3
|
+
gem is installed, SimpleModel::Based will include ActiveModel::Validations,
|
4
|
+
include ActiveModel::Conversion and extend ActiveModel::Naming. If ActiveModel
|
5
|
+
gem is not available, SimpleModel::Base defaults to its own built-in Error and Validation modules.
|
2
6
|
|
3
7
|
## Installation
|
4
8
|
|
@@ -1,16 +1,12 @@
|
|
1
1
|
module SimpleModel
|
2
2
|
module Attributes
|
3
3
|
include ExtendCore
|
4
|
-
|
5
4
|
|
6
5
|
#Set attribute values to those supplied at initialization
|
7
6
|
def initialize(*attrs)
|
8
|
-
attrs.extract_options
|
9
|
-
self.send(("#{attr[0]}=").to_sym, attr[1])
|
10
|
-
end
|
7
|
+
set_attributes(attrs.extract_options!)
|
11
8
|
end
|
12
9
|
|
13
|
-
############## Pseudo Rails Methods ###############
|
14
10
|
def id
|
15
11
|
@id
|
16
12
|
end
|
@@ -19,29 +15,37 @@ module SimpleModel
|
|
19
15
|
@id=id
|
20
16
|
end
|
21
17
|
|
22
|
-
# Defaults to true so rails will create a "new" form
|
23
|
-
# Set to false and rails will produce an "edit" form
|
24
18
|
def new_record
|
25
19
|
@new_record = true if @new_record.nil?
|
26
20
|
@new_record
|
27
21
|
end
|
28
22
|
|
23
|
+
def persisted?
|
24
|
+
!new_record
|
25
|
+
end
|
26
|
+
|
29
27
|
def new_record?
|
30
28
|
new_record
|
31
29
|
end
|
32
30
|
|
33
31
|
def new_record=(new_record)
|
34
|
-
@new_record = new_record
|
32
|
+
@new_record = new_record
|
35
33
|
end
|
36
34
|
|
37
|
-
############### End Pseudo Rails Methods ##############
|
38
|
-
|
39
35
|
# Place to store set attributes and their values
|
40
36
|
def attributes
|
41
37
|
@attributes ||= {}
|
42
38
|
@attributes
|
43
39
|
end
|
44
40
|
|
41
|
+
def set_attributes(attrs)
|
42
|
+
attrs.each do |attr|
|
43
|
+
self.send("#{attr[0].to_sym}=",attr[1])
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
alias :update_attributes :set_attributes
|
48
|
+
|
45
49
|
|
46
50
|
def self.included(base)
|
47
51
|
base.extend(ClassMethods)
|
@@ -164,9 +168,9 @@ module SimpleModel
|
|
164
168
|
|
165
169
|
def fetch_alias_name(attr)
|
166
170
|
alias_name = (attr.to_s << "_old=").to_sym
|
167
|
-
|
171
|
+
self.module_eval("alias #{alias_name} #{attr}")
|
168
172
|
|
169
|
-
|
173
|
+
alias_name
|
170
174
|
end
|
171
175
|
|
172
176
|
# Defines a reader method that returns a default value if current value
|
data/lib/simple_model/errors.rb
CHANGED
data/lib/simple_model/version.rb
CHANGED
data/lib/simple_model.rb
CHANGED
@@ -5,9 +5,22 @@ module SimpleModel
|
|
5
5
|
autoload :Errors, "simple_model/errors"
|
6
6
|
autoload :Validation, "simple_model/validation"
|
7
7
|
|
8
|
+
# require active_model if available
|
9
|
+
begin
|
10
|
+
require 'active_model'
|
11
|
+
rescue LoadError
|
12
|
+
#Don't fail
|
13
|
+
end
|
14
|
+
|
8
15
|
class Base
|
9
|
-
include SimpleModel::Errors
|
10
16
|
include SimpleModel::Attributes
|
11
|
-
|
17
|
+
begin
|
18
|
+
include ActiveModel::Validations
|
19
|
+
include ActiveModel::Conversion
|
20
|
+
extend ActiveModel::Naming
|
21
|
+
rescue NameError
|
22
|
+
include SimpleModel::Errors
|
23
|
+
include SimpleModel::Validation
|
24
|
+
end
|
12
25
|
end
|
13
26
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 3
|
9
|
+
version: 0.1.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joshua T Mckinney
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-01 00:00:00 -06:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|