lib/extend_at.rb in extend_at-0.2.1 vs lib/extend_at.rb in extend_at-0.2.2

- old
+ new

@@ -17,15 +17,15 @@ end # The object how controll the data class Extention def initialize(options={}) - @configuration = ExtendModelAt::Configuration.run options, options[:model] - @model_manager = ::ExtendModelAt::ModelManager.new(@column_name, @configuration[:model], @configuration) + @configuration = ExtendModelAt::Configuration.new.run options, options[:model].clone + @model_manager = ::ExtendModelAt::ModelManager.new(@configuration[:column_name].to_s, options[:model], @configuration) @static = @configuration[:static] || false - @model = @configuration[:model] + @model = @configuration[:model].clone @column_name = @configuration[:column_name].to_s @columns = @configuration[:columns] @value = get_defaults_values @configuration # define_associations @@ -85,13 +85,42 @@ def configuration @configuration end -# def define_associationss -# end + def configuration=(value) + @configuration = value + end + def define_associationss + [:has_one, :has_many, :belongs_to].each do |relation| + eval <<-EOS + if @configuration.keys.include? :#{relation} + raise "Invalid #{relation} value" if not [Hash, Array, NilClass].include? @configuration[:#{relation}].class + # We nee an array of models, then, we + if @configuration[:#{relation}].kind_of? Hash + list_models = @configuration[:#{relation}].keys + elsif @configuration[:#{relation}].kind_of? Array + list_models = @configuration[:#{relation}] + else + list_models = [@configuration[:#{relation}]] + end + list_models.each do |model| + define_method(model.to_sym) do |force_reload| + if @configuration[:#{relation}].kind_of? Hash + config = @configuration[:#{relation}][model] + else + config = {} + end + @model_manager.read_#{relation} model, @configuration[:#{relation}][model], force_reload + end + end + end + EOS + end + end + def get_adapter(column, value) if @columns[column.to_sym][:type] == String return :to_s elsif @columns[column.to_sym][:type] == Fixnum return :to_i if value.respond_to? :to_i @@ -237,14 +266,13 @@ validate :extend_at_validations after_save :update_model_manager, :on => :create define_method(column_name.to_s) do if not @extend_at_configuration.kind_of? ExtendModelAt::Extention - options[:model] = self - @extend_at_configuration ||= ExtendModelAt::Extention.new(options ) - - initialize_columns @extend_at_configuration.send(:configuration)[:columns] if options.kind_of? Hash + options[:model] = self.clone + @extend_at_configuration = ExtendModelAt::Extention.new(options ) + initialize_columns @extend_at_configuration.send(:configuration)[:columns] || {} end @extend_at_configuration end protected @@ -259,15 +287,25 @@ instance_exec @extend_at_configuration[column.to_sym], &validation end end end + def set_extend_at_validation(value={}) + @extend_at_validation + end + + def update_model_manager + @extend_at_configuration.send :update_model_manager if @extend_at_configuration.respond_to? :update_model_manager + end + # Initialize each column configuration def initialize_columns(columns = {}) + colunms_config = {} columns.each do |column, config| - initialize_column column, config + colunms_config[column.to_sym] = initialize_column column, config end + colunms_config end def initialize_column(column,config={}) raise ExtendModelAt::ArgumentError, "The column \#\{column\} have an invalid configuration (\#\{config.class\} => \#\{config\})" if not config.kind_of? Hash @@ -303,11 +341,10 @@ create_validation_for column, config[:validate] elsif not config[:validate].nil? raise ExtendModelAt::ArgumentError, "The validation of \#\{column\} is invalid" end - column_config end def get_type_from_symbol(type) type = type.to_s @@ -320,19 +357,14 @@ return Date if type == 'date' or type == 'datetime' return eval type.classify end def create_validation_for(column, validation) - column = column.to_sym @extend_at_validation ||= {} @extend_at_validation[column] = validation end - def update_model_manager - @extend_at_configuration.send :update_model_manager if @extend_at_configuration.respond_to? :update_model_manager - end - def get_type_for_class(type) type = type.name return :any if type == 'NilClass' return :float if type == 'Float' return :integer if type == 'Fixnum' @@ -353,15 +385,15 @@ return true if [Date, Time, ActiveSupport::TimeWithZone].include? value.class and [:datetime, :timestamp].include? type false end def valid_type?(value, type) - type = type.to_s.to_sym - [:"", :any].include? type or - value.nil? or - (type == :boolean and ([true.class, false.class].include? value.class)) or - ((not [:boolean, nil].include?(type)) and not value.nil? and compatible_type(value, type)) - end + type = type.to_s.to_sym + [:"", :any].include? type or + value.nil? or + (type == :boolean and ([true.class, false.class].include? value.class)) or + ((not [:boolean, nil].include?(type)) and not value.nil? and compatible_type value, type ) + end end end end end \ No newline at end of file