Module: ActsAsConfiguration::ClassMethods

Defined in:
lib/acts_as_configuration.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_configuration(column_name, options = {}) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# File 'lib/acts_as_configuration.rb', line 128

def acts_as_configuration(column_name, options = {})
  cattr_accessor :aac_columns_config
  self.aac_columns_config = self.aac_columns_config || {}

  # Parse options
  # Definir attr_accessor
  # definir cattr_accessor
  # Realizar un include que redefina el initialize
  # Ver https://github.com/Caseproof/metafy/blob/master/lib/metafy/activerecord_methods.rb

  # Para ver que hacer en el initialize ver:
  # https://github.com/Caseproof/metafy/blob/master/lib/metafy/base.rb

  assign_attributes_eval = "
  def assign_attributes(attributes = nil, options = {})
    attributes.each_pair do |key, value|
      puts key
      if key.to_s =~ /^#{column_name}_/
        puts key
        rb = \"#{column_name}.\#\{key.to_s.gsub(/^#{column_name}_/,'')\}\"
        eval rb, binding
      end
    end
    attributes.delete_if do |key,value|
      key.to_s =~ /^#{column_name}_/
    end
    super attributes, options
  end
  "
  
  class_eval "  public\n    validate :aac_validations\n\n    puts \"Evaluando de: \\#\\{self.name}\"\n    (eval self.name).class_eval <<-EOS\n      def assign_attributes(attributes = nil, options = {})\n        attributes.each_pair do |key, value|\n          puts key\n          if key.to_s =~ /^\#{column_name}_/\n            puts key\n            rb = \\\"\#{column_name}.\\#\\{key.to_s.gsub(/^\#{column_name}_/,'')\\}\\\"\n            eval rb, binding\n          end\n        end\n        attributes.delete_if do |key,value|\n          key.to_s =~ /^\#{column_name}_/\n        end\n        super attributes, options\n      end\n    EOS\n\n    def \#{column_name.to_s}\n      if not @\#{column_name.to_s}_configuration.kind_of? ActsAsConfiguration::Configuration\n        opts = initialize_options(\#{options})\n        options = {\n            :extensible => true    # If is false, only the columns defined in :columns can be used\n          }.merge! opts\n        columns = initialize_columns expand_options(options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] })\n        @\#{column_name.to_s}_configuration ||= ActsAsConfiguration::Configuration.new({:model => self, :column_name => :\#{column_name.to_s}, :columns => columns})\n      end\n      @\#{column_name.to_s}_configuration\n    end\n\n  protected\n    def aac_validations\n      @aac_validation ||= {} if not @aac_validation.kind_of? Hash\n      @aac_validation.each do |column, validation|\n        if validation.kind_of? Symbol\n          self.send validation, eval(\"@\#{column_name.to_s}_configuration.\\#\\{column.to_s\\}\")\n        elsif validation.kind_of? Proc\n          validation.call @\#{column_name.to_s}_configuration[column.to_sym]\n        end\n      end\n    end\n\n    def initialize_options(options={})\n      opts = expand_options options, { :not_call_symbol => [:boolean], :not_expand => [:validate, :default] }\n    end\n\n    # Initialize each column configuration\n    def initialize_columns(options = {})\n      columns = {}\n      if options[:columns].kind_of? Hash\n        options[:columns].each do |column, config|\n          columns[column] = initialize_column column, config\n        end\n      elsif options[:columns].kind_of? Symbol\n        hash =  self.send options[:columns]\n        raise \"Invalid columns configuration\" if not hash.kind_of? Hash\n        columns = initialize_columns :columns => hash\n      elsif options[:columns].kind_of? Proc\n        hash = options[:columns].call\n        raise \"Invalid columns configuration\" if not hash.kind_of? Hash\n        columns = initialize_columns :columns => hash\n      end\n      columns\n    end\n\n    def initialize_column(column,config={})\n      raise \"The column \\#\\{column\\} have an invalid configuration (\\#\\{config.class\\} => \\#\\{config\\})\" if not config.kind_of? Hash\n      column = column.to_sym\n      column_config = {}\n\n      # Stablish the type\n      if config[:type].class == Class\n        # If exist :type, is a static column\n        column_config[:type] = config[:type]\n      else\n        # if not, is a dynamic column\n        if config[:type].to_sym == :any\n          column_config[:type] = nil\n        elsif config[:type].to_sym == :boolean\n          column_config[:type] = :boolean\n        else\n          raise \"\\#\\{config[:type]\\} is not a valid column type\"\n        end\n      end\n\n      # Stablish the default value\n      # if is a symbol, we execute the function from the model\n      if config[:default].kind_of? Symbol\n        column_config[:default] = self.send(:config[:default])\n      elsif config[:default].kind_of? Proc\n        column_config[:default] = config[:default].call\n      else\n        # If the column have a type, we verify the type\n        if not column_config[:type].nil?\n          if  (column_config[:type] == :boolean and (not [true.class, false.class].include? config[:default].class)) or\n              ((not [:boolean, nil].include?(column_config[:type])) and column_config[:type] != config[:default].class )\n              raise \"The column \\#\\{column\\} has an invalid default value. Expected \\#\\{column_config[:type]}, not \\#\\{config[:default].class}\"\n          end\n          column_config[:default] = config[:default]\n        else\n          # If is dynamic, only we set the default value\n          column_config[:default] = config[:default]\n        end\n      end\n\n      # Set the validation\n      if [Symbol, Proc].include? config[:validate].class\n        column_config[:validate] = config[:validate]\n        create_validation_for column, config[:validate]\n      else\n        raise \"The validation of \\#\\{column\\} is invalid\"\n      end\n\n\n      column_config\n    end\n\n    def create_validation_for(column, validation)\n      column = column.to_sym\n      @aac_validation ||= {}\n      @aac_validation[column] = validation\n    end\n\n    def expand_options(options={}, opts={})\n      config_opts = {\n        :not_expand => [],\n        :not_call_symbol => []\n      }.merge! opts\n      if options.kind_of? Hash\n        opts = {}\n        options.each do |column, config|\n          if not config_opts[:not_expand].include? column.to_sym\n            if not config_opts[:not_call_symbol].include? config\n              opts[column.to_sym] = expand_options(get_value_of(config), config_opts)\n            else\n              opts[column.to_sym] = expand_options(config, config_opts)\n            end\n          else\n            opts[column.to_sym] = config\n          end\n        end\n        return opts\n      else\n        return get_value_of options\n      end\n    end\n\n    def get_value_of(value)\n      if value.kind_of? Symbol\n        # If the function exist, we execute it\n        if  self.respond_to? value\n          return self.send value\n        # if the the function not exist, whe set te symbol as a value\n        else\n          return value\n        end\n      elsif value.kind_of? Proc\n        return value.call\n      else\n        return value\n      end\n    end\n  EOV\n  \nend\n"