property_sets 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/property_sets/active_record_extension.rb +27 -1
- data/lib/property_sets/property_set_model.rb +4 -16
- data/property_sets.gemspec +2 -2
- data/test/test_property_sets.rb +2 -2
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.9
|
@@ -1,5 +1,30 @@
|
|
1
|
+
require 'delegate'
|
2
|
+
|
1
3
|
module PropertySets
|
2
4
|
module ActiveRecordExtension
|
5
|
+
|
6
|
+
class PropertySetProxy < Delegator
|
7
|
+
attr_accessor :record
|
8
|
+
|
9
|
+
def initialize(record)
|
10
|
+
self.record = record
|
11
|
+
end
|
12
|
+
|
13
|
+
def __getobj__
|
14
|
+
record
|
15
|
+
end
|
16
|
+
|
17
|
+
def id
|
18
|
+
record.id
|
19
|
+
end
|
20
|
+
|
21
|
+
def create(args = {})
|
22
|
+
record.attributes = args
|
23
|
+
record.save
|
24
|
+
record
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
3
28
|
module ClassMethods
|
4
29
|
def property_set(association, &block)
|
5
30
|
raise "Invalid association name, letters only" unless association.to_s =~ /[a-z]+/
|
@@ -40,7 +65,8 @@ module PropertySets
|
|
40
65
|
# The finder method which returns the property if present, otherwise a new instance with defaults
|
41
66
|
define_method "lookup" do |arg|
|
42
67
|
instance = detect { |property| property.name.to_sym == arg }
|
43
|
-
instance ||= property_class.new(@owner.class.name.underscore.to_sym => @owner, :name => arg.to_s
|
68
|
+
instance ||= property_class.new(@owner.class.name.underscore.to_sym => @owner, :name => arg.to_s)
|
69
|
+
PropertySetProxy.new(instance)
|
44
70
|
end
|
45
71
|
end
|
46
72
|
end
|
@@ -2,18 +2,6 @@ module PropertySets
|
|
2
2
|
module PropertySetModel
|
3
3
|
module InstanceMethods
|
4
4
|
|
5
|
-
def create
|
6
|
-
self.value = self.class.default(name.to_sym) if self.value.nil?
|
7
|
-
if new_record?
|
8
|
-
super
|
9
|
-
end
|
10
|
-
self
|
11
|
-
end
|
12
|
-
|
13
|
-
def protected?
|
14
|
-
self.class.protected?(self.name.to_sym)
|
15
|
-
end
|
16
|
-
|
17
5
|
def false?
|
18
6
|
[ "false", "0", "", "off", "n" ].member?(value.to_s.downcase)
|
19
7
|
end
|
@@ -22,6 +10,10 @@ module PropertySets
|
|
22
10
|
!false?
|
23
11
|
end
|
24
12
|
|
13
|
+
def value
|
14
|
+
read_attribute(:value) || self.class.default(name.to_sym)
|
15
|
+
end
|
16
|
+
|
25
17
|
def to_s
|
26
18
|
value.to_s
|
27
19
|
end
|
@@ -65,10 +57,6 @@ module PropertySets
|
|
65
57
|
@properties.keys
|
66
58
|
end
|
67
59
|
|
68
|
-
def protected?(key)
|
69
|
-
!!@properties[key][:protected]
|
70
|
-
end
|
71
|
-
|
72
60
|
def default(key)
|
73
61
|
@properties[key] && @properties[key].key?(:default) ? @properties[key][:default] : nil
|
74
62
|
end
|
data/property_sets.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{property_sets}
|
8
|
-
s.version = "0.0.
|
8
|
+
s.version = "0.0.9"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Morten Primdahl"]
|
12
|
-
s.date = %q{2011-01-
|
12
|
+
s.date = %q{2011-01-13}
|
13
13
|
s.description = %q{This gem is an ActiveRecord extension which provides a convenient interface for managing per row properties}
|
14
14
|
s.email = %q{morten@zendesk.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/test/test_property_sets.rb
CHANGED
@@ -6,7 +6,7 @@ class Account < ActiveRecord::Base
|
|
6
6
|
property :bar
|
7
7
|
property :baz
|
8
8
|
property :hep, :default => 'skep'
|
9
|
-
property :bob
|
9
|
+
property :bob
|
10
10
|
end
|
11
11
|
|
12
12
|
property_set :texts do
|
@@ -81,7 +81,7 @@ class TestPropertySets < ActiveSupport::TestCase
|
|
81
81
|
@account.settings.foo.destroy
|
82
82
|
@account.reload
|
83
83
|
assert @account.settings.foo.new_record?
|
84
|
-
assert @account.settings.foo.
|
84
|
+
assert @account.settings.foo.create(:value => 8)
|
85
85
|
assert @account.settings.foo.id
|
86
86
|
assert @account.settings.foo.value == "8"
|
87
87
|
assert @account.settings.hep.create
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: property_sets
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Morten Primdahl
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-01-
|
18
|
+
date: 2011-01-13 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|