app_attributes 0.5.2 → 0.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.md +15 -19
- data/lib/app_attributes.rb +18 -6
- data/lib/app_attributes/version.rb +1 -1
- metadata +5 -5
data/README.md
CHANGED
@@ -2,42 +2,38 @@
|
|
2
2
|
|
3
3
|
A very simple polymorphic implementation of name-value pairs for any ActiveRecord model. The value is serialized to allow storage of ruby objects.
|
4
4
|
|
5
|
-
|
5
|
+
## Install
|
6
6
|
|
7
|
-
|
8
|
-
sudo gem install app_attributes
|
9
|
-
</pre>
|
7
|
+
sudo gem install app_attributes
|
10
8
|
|
11
9
|
OR
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
bundle install
|
16
|
-
</pre>
|
11
|
+
gem 'app_attributes'
|
12
|
+
bundle install
|
17
13
|
|
18
|
-
|
14
|
+
## Usage
|
19
15
|
|
20
16
|
in your model:
|
21
17
|
|
22
|
-
|
23
|
-
|
24
|
-
include AppAttributes
|
18
|
+
class Author < ActiveRecord::Base
|
19
|
+
include AppAttributes
|
25
20
|
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
21
|
+
ext_attribute :hat_size, 10, :to_i
|
22
|
+
# args: attribute name, default value, casting method for returned value
|
23
|
+
# provides @author.hat_size, @author.hat_size=, @author.hat_size?
|
24
|
+
# returns value cast as integer
|
25
|
+
end
|
30
26
|
|
31
|
-
|
27
|
+
## TODO
|
32
28
|
|
33
29
|
* automate db migration. currently, you must manually copy/rename migration file in app_attributes/db/migrate
|
34
30
|
* build out tests likely as a nested Rails app
|
35
31
|
|
36
|
-
|
32
|
+
## Copyright and license
|
37
33
|
|
38
34
|
Copyright (c) 2011 Luke Wendling, released under the New BSD License
|
39
35
|
|
40
|
-
Contributors:
|
36
|
+
### Contributors:
|
41
37
|
|
42
38
|
* Jeff Pihl
|
43
39
|
* Scott Robertson
|
data/lib/app_attributes.rb
CHANGED
@@ -10,18 +10,28 @@ module AppAttributes
|
|
10
10
|
|
11
11
|
# Example:
|
12
12
|
# include AppAttributes
|
13
|
-
# ext_attribute :
|
14
|
-
# ext_attribute :
|
13
|
+
# ext_attribute :some_numeric_value, 0, :to_i
|
14
|
+
# ext_attribute :some_string_value, 'none'
|
15
15
|
#
|
16
16
|
module ClassMethods
|
17
|
-
def ext_attribute(name, default_value=nil)
|
17
|
+
def ext_attribute(name, default_value=nil, casting_method=nil)
|
18
18
|
define_method("#{name}") do
|
19
|
-
if self.new_record?
|
19
|
+
if self.new_record? # search in-memory
|
20
20
|
app_attr = app_attributes.detect{|oa| oa.name == "#{name}"}
|
21
21
|
else
|
22
|
-
|
22
|
+
if app_attributes.empty? # try the db again
|
23
|
+
app_attr = app_attributes.find(:first, :conditions => {:name => "#{name}"})
|
24
|
+
else # search in-memory
|
25
|
+
app_attr = app_attributes.detect{|oa| oa.name == "#{name}"}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
ret_val = (app_attr && app_attr.value) || default_value
|
29
|
+
# cast value, if requested
|
30
|
+
if ret_val
|
31
|
+
casting_method ? ret_val.send(casting_method) : ret_val
|
32
|
+
else
|
33
|
+
ret_val #nil
|
23
34
|
end
|
24
|
-
(app_attr && app_attr.value) || default_value
|
25
35
|
end
|
26
36
|
|
27
37
|
define_method("#{name}?") do
|
@@ -29,6 +39,7 @@ module AppAttributes
|
|
29
39
|
end
|
30
40
|
|
31
41
|
define_method("#{name}=") do |raw_value|
|
42
|
+
# handle input of boolean false, converting to 'false'
|
32
43
|
new_value = raw_value == false ? 'false' : (raw_value.blank? ? default_value : raw_value)
|
33
44
|
if self.new_record?
|
34
45
|
return if new_value.blank? # fail silently if no default value and new value is blank
|
@@ -39,6 +50,7 @@ module AppAttributes
|
|
39
50
|
app_attr = app_attributes.find_or_create_by_name("#{name}")
|
40
51
|
# new_value = '-' if new_value.blank? # validation prevents blank value
|
41
52
|
app_attr.update_attributes(:value => new_value)
|
53
|
+
app_attributes(true) # reload in-memory objects
|
42
54
|
end
|
43
55
|
return app_attr.value
|
44
56
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: app_attributes
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 7
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 6
|
9
|
+
- 0
|
10
|
+
version: 0.6.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Luke Wendling
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-11-10 00:00:00 -06:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|