Module: SimpleModel::Attributes::ClassMethods

Defined in:
lib/simple_model/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject

The current intent of the config is allow the managing of features at the global level and overrides options set at attribute definition, which may not be the most flexible and may require re-thinking for future options Options:

  • config.initialize_defaults default is true, if false prevents attributes with default values from auto-initializing



284
285
286
# File 'lib/simple_model/attributes.rb', line 284

def config
  @config
end

Instance Method Details

#add_defined_attribute(attr, options) ⇒ Object

We want to re-run define_attribute_methods since attributes are not all defined at once, so we must set @attribute_methods_generated to nil to allow the re-run to occur ONLY IN RAILS 3.0.



308
309
310
311
312
# File 'lib/simple_model/attributes.rb', line 308

def add_defined_attribute(attr,options)
  defined_attributes[attr] = options
  @attribute_methods_generated = nil #if (ActiveModel::VERSION::MAJOR == 3 && ActiveModel::VERSION::MINOR == 0)
  define_attribute_methods(defined_attributes_keys)
end

#after_initializeObject

A hook to perform actions after all attributes have been initialized Expects an lambda that accept the object and the pending attributes hash EX: lambda {|obj| puts “initialized”}



394
395
396
# File 'lib/simple_model/attributes.rb', line 394

def after_initialize
  @after_initialize
end

#after_initialize=(after_initialize) ⇒ Object

Expects an lambda that accept the object and the pending attributes hash EX: lambda {|obj| puts “initialized”}



400
401
402
403
# File 'lib/simple_model/attributes.rb', line 400

def after_initialize=after_initialize
  raise TypeError "after_initalize must be a Proc" unless after_initialize.is_a?(Proc)
  @after_initialize = after_initialize
end

#alias_attribute(new_alias, attr) ⇒ Object

Creates alias setter and getter for the supplied attribute using the supplied alias See spec for example.

Raises:



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
# File 'lib/simple_model/attributes.rb', line 352

def alias_attribute(new_alias,attr)

  # get to the base attribute
  while alias_attributes[attr]
    attr = alias_attributes[attr]
  end

  raise UndefinedAttribute, "#{attr} is not a defined attribute so it cannot be aliased" unless defined_attributes[attr]

  alias_attributes[new_alias] = attr

  define_method(new_alias) do
    send(attr)
  end
  define_method("#{new_alias}?") do
    send("#{attr}?")
  end
  define_method("#{new_alias}=") do |*args, &block|
    send("#{attr}=",*args, &block)
  end
end

#alias_attributesObject



260
261
262
# File 'lib/simple_model/attributes.rb', line 260

def alias_attributes
  @alias_attributes ||= Hash.new
end

#alias_attributes=(alias_attributes) ⇒ Object



264
265
266
# File 'lib/simple_model/attributes.rb', line 264

def alias_attributes=alias_attributes
  @alias_attributes = alias_attributes
end

#attribute_defined?(attr) ⇒ Boolean

Returns:

  • (Boolean)


276
277
278
# File 'lib/simple_model/attributes.rb', line 276

def attribute_defined?(attr)
  defined_attributes.has_key?(attr.to_sym)
end

#before_initializeObject

A hook to perform actions on the pending attributes or the object before the pending attributes have been initialized. Expects an lambda that accept the object, the pending attributes hash and should return a hash to be set EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}



379
380
381
# File 'lib/simple_model/attributes.rb', line 379

def before_initialize
  @before_initialize
end

#before_initialize=(before_initialize) ⇒ Object

Expects an lambda that accept the object, the pending attributes hash and should return a hash to be set EX: lambda {|obj,attrs| attrs.select{|k,v| !v.blank?}}



386
387
388
389
# File 'lib/simple_model/attributes.rb', line 386

def before_initialize=before_initialize
  raise TypeError "before_initialize must be a lambda that accepts the attributes to be initialize" unless before_initialize.is_a?(Proc)
  @before_initialize = before_initialize
end

#create_attribute_methods(attrs, options) ⇒ Object

builds the setter and getter methods



322
323
324
325
326
327
328
329
330
# File 'lib/simple_model/attributes.rb', line 322

def create_attribute_methods(attrs,options)
  unless attrs.blank?
    attrs.each do |attr|
      define_setter_with_options(attr,options)
      define_reader_with_options(attr,options)
      add_defined_attribute(attr,options)
    end
  end
end

#default_attribute_settingsObject

The default settings for a SimpeModel class Options:

  • :on_set - accepts a lambda that is run when an attribute is set

  • :on_get - accepts a lambda that is run when you get/read an attribute

  • :default - the default value for the attribute, can be a symbol that is sent for a method

  • :initialize - informations the object whether or not it should initialize the attribute with :default value, defaults to true,

    and is overridden by config.initialzie_defaults
    

** If :initialize is set to false you must set :allow_blank to false or it will never set the default value

  • :allow_blank - when set to false, if an attributes value is blank attempts to set the default value, defaults to true



297
298
299
# File 'lib/simple_model/attributes.rb', line 297

def default_attribute_settings
  @default_attribute_settings ||= DEFAULT_ATTRIBUTE_SETTINGS
end

#default_attribute_settings=(default_attribute_settings) ⇒ Object



301
302
303
# File 'lib/simple_model/attributes.rb', line 301

def default_attribute_settings=default_attribute_settings
  @default_attribute_settings = default_attribute_settings
end

#define_reader_with_options(attr, options) ⇒ Object



332
333
334
335
336
337
338
339
# File 'lib/simple_model/attributes.rb', line 332

def define_reader_with_options(attr,options)
  define_method(attr) do
    get_attribute(attr,options)
  end
  define_method("#{attr}?") do
    get_attribute?(attr)
  end
end

#define_setter_with_options(attr, options) ⇒ Object

Creates setter methods for the provided attributes On set, it will mark the attribute as changed if the attributes has been initialized.



344
345
346
347
348
# File 'lib/simple_model/attributes.rb', line 344

def define_setter_with_options(attr,options)
  define_method("#{attr}=") do |val|
    set_attribute(attr,val,options)
  end
end

#defined_attributesObject



268
269
270
# File 'lib/simple_model/attributes.rb', line 268

def defined_attributes
  @defined_attributes ||= Hash.new
end

#defined_attributes=(defined_attributes) ⇒ Object



272
273
274
# File 'lib/simple_model/attributes.rb', line 272

def defined_attributes=defined_attributes
  @defined_attributes = defined_attributes
end

#defined_attributes_keysObject

We don’t want to call define_attribute_methods on methods defined in the parent class



315
316
317
318
319
# File 'lib/simple_model/attributes.rb', line 315

def defined_attributes_keys
  dak = defined_attributes.keys
  dak = dak - superclass.defined_attributes.keys if superclass.respond_to?(:defined_attributes)
  dak
end

#inherited(base) ⇒ Object

Must inherit super’s defined_attributes and alias_attributes



406
407
408
409
410
# File 'lib/simple_model/attributes.rb', line 406

def inherited(base)
  base.defined_attributes = defined_attributes.merge(base.defined_attributes)
  base.alias_attributes = alias_attributes.merge(base.alias_attributes)
  super
end

#new_with_store(session_hash) ⇒ Object

Creates a new instance where the attributes store is set to object provided, which allows one to pass a session store hash or any other hash-like object to be used for persistence. Typically used for modeling session stores for authorization or shopping carts EX:

class ApplicationController < ActionController::Base
  def session_user
    session[:user] ||= {}
    @session_user ||= SessionUser.new_with_store(session[:user])
  end
  helper_method :session_user
end


256
257
258
# File 'lib/simple_model/attributes.rb', line 256

def new_with_store(session_hash)
  self.new(:attributes => session_hash)
end