Browse files

Support static columns wit :static => true option

  • Loading branch information...
1 parent cb1be8e commit fa77b68158378af3121a4618d23a03d1d03d82db @anga committed Jan 18, 2012
View
3 .gitignore
@@ -1,2 +1,3 @@
.directory
-*~
+*~
+*Gemfile.lock
View
32 lib/extend_at.rb
@@ -8,16 +8,24 @@ def self.included(base)
base.extend(ClassMethods)
end
+ # Errors
+ class InvalidColumn < Exception
+ end
+
+ class ArgumentError < Exception
+ end
+
# The object how controll the data
class Extention
def initialize(options={})
+ @static = options[:static] || false
@model = options[:model]
@column_name = options[:column_name].to_s
@columns = options[:columns]
@value = get_defaults_values options
@model_manager = ::ExtendModelAt::ModelManager.new(@column_name, @model, @columns)
- 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
+ raise ExtendModelAt::ArgumentError, "#{@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
initialize_values
end
@@ -30,7 +38,7 @@ def []=(key, value)
if not valid_type? value, @columns[key.to_sym].try(:[],:type)
# Try to adapt the value
adapter = get_adapter key, value
- raise "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}" if adapter.nil? # We can't adapt the value
+ raise ExtendModelAt::ArgumentError, "#{value.inspect} is not a valid type, expected #{@columns[key.to_sym][:type]}" if adapter.nil? # We can't adapt the value
value = value.send adapter
end
@value[key.to_s] = value
@@ -59,12 +67,13 @@ def all_hash
# Use the undefined method as a column
def method_missing(m, *args, &block)
+ column_name = m.to_s.gsub(/\=$/, '')
+ raise ExtendModelAt::InvalidColumn, "#{column_name} not exist" if @static == true and not (@columns.try(:keys).try(:include?, column_name.to_sym) )
# If the method don't finish with "=" is fore read
if m !~ /\=$/
self[m.to_s]
# but if finish with "=" is for wirte
else
- column_name = m.to_s.gsub(/\=$/, '')
self[column_name.to_s] = args.first
end
end
@@ -225,7 +234,7 @@ def search_in_extention(column, method, value)
:extensible => true # If is false, only the columns defined in :columns can be used
}.merge!(opts)
columns = initialize_columns expand_options(options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }) if options.kind_of? Hash
- @extend_at_configuration ||= ExtendModelAt::Extention.new({:model => self, :column_name => column_name.to_sym, :columns => columns})
+ @extend_at_configuration ||= ExtendModelAt::Extention.new({:model => self, :column_name => column_name.to_sym, :columns => columns, :static => options[:static]})
end
@extend_at_configuration
end
@@ -239,7 +248,8 @@ def extend_at_validations
if validation.kind_of? Symbol
self.send validation, eval("@extend_at_configuration.\#\{column.to_s\}", binding)
elsif validation.kind_of? Proc
- validation.call @extend_at_configuration[column.to_sym]
+ instance_exec @extend_at_configuration[column.to_sym], &validation
+# validation.call @extend_at_configuration[column.to_sym]
end
end
end
@@ -257,18 +267,18 @@ def initialize_columns(options = {})
end
elsif options[:columns].kind_of? Symbol
hash = self.send options[:columns]
- raise "Invalid columns configuration" if not hash.kind_of? Hash
+ raise ExtendModelAt::ArgumentError, "Invalid columns configuration" if not hash.kind_of? Hash
columns = initialize_columns :columns => hash
elsif options[:columns].kind_of? Proc
hash = options[:columns].call
- raise "Invalid columns configuration" if not hash.kind_of? Hash
+ raise ExtendModelAt::ArgumentError, "Invalid columns configuration" if not hash.kind_of? Hash
columns = initialize_columns :columns => hash
end
columns
end
def initialize_column(column,config={})
- raise "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
+ raise ExtendModelAt::ArgumentError, "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash
@VALID_SYMBOLS ||= [:any, :binary, :boolean, :date, :datetime, :decimal, :float, :integer, :string, :text, :time, :timestamp]
@@ -284,7 +294,7 @@ def initialize_column(column,config={})
elsif [Symbol, Proc].include? config[:type]
column_config[:type] = get_value_of config[:type]
else
- raise "\#\{config[:type]\} is not a valid column type"
+ raise ExtendModelAt::ArgumentError, "\#\{config[:type]\} is not a valid column type"
end
# Stablish the default value
@@ -297,7 +307,7 @@ def initialize_column(column,config={})
# If the column have a type, we verify the type
if not column_config[:type].nil?
if not valid_type?(config[:default], column_config[:type])
- raise "The column \#\{column\} has an invalid default value. Expected \#\{column_config[:type]}, not \#\{config[:default].class}"
+ raise ExtendModelAt::ArgumentError, "The column \#\{column\} has an invalid default value. Expected \#\{column_config[:type]}, not \#\{config[:default].class}"
end
column_config[:default] = config[:default]
else
@@ -311,7 +321,7 @@ def initialize_column(column,config={})
column_config[:validate] = config[:validate]
create_validation_for column, config[:validate]
elsif not config[:validate].nil?
- raise "The validation of \#\{column\} is invalid"
+ raise ExtendModelAt::ArgumentError, "The validation of \#\{column\} is invalid"
end
View
2 lib/extend_at/version.rb
@@ -1,3 +1,3 @@
module ExtendModelAt
- VERSION = "0.1.5"
+ VERSION = "0.2.0"
end
View
136 spec/app/Gemfile.lock
@@ -1,136 +0,0 @@
-PATH
- remote: ../../
- specs:
- extend_at (0.1.4)
- rails (~> 3.1)
-
-GEM
- remote: http://rubygems.org/
- specs:
- actionmailer (3.1.3)
- actionpack (= 3.1.3)
- mail (~> 2.3.0)
- actionpack (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- builder (~> 3.0.0)
- erubis (~> 2.7.0)
- i18n (~> 0.6)
- rack (~> 1.3.5)
- rack-cache (~> 1.1)
- rack-mount (~> 0.8.2)
- rack-test (~> 0.6.1)
- sprockets (~> 2.0.3)
- activemodel (3.1.3)
- activesupport (= 3.1.3)
- builder (~> 3.0.0)
- i18n (~> 0.6)
- activerecord (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- arel (~> 2.2.1)
- tzinfo (~> 0.3.29)
- activeresource (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- activesupport (3.1.3)
- multi_json (~> 1.0)
- ansi (1.4.1)
- arel (2.2.1)
- builder (3.0.0)
- coffee-rails (3.1.1)
- coffee-script (>= 2.2.0)
- railties (~> 3.1.0)
- coffee-script (2.2.0)
- coffee-script-source
- execjs
- coffee-script-source (1.2.0)
- diff-lcs (1.1.3)
- erubis (2.7.0)
- execjs (1.2.13)
- multi_json (~> 1.0)
- hike (1.2.1)
- i18n (0.6.0)
- jquery-rails (1.0.19)
- railties (~> 3.0)
- thor (~> 0.14)
- json (1.6.4)
- mail (2.3.0)
- i18n (>= 0.4.0)
- mime-types (~> 1.16)
- treetop (~> 1.4.8)
- mime-types (1.17.2)
- multi_json (1.0.4)
- polyglot (0.3.3)
- rack (1.3.6)
- rack-cache (1.1)
- rack (>= 0.4)
- rack-mount (0.8.3)
- rack (>= 1.0.0)
- rack-ssl (1.3.2)
- rack
- rack-test (0.6.1)
- rack (>= 1.0)
- rails (3.1.3)
- actionmailer (= 3.1.3)
- actionpack (= 3.1.3)
- activerecord (= 3.1.3)
- activeresource (= 3.1.3)
- activesupport (= 3.1.3)
- bundler (~> 1.0)
- railties (= 3.1.3)
- railties (3.1.3)
- actionpack (= 3.1.3)
- activesupport (= 3.1.3)
- rack-ssl (~> 1.3.2)
- rake (>= 0.8.7)
- rdoc (~> 3.4)
- thor (~> 0.14.6)
- rake (0.9.2.2)
- rdoc (3.12)
- json (~> 1.4)
- rspec (2.8.0)
- rspec-core (~> 2.8.0)
- rspec-expectations (~> 2.8.0)
- rspec-mocks (~> 2.8.0)
- rspec-core (2.8.0)
- rspec-expectations (2.8.0)
- diff-lcs (~> 1.1.2)
- rspec-mocks (2.8.0)
- sass (3.1.12)
- sass-rails (3.1.5)
- actionpack (~> 3.1.0)
- railties (~> 3.1.0)
- sass (~> 3.1.10)
- tilt (~> 1.3.2)
- sprockets (2.0.3)
- hike (~> 1.2)
- rack (~> 1.0)
- tilt (~> 1.1, != 1.3.0)
- sqlite3 (1.3.5)
- thor (0.14.6)
- tilt (1.3.3)
- treetop (1.4.10)
- polyglot
- polyglot (>= 0.3.1)
- turn (0.8.3)
- ansi
- tzinfo (0.3.31)
- uglifier (1.2.1)
- execjs (>= 0.3.0)
- multi_json (>= 1.0.2)
-
-PLATFORMS
- ruby
-
-DEPENDENCIES
- coffee-rails (~> 3.1)
- extend_at!
- jquery-rails
- rails (~> 3.1)
- rspec
- rspec-core
- sass-rails (~> 3.1)
- sqlite3
- turn
- uglifier
View
2 spec/app/app/models/article.rb
@@ -8,7 +8,7 @@ class Article < ActiveRecord::Base
:type => :get_int1_type,
:default => 1,
:validate => lambda do |value|
- errors.add :extra_int1, "Most by greater than 0" if value <= 0
+ self.errors.add :extra_int1, "Most by greater than 0" if value <= 0
end
},
:int2 => :get_int2_config
View
2 spec/app/app/models/user.rb
@@ -12,5 +12,5 @@ class User < ActiveRecord::Base
:type => Time
}
}
- end)
+ end), :static => true
end
View
7 spec/app/config/database.yml
@@ -7,6 +7,13 @@
# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+ pool: 5
+ timeout: 5000
+
+
test:
adapter: sqlite3
database: db/test.sqlite3
View
19 spec/extend_at_spec.rb
@@ -56,5 +56,24 @@
article.extra.int2 = 10
article.extra.int2.class.should == Fixnum
end
+
+ it "should accept validate" do
+ article = Article.new :extra_int1 => -1
+ article.save
+ article.errors.keys.include?(:extra_int1).should == true
+ end
+
+ it "should support static columns" do
+ user = User.new :name => "Account"
+ lambda {user.private_info.etc = "etc"}.should raise_error(ExtendModelAt::InvalidColumn)
+ end
+
+ it "if is't static should support dynamic columns" do
+ article = Article.new
+ lambda {article.extra.etc = "etc"}.should_not raise_error(ExtendModelAt::InvalidColumn)
+ article.save :validate => false
+ article.reload
+ article.extra.etc.should == "etc"
+ end
end
end

0 comments on commit fa77b68

Please sign in to comment.