has_properties 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/has_properties.rb +62 -0
- metadata +67 -0
@@ -0,0 +1,62 @@
|
|
1
|
+
module HasProperties
|
2
|
+
module SingletonMethods
|
3
|
+
def has_properties(options = {})
|
4
|
+
default_options = { :in => :data }
|
5
|
+
options = default_options.merge(options)
|
6
|
+
|
7
|
+
cattr_accessor :property_field
|
8
|
+
self.property_field = options[:in]
|
9
|
+
|
10
|
+
serialize self.property_field, Hash
|
11
|
+
|
12
|
+
extend ClassMethods
|
13
|
+
include InstanceMethods
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
module ClassMethods
|
18
|
+
def property(name, type, options = {})
|
19
|
+
define_method name do
|
20
|
+
convert_property(property_hash[name.to_s], type, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
define_method "#{name}=" do |value|
|
24
|
+
property_hash[name.to_s] = convert_property(value, type)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
module InstanceMethods
|
30
|
+
def property_hash
|
31
|
+
send("#{self.class.property_field}=", {}) unless send(self.class.property_field)
|
32
|
+
send(self.class.property_field)
|
33
|
+
end
|
34
|
+
|
35
|
+
def convert_property(value, type, options = {})
|
36
|
+
value = evaluate_property_default(options[:default]) if value.nil? && options[:default]
|
37
|
+
|
38
|
+
case type.to_sym
|
39
|
+
when :string
|
40
|
+
value.to_s
|
41
|
+
when :integer
|
42
|
+
value.to_i
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def evaluate_property_default(default)
|
47
|
+
case default
|
48
|
+
when Proc
|
49
|
+
default.bind(self).call
|
50
|
+
else
|
51
|
+
default
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def self.included(receiver)
|
57
|
+
receiver.extend SingletonMethods
|
58
|
+
receiver.send :include, InstanceMethods
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
ActiveRecord::Base.send :include, HasProperties
|
metadata
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: has_properties
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Thomas Kadauke
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-30 00:00:00 +02:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: Makes STI base classes with extensible attributes possible through serialization
|
23
|
+
email: tkadauke@imedo.de
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/has_properties.rb
|
32
|
+
has_rdoc: true
|
33
|
+
homepage:
|
34
|
+
licenses: []
|
35
|
+
|
36
|
+
post_install_message:
|
37
|
+
rdoc_options: []
|
38
|
+
|
39
|
+
require_paths:
|
40
|
+
- lib
|
41
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
42
|
+
none: false
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
hash: 3
|
47
|
+
segments:
|
48
|
+
- 0
|
49
|
+
version: "0"
|
50
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
hash: 3
|
56
|
+
segments:
|
57
|
+
- 0
|
58
|
+
version: "0"
|
59
|
+
requirements: []
|
60
|
+
|
61
|
+
rubyforge_project:
|
62
|
+
rubygems_version: 1.3.7
|
63
|
+
signing_key:
|
64
|
+
specification_version: 3
|
65
|
+
summary: Insert description here
|
66
|
+
test_files: []
|
67
|
+
|