acts_as_configuration 0.0.1
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/.gitignore +2 -0
- data/Gemfile +4 -0
- data/README +164 -0
- data/README.markdown +164 -0
- data/Rakefile +1 -0
- data/acts_as_configuration.gemspec +24 -0
- data/lib/acts_as_configuration/version.rb +3 -0
- data/lib/acts_as_configuration.rb +336 -0
- metadata +64 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# Acts as configuration
|
2
|
+
|
3
|
+
This *acts_as* extension provides the capabilities for transform a *string* or *text* column to a configuration variable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
<code>gem install acts_as_configuration</code>
|
8
|
+
|
9
|
+
### Rails 3
|
10
|
+
Add in your Gemfile:
|
11
|
+
<code>gem 'acts_as_configuration', :git => 'git://github.com/anga/acts_as_configuration.git'</code>
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Only you need to add the next line in your model.
|
16
|
+
|
17
|
+
<code>acts_as_configuration :configuration</code>
|
18
|
+
|
19
|
+
For example:
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
acts_as_configuration :config
|
23
|
+
end
|
24
|
+
|
25
|
+
Now you can write your configuration like:
|
26
|
+
|
27
|
+
user.configuration.private_photos = true
|
28
|
+
user.configuration.subscribe_to_news = false
|
29
|
+
user.configuration.perfil_description = ''
|
30
|
+
user.save
|
31
|
+
|
32
|
+
### Columns configuration
|
33
|
+
|
34
|
+
You can configurate each column.
|
35
|
+
|
36
|
+
#### Set column type
|
37
|
+
|
38
|
+
You can set the type of the colum.
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
acts_as_configuration :config, :columns => {
|
42
|
+
:private_photos => {
|
43
|
+
:type => :boolean
|
44
|
+
}, :age => {
|
45
|
+
:type => :get_type
|
46
|
+
}, :perfil_description => {
|
47
|
+
:type => lambda {
|
48
|
+
String
|
49
|
+
}
|
50
|
+
}, :last_loggin => {
|
51
|
+
:type => Time.now.class
|
52
|
+
}, :subscribe_to_rss => :get_rss_config
|
53
|
+
}
|
54
|
+
|
55
|
+
protected
|
56
|
+
def get_type
|
57
|
+
Fixnum
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_rss_config
|
61
|
+
{
|
62
|
+
:type => :boolean
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
You can use any class, but if you need use boolean values, you must use :boolean.
|
68
|
+
|
69
|
+
#### Set default value
|
70
|
+
|
71
|
+
class User < ActiveRecord::Base
|
72
|
+
acts_as_configuration :config, :columns => {
|
73
|
+
:private_photos => {
|
74
|
+
:type => :boolean,
|
75
|
+
:default => true
|
76
|
+
}, :age => {
|
77
|
+
:type => :get_type,
|
78
|
+
:default => 1
|
79
|
+
}, :perfil_description => {
|
80
|
+
:type => lambda {
|
81
|
+
String
|
82
|
+
},
|
83
|
+
:default => :get_default_perfil_description
|
84
|
+
}, :last_loggin => {
|
85
|
+
:type => Time.now.class,
|
86
|
+
:default => lambda {
|
87
|
+
self.created_at.time
|
88
|
+
}
|
89
|
+
}, :subscribe_to_rss => :get_rss_config
|
90
|
+
}
|
91
|
+
|
92
|
+
protected
|
93
|
+
def get_type
|
94
|
+
Fixnum
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_rss_config
|
98
|
+
{
|
99
|
+
:type => :boolean,
|
100
|
+
:default => true
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_default_perfil_description
|
105
|
+
Description.where(:user_id => self.id).default
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
#### Set validation
|
110
|
+
class User < ActiveRecord::Base
|
111
|
+
acts_as_configuration :config, :columns => {
|
112
|
+
:private_photos => {
|
113
|
+
:type => :boolean,
|
114
|
+
:default => true
|
115
|
+
}, :age => {
|
116
|
+
:type => :get_type,
|
117
|
+
:default => 1,
|
118
|
+
:validate => lambda {
|
119
|
+
|age|
|
120
|
+
errors.add :config_age, "You are Matusalén?" if age > 150
|
121
|
+
errors.add :config_age, "You're a fetus?" if age <= 0
|
122
|
+
}
|
123
|
+
}, :perfil_description => {
|
124
|
+
:type => lambda {
|
125
|
+
String
|
126
|
+
},
|
127
|
+
:default => :get_default_perfil_description,
|
128
|
+
:lambda => :must_not_have_strong_language
|
129
|
+
}, :last_loggin => {
|
130
|
+
:type => Time.now.class,
|
131
|
+
:default => lambda {
|
132
|
+
self.created_at.time
|
133
|
+
},
|
134
|
+
:validate => lambda {
|
135
|
+
|time|
|
136
|
+
errors.add :config_last_loggin, "You can't loggin in the future" if time > Time.now
|
137
|
+
}
|
138
|
+
}, :subscribe_to_rss => :get_rss_config
|
139
|
+
}
|
140
|
+
|
141
|
+
protected
|
142
|
+
STRONG_WORD = [
|
143
|
+
#...
|
144
|
+
]
|
145
|
+
|
146
|
+
def get_type
|
147
|
+
Fixnum
|
148
|
+
end
|
149
|
+
|
150
|
+
def get_rss_config
|
151
|
+
{
|
152
|
+
:type => :boolean,
|
153
|
+
:default => true
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_default_perfil_description
|
158
|
+
Description.where(:user_id => self.id).default
|
159
|
+
end
|
160
|
+
|
161
|
+
def must_not_have_strong_language(desc)
|
162
|
+
errors.add :cofig_perfil_description, "You must not have strong language" if desc =~ /(#{STRONG_WORD.join('|')})/
|
163
|
+
end
|
164
|
+
end
|
data/README.markdown
ADDED
@@ -0,0 +1,164 @@
|
|
1
|
+
# Acts as configuration
|
2
|
+
|
3
|
+
This *acts_as* extension provides the capabilities for transform a *string* or *text* column to a configuration variable.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
<code>gem install acts_as_configuration</code>
|
8
|
+
|
9
|
+
### Rails 3
|
10
|
+
Add in your Gemfile:
|
11
|
+
<code>gem 'acts_as_configuration', :git => 'git://github.com/anga/acts_as_configuration.git'</code>
|
12
|
+
|
13
|
+
## Usage
|
14
|
+
|
15
|
+
Only you need to add the next line in your model.
|
16
|
+
|
17
|
+
<code>acts_as_configuration :configuration</code>
|
18
|
+
|
19
|
+
For example:
|
20
|
+
|
21
|
+
class User < ActiveRecord::Base
|
22
|
+
acts_as_configuration :config
|
23
|
+
end
|
24
|
+
|
25
|
+
Now you can write your configuration like:
|
26
|
+
|
27
|
+
user.configuration.private_photos = true
|
28
|
+
user.configuration.subscribe_to_news = false
|
29
|
+
user.configuration.perfil_description = ''
|
30
|
+
user.save
|
31
|
+
|
32
|
+
### Columns configuration
|
33
|
+
|
34
|
+
You can configurate each column.
|
35
|
+
|
36
|
+
#### Set column type
|
37
|
+
|
38
|
+
You can set the type of the colum.
|
39
|
+
|
40
|
+
class User < ActiveRecord::Base
|
41
|
+
acts_as_configuration :config, :columns => {
|
42
|
+
:private_photos => {
|
43
|
+
:type => :boolean
|
44
|
+
}, :age => {
|
45
|
+
:type => :get_type
|
46
|
+
}, :perfil_description => {
|
47
|
+
:type => lambda {
|
48
|
+
String
|
49
|
+
}
|
50
|
+
}, :last_loggin => {
|
51
|
+
:type => Time.now.class
|
52
|
+
}, :subscribe_to_rss => :get_rss_config
|
53
|
+
}
|
54
|
+
|
55
|
+
protected
|
56
|
+
def get_type
|
57
|
+
Fixnum
|
58
|
+
end
|
59
|
+
|
60
|
+
def get_rss_config
|
61
|
+
{
|
62
|
+
:type => :boolean
|
63
|
+
}
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
You can use any class, but if you need use boolean values, you must use :boolean.
|
68
|
+
|
69
|
+
#### Set default value
|
70
|
+
|
71
|
+
class User < ActiveRecord::Base
|
72
|
+
acts_as_configuration :config, :columns => {
|
73
|
+
:private_photos => {
|
74
|
+
:type => :boolean,
|
75
|
+
:default => true
|
76
|
+
}, :age => {
|
77
|
+
:type => :get_type,
|
78
|
+
:default => 1
|
79
|
+
}, :perfil_description => {
|
80
|
+
:type => lambda {
|
81
|
+
String
|
82
|
+
},
|
83
|
+
:default => :get_default_perfil_description
|
84
|
+
}, :last_loggin => {
|
85
|
+
:type => Time.now.class,
|
86
|
+
:default => lambda {
|
87
|
+
self.created_at.time
|
88
|
+
}
|
89
|
+
}, :subscribe_to_rss => :get_rss_config
|
90
|
+
}
|
91
|
+
|
92
|
+
protected
|
93
|
+
def get_type
|
94
|
+
Fixnum
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_rss_config
|
98
|
+
{
|
99
|
+
:type => :boolean,
|
100
|
+
:default => true
|
101
|
+
}
|
102
|
+
end
|
103
|
+
|
104
|
+
def get_default_perfil_description
|
105
|
+
Description.where(:user_id => self.id).default
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
#### Set validation
|
110
|
+
class User < ActiveRecord::Base
|
111
|
+
acts_as_configuration :config, :columns => {
|
112
|
+
:private_photos => {
|
113
|
+
:type => :boolean,
|
114
|
+
:default => true
|
115
|
+
}, :age => {
|
116
|
+
:type => :get_type,
|
117
|
+
:default => 1,
|
118
|
+
:validate => lambda {
|
119
|
+
|age|
|
120
|
+
errors.add :config_age, "You are Matusalén?" if age > 150
|
121
|
+
errors.add :config_age, "You're a fetus?" if age <= 0
|
122
|
+
}
|
123
|
+
}, :perfil_description => {
|
124
|
+
:type => lambda {
|
125
|
+
String
|
126
|
+
},
|
127
|
+
:default => :get_default_perfil_description,
|
128
|
+
:lambda => :must_not_have_strong_language
|
129
|
+
}, :last_loggin => {
|
130
|
+
:type => Time.now.class,
|
131
|
+
:default => lambda {
|
132
|
+
self.created_at.time
|
133
|
+
},
|
134
|
+
:validate => lambda {
|
135
|
+
|time|
|
136
|
+
errors.add :config_last_loggin, "You can't loggin in the future" if time > Time.now
|
137
|
+
}
|
138
|
+
}, :subscribe_to_rss => :get_rss_config
|
139
|
+
}
|
140
|
+
|
141
|
+
protected
|
142
|
+
STRONG_WORD = [
|
143
|
+
#...
|
144
|
+
]
|
145
|
+
|
146
|
+
def get_type
|
147
|
+
Fixnum
|
148
|
+
end
|
149
|
+
|
150
|
+
def get_rss_config
|
151
|
+
{
|
152
|
+
:type => :boolean,
|
153
|
+
:default => true
|
154
|
+
}
|
155
|
+
end
|
156
|
+
|
157
|
+
def get_default_perfil_description
|
158
|
+
Description.where(:user_id => self.id).default
|
159
|
+
end
|
160
|
+
|
161
|
+
def must_not_have_strong_language(desc)
|
162
|
+
errors.add :cofig_perfil_description, "You must not have strong language" if desc =~ /(#{STRONG_WORD.join('|')})/
|
163
|
+
end
|
164
|
+
end
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "acts_as_configuration/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "acts_as_configuration"
|
7
|
+
s.version = ActsAsConfiguration::VERSION
|
8
|
+
s.authors = ["Andrés B."]
|
9
|
+
s.email = ["andres.b.dev@gmail.com"]
|
10
|
+
s.homepage = ""
|
11
|
+
s.summary = %q{Make a variable to work like a configuration variable}
|
12
|
+
s.description = %q{Make a variable to work like a configuration variable}
|
13
|
+
|
14
|
+
s.rubyforge_project = "acts_as_configuration"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
# specify any dependencies here; for example:
|
22
|
+
s.add_development_dependency "rspec"
|
23
|
+
# s.add_runtime_dependency "rest-client"
|
24
|
+
end
|
@@ -0,0 +1,336 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
require "acts_as_configuration/version"
|
3
|
+
|
4
|
+
module ActsAsConfiguration
|
5
|
+
def self.included(base)
|
6
|
+
base.extend(ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
# The object how controll the data
|
10
|
+
class Configuration
|
11
|
+
def initialize(options={})
|
12
|
+
@model = options[:model]
|
13
|
+
@column_name = options[:column_name].to_s
|
14
|
+
@columns = expand_options options[:columns], { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }
|
15
|
+
@value = get_defaults_values options
|
16
|
+
|
17
|
+
raise "#{@column_name} should by text or string not #{options[:model].column_for_attribute(@column_name.to_sym).type}" if not [:text, :stiring].include? options[:model].column_for_attribute(@column_name.to_sym).type
|
18
|
+
|
19
|
+
out = YAML.parse(@model[@column_name].to_s)
|
20
|
+
if out == false
|
21
|
+
db_value = nil
|
22
|
+
else
|
23
|
+
db_value = out.to_ruby
|
24
|
+
end
|
25
|
+
@value.merge! db_value if db_value.kind_of? Hash
|
26
|
+
|
27
|
+
initialize_values
|
28
|
+
|
29
|
+
# Raise or not if fail?...
|
30
|
+
@model.attributes[@column_name] = @value
|
31
|
+
@model.save(:validate => false)
|
32
|
+
end
|
33
|
+
|
34
|
+
def [](key)
|
35
|
+
@value[key.to_s]
|
36
|
+
end
|
37
|
+
|
38
|
+
def []=(key, value)
|
39
|
+
if (@columns[key.to_sym][:type] == :boolean and (not [true.class, false.class].include? value.class)) or
|
40
|
+
((not [:boolean, nil].include?(@columns[key.to_sym][:type])) and @columns[key.to_sym][:type] != value.class )
|
41
|
+
raise "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}"
|
42
|
+
end
|
43
|
+
@value[key.to_s] = value
|
44
|
+
@model.send :"#{@column_name}=", @value.to_yaml
|
45
|
+
end
|
46
|
+
|
47
|
+
# The "magic" happen here.
|
48
|
+
# Use the undefined method as a column
|
49
|
+
def method_missing(m, *args, &block)
|
50
|
+
# If the method don't finish in "=" is fore read
|
51
|
+
if m !~ /\=$/
|
52
|
+
self[m.to_s]
|
53
|
+
# but if finish with "=" is for wirte
|
54
|
+
else
|
55
|
+
column_name = m.to_s.gsub(/\=$/, '')
|
56
|
+
self[column_name.to_s] = args.first
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def initialize_values
|
63
|
+
if not @value.kind_of? Hash
|
64
|
+
@model.attributes[@column_name] = {}.to_yaml
|
65
|
+
@model.save
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def get_defaults_values(options = {})
|
70
|
+
defaults_ = {}
|
71
|
+
options[:columns].each do |column, config|
|
72
|
+
defaults_[column.to_s] = @columns[column.to_sym][:default] || nil
|
73
|
+
end
|
74
|
+
defaults_
|
75
|
+
end
|
76
|
+
|
77
|
+
# Only we accept strings as key
|
78
|
+
def transform_defaults(hash)
|
79
|
+
_hash = {}
|
80
|
+
hash.each do |key, value|
|
81
|
+
_hash[key.to_s] = value
|
82
|
+
end
|
83
|
+
_hash
|
84
|
+
end
|
85
|
+
|
86
|
+
def expand_options(options={}, opts={})
|
87
|
+
config_opts = {
|
88
|
+
:not_expand => [],
|
89
|
+
:not_call_symbol => []
|
90
|
+
}.merge! opts
|
91
|
+
if options.kind_of? Hash
|
92
|
+
opts = {}
|
93
|
+
options.each do |column, config|
|
94
|
+
if not config_opts[:not_expand].include? column.to_sym
|
95
|
+
if not config_opts[:not_call_symbol].include? config
|
96
|
+
opts[column.to_sym] = expand_options(get_value_of(config), config_opts)
|
97
|
+
else
|
98
|
+
opts[column.to_sym] = expand_options(config, config_opts)
|
99
|
+
end
|
100
|
+
else
|
101
|
+
opts[column.to_sym] = config
|
102
|
+
end
|
103
|
+
end
|
104
|
+
return opts
|
105
|
+
else
|
106
|
+
return get_value_of options
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def get_value_of(value)
|
111
|
+
if value.kind_of? Symbol
|
112
|
+
# If the function exist, we execute it
|
113
|
+
if @model.respond_to? value
|
114
|
+
return @model.send value
|
115
|
+
# if the the function not exist, whe set te symbol as a value
|
116
|
+
else
|
117
|
+
return value
|
118
|
+
end
|
119
|
+
elsif value.kind_of? Proc
|
120
|
+
return value.call
|
121
|
+
else
|
122
|
+
return value
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
module ClassMethods
|
128
|
+
def acts_as_configuration(column_name, options = {})
|
129
|
+
cattr_accessor :aac_columns_config
|
130
|
+
self.aac_columns_config = self.aac_columns_config || {}
|
131
|
+
|
132
|
+
# Parse options
|
133
|
+
# Definir attr_accessor
|
134
|
+
# definir cattr_accessor
|
135
|
+
# Realizar un include que redefina el initialize
|
136
|
+
# Ver https://github.com/Caseproof/metafy/blob/master/lib/metafy/activerecord_methods.rb
|
137
|
+
|
138
|
+
# Para ver que hacer en el initialize ver:
|
139
|
+
# https://github.com/Caseproof/metafy/blob/master/lib/metafy/base.rb
|
140
|
+
|
141
|
+
assign_attributes_eval = "
|
142
|
+
def assign_attributes(attributes = nil, options = {})
|
143
|
+
attributes.each_pair do |key, value|
|
144
|
+
puts key
|
145
|
+
if key.to_s =~ /^#{column_name}_/
|
146
|
+
puts key
|
147
|
+
rb = \"#{column_name}.\#\{key.to_s.gsub(/^#{column_name}_/,'')\}\"
|
148
|
+
eval rb, binding
|
149
|
+
end
|
150
|
+
end
|
151
|
+
attributes.delete_if do |key,value|
|
152
|
+
key.to_s =~ /^#{column_name}_/
|
153
|
+
end
|
154
|
+
super attributes, options
|
155
|
+
end
|
156
|
+
"
|
157
|
+
|
158
|
+
class_eval <<-EOV
|
159
|
+
public
|
160
|
+
validate :aac_validations
|
161
|
+
|
162
|
+
puts "Evaluando de: \#\{self.name}"
|
163
|
+
(eval self.name).class_eval <<-EOS
|
164
|
+
def assign_attributes(attributes = nil, options = {})
|
165
|
+
attributes.each_pair do |key, value|
|
166
|
+
puts key
|
167
|
+
if key.to_s =~ /^#{column_name}_/
|
168
|
+
puts key
|
169
|
+
rb = \"#{column_name}.\#\{key.to_s.gsub(/^#{column_name}_/,'')\}\"
|
170
|
+
eval rb, binding
|
171
|
+
end
|
172
|
+
end
|
173
|
+
attributes.delete_if do |key,value|
|
174
|
+
key.to_s =~ /^#{column_name}_/
|
175
|
+
end
|
176
|
+
super attributes, options
|
177
|
+
end
|
178
|
+
EOS
|
179
|
+
|
180
|
+
def #{column_name.to_s}
|
181
|
+
if not @#{column_name.to_s}_configuration.kind_of? ActsAsConfiguration::Configuration
|
182
|
+
opts = initialize_options(#{options})
|
183
|
+
options = {
|
184
|
+
:extensible => true # If is false, only the columns defined in :columns can be used
|
185
|
+
}.merge! opts
|
186
|
+
columns = initialize_columns expand_options(options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] })
|
187
|
+
@#{column_name.to_s}_configuration ||= ActsAsConfiguration::Configuration.new({:model => self, :column_name => :#{column_name.to_s}, :columns => columns})
|
188
|
+
end
|
189
|
+
@#{column_name.to_s}_configuration
|
190
|
+
end
|
191
|
+
|
192
|
+
protected
|
193
|
+
def aac_validations
|
194
|
+
@aac_validation ||= {} if not @aac_validation.kind_of? Hash
|
195
|
+
@aac_validation.each do |column, validation|
|
196
|
+
if validation.kind_of? Symbol
|
197
|
+
self.send validation, eval("@#{column_name.to_s}_configuration.\#\{column.to_s\}")
|
198
|
+
elsif validation.kind_of? Proc
|
199
|
+
validation.call @#{column_name.to_s}_configuration[column.to_sym]
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
203
|
+
|
204
|
+
def initialize_options(options={})
|
205
|
+
opts = expand_options options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }
|
206
|
+
end
|
207
|
+
|
208
|
+
# Initialize each column configuration
|
209
|
+
def initialize_columns(options = {})
|
210
|
+
columns = {}
|
211
|
+
if options[:columns].kind_of? Hash
|
212
|
+
options[:columns].each do |column, config|
|
213
|
+
columns[column] = initialize_column column, config
|
214
|
+
end
|
215
|
+
elsif options[:columns].kind_of? Symbol
|
216
|
+
hash = self.send options[:columns]
|
217
|
+
raise "Invalid columns configuration" if not hash.kind_of? Hash
|
218
|
+
columns = initialize_columns :columns => hash
|
219
|
+
elsif options[:columns].kind_of? Proc
|
220
|
+
hash = options[:columns].call
|
221
|
+
raise "Invalid columns configuration" if not hash.kind_of? Hash
|
222
|
+
columns = initialize_columns :columns => hash
|
223
|
+
end
|
224
|
+
columns
|
225
|
+
end
|
226
|
+
|
227
|
+
def initialize_column(column,config={})
|
228
|
+
raise "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
|
229
|
+
column = column.to_sym
|
230
|
+
column_config = {}
|
231
|
+
|
232
|
+
# Stablish the type
|
233
|
+
if config[:type].class == Class
|
234
|
+
# If exist :type, is a static column
|
235
|
+
column_config[:type] = config[:type]
|
236
|
+
else
|
237
|
+
# if not, is a dynamic column
|
238
|
+
if config[:type].to_sym == :any
|
239
|
+
column_config[:type] = nil
|
240
|
+
elsif config[:type].to_sym == :boolean
|
241
|
+
column_config[:type] = :boolean
|
242
|
+
else
|
243
|
+
raise "\#\{config[:type]\} is not a valid column type"
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Stablish the default value
|
248
|
+
# if is a symbol, we execute the function from the model
|
249
|
+
if config[:default].kind_of? Symbol
|
250
|
+
column_config[:default] = self.send(:config[:default])
|
251
|
+
elsif config[:default].kind_of? Proc
|
252
|
+
column_config[:default] = config[:default].call
|
253
|
+
else
|
254
|
+
# If the column have a type, we verify the type
|
255
|
+
if not column_config[:type].nil?
|
256
|
+
if (column_config[:type] == :boolean and (not [true.class, false.class].include? config[:default].class)) or
|
257
|
+
((not [:boolean, nil].include?(column_config[:type])) and column_config[:type] != config[:default].class )
|
258
|
+
raise "The column \#\{column\} has an invalid default value. Expected \#\{column_config[:type]}, not \#\{config[:default].class}"
|
259
|
+
end
|
260
|
+
column_config[:default] = config[:default]
|
261
|
+
else
|
262
|
+
# If is dynamic, only we set the default value
|
263
|
+
column_config[:default] = config[:default]
|
264
|
+
end
|
265
|
+
end
|
266
|
+
|
267
|
+
# Set the validation
|
268
|
+
if [Symbol, Proc].include? config[:validate].class
|
269
|
+
column_config[:validate] = config[:validate]
|
270
|
+
create_validation_for column, config[:validate]
|
271
|
+
else
|
272
|
+
raise "The validation of \#\{column\} is invalid"
|
273
|
+
end
|
274
|
+
|
275
|
+
|
276
|
+
column_config
|
277
|
+
end
|
278
|
+
|
279
|
+
def create_validation_for(column, validation)
|
280
|
+
column = column.to_sym
|
281
|
+
@aac_validation ||= {}
|
282
|
+
@aac_validation[column] = validation
|
283
|
+
end
|
284
|
+
|
285
|
+
def expand_options(options={}, opts={})
|
286
|
+
config_opts = {
|
287
|
+
:not_expand => [],
|
288
|
+
:not_call_symbol => []
|
289
|
+
}.merge! opts
|
290
|
+
if options.kind_of? Hash
|
291
|
+
opts = {}
|
292
|
+
options.each do |column, config|
|
293
|
+
if not config_opts[:not_expand].include? column.to_sym
|
294
|
+
if not config_opts[:not_call_symbol].include? config
|
295
|
+
opts[column.to_sym] = expand_options(get_value_of(config), config_opts)
|
296
|
+
else
|
297
|
+
opts[column.to_sym] = expand_options(config, config_opts)
|
298
|
+
end
|
299
|
+
else
|
300
|
+
opts[column.to_sym] = config
|
301
|
+
end
|
302
|
+
end
|
303
|
+
return opts
|
304
|
+
else
|
305
|
+
return get_value_of options
|
306
|
+
end
|
307
|
+
end
|
308
|
+
|
309
|
+
def get_value_of(value)
|
310
|
+
if value.kind_of? Symbol
|
311
|
+
# If the function exist, we execute it
|
312
|
+
if self.respond_to? value
|
313
|
+
return self.send value
|
314
|
+
# if the the function not exist, whe set te symbol as a value
|
315
|
+
else
|
316
|
+
return value
|
317
|
+
end
|
318
|
+
elsif value.kind_of? Proc
|
319
|
+
return value.call
|
320
|
+
else
|
321
|
+
return value
|
322
|
+
end
|
323
|
+
end
|
324
|
+
EOV
|
325
|
+
|
326
|
+
end
|
327
|
+
|
328
|
+
|
329
|
+
|
330
|
+
protected
|
331
|
+
|
332
|
+
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
ActiveRecord::Base.class_eval { include ActsAsConfiguration }
|
metadata
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: acts_as_configuration
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Andrés B.
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-30 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &5405980 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *5405980
|
25
|
+
description: Make a variable to work like a configuration variable
|
26
|
+
email:
|
27
|
+
- andres.b.dev@gmail.com
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README
|
35
|
+
- README.markdown
|
36
|
+
- Rakefile
|
37
|
+
- acts_as_configuration.gemspec
|
38
|
+
- lib/acts_as_configuration.rb
|
39
|
+
- lib/acts_as_configuration/version.rb
|
40
|
+
homepage: ''
|
41
|
+
licenses: []
|
42
|
+
post_install_message:
|
43
|
+
rdoc_options: []
|
44
|
+
require_paths:
|
45
|
+
- lib
|
46
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
47
|
+
none: false
|
48
|
+
requirements:
|
49
|
+
- - ! '>='
|
50
|
+
- !ruby/object:Gem::Version
|
51
|
+
version: '0'
|
52
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
none: false
|
54
|
+
requirements:
|
55
|
+
- - ! '>='
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0'
|
58
|
+
requirements: []
|
59
|
+
rubyforge_project: acts_as_configuration
|
60
|
+
rubygems_version: 1.8.11
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Make a variable to work like a configuration variable
|
64
|
+
test_files: []
|