Module: ActiveEntity::AttributeMethods::ClassMethods

Defined in:
lib/active_entity/attribute_methods.rb

Instance Method Summary collapse

Instance Method Details

#_has_attribute?(attr_name) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


664
665
666
# File 'lib/active_entity/attribute_methods.rb', line 664

def _has_attribute?(attr_name) # :nodoc:
  attribute_types.key?(attr_name)
end

#alias_attribute(new_name, old_name) ⇒ Object



669
670
671
672
673
674
675
676
677
678
# File 'lib/active_entity/attribute_methods.rb', line 669

def alias_attribute(new_name, old_name)
  self.attribute_aliases = attribute_aliases.merge(new_name.to_s => old_name.to_s)
  CodeGenerator.batch(self, __FILE__, __LINE__) do |owner|
    attribute_method_matchers.each do |matcher|
      matcher_new = matcher.method_name(new_name).to_s
      matcher_old = matcher.method_name(old_name).to_s
      define_proxy_call false, owner, matcher_new, matcher_old
    end
  end
end

#attribute_namesObject

Returns an array of column names as strings if it’s not an abstract class. Otherwise it returns an empty array.

class Person < ActiveEntity::Base
end

Person.attribute_names
# => ["id", "created_at", "updated_at", "name", "age"]


641
642
643
644
645
646
647
# File 'lib/active_entity/attribute_methods.rb', line 641

def attribute_names
  @attribute_names ||= if !abstract_class?
    attribute_types.keys
  else
    []
  end
end

#dangerous_attribute_method?(name) ⇒ Boolean

A method name is ‘dangerous’ if it is already (re)defined by Active Entity, but not by any ancestors. (So ‘puts’ is not dangerous but ‘save’ is.)

Returns:

  • (Boolean)


601
602
603
# File 'lib/active_entity/attribute_methods.rb', line 601

def dangerous_attribute_method?(name) # :nodoc:
  ::ActiveEntity::AttributeMethods.dangerous_attribute_methods.include?(name.to_s)
end

#dangerous_class_method?(method_name) ⇒ Boolean

A class method is ‘dangerous’ if it is already (re)defined by Active Entity, but not by any ancestors. (So ‘puts’ is not dangerous but ‘new’ is.)

Returns:

  • (Boolean)


619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/active_entity/attribute_methods.rb', line 619

def dangerous_class_method?(method_name)
  return true if RESTRICTED_CLASS_METHODS.include?(method_name.to_s)

  if Base.respond_to?(method_name, true)
    if Object.respond_to?(method_name, true)
      Base.method(method_name).owner != Object.method(method_name).owner
    else
      true
    end
  else
    false
  end
end

#define_attribute_methodsObject

Generates all the attribute related methods for columns in the database accessors, mutators and query methods.



550
551
552
553
554
555
556
557
558
559
560
# File 'lib/active_entity/attribute_methods.rb', line 550

def define_attribute_methods # :nodoc:
  return false if @attribute_methods_generated
  # Use a mutex; we don't want two threads simultaneously trying to define
  # attribute methods.
  generated_attribute_methods.synchronize do
    return false if @attribute_methods_generated
    superclass.define_attribute_methods unless base_class?
    super(attribute_names)
    @attribute_methods_generated = true
  end
end

#has_attribute?(attr_name) ⇒ Boolean

Returns true if the given attribute exists, otherwise false.

class Person < ActiveEntity::Base
end

Person.has_attribute?('name')     # => true
Person.has_attribute?('new_name') # => true
Person.has_attribute?(:age)       # => true
Person.has_attribute?(:nothing)   # => false

Returns:

  • (Boolean)


658
659
660
661
662
# File 'lib/active_entity/attribute_methods.rb', line 658

def has_attribute?(attr_name)
  attr_name = attr_name.to_s
  attr_name = attribute_aliases[attr_name] || attr_name
  attribute_types.key?(attr_name)
end

#inherited(child_class) ⇒ Object

:nodoc:



534
535
536
537
# File 'lib/active_entity/attribute_methods.rb', line 534

def inherited(child_class) #:nodoc:
  child_class.initialize_generated_modules
  super
end

#initialize_generated_modulesObject

:nodoc:



539
540
541
542
543
544
545
546
# File 'lib/active_entity/attribute_methods.rb', line 539

def initialize_generated_modules # :nodoc:
  @generated_attribute_methods = const_set(:GeneratedAttributeMethods, GeneratedAttributeMethods.new)
  private_constant :GeneratedAttributeMethods
  @attribute_methods_generated = false
  include @generated_attribute_methods

  super
end

#instance_method_already_implemented?(method_name) ⇒ Boolean

Raises an ActiveEntity::DangerousAttributeError exception when an Active Record method is defined in the model, otherwise false.

class Person < ActiveEntity::Base
  def save
    'already defined by Active Entity'
  end
end

Person.instance_method_already_implemented?(:save)
# => ActiveEntity::DangerousAttributeError: save is defined by Active Entity. Check to make sure that you don't have an attribute or method with the same name.

Person.instance_method_already_implemented?(:name)
# => false

Returns:

  • (Boolean)


583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
# File 'lib/active_entity/attribute_methods.rb', line 583

def instance_method_already_implemented?(method_name)
  if dangerous_attribute_method?(method_name)
    raise DangerousAttributeError, "#{method_name} is defined by Active Entity. Check to make sure that you don't have an attribute or method with the same name."
  end

  if superclass == Base
    super
  else
    # If ThisClass < ... < SomeSuperClass < ... < Base and SomeSuperClass
    # defines its own attribute method, then we don't want to overwrite that.
    defined = method_defined_within?(method_name, superclass, Base) &&
      ! superclass.instance_method(method_name).owner.is_a?(GeneratedAttributeMethods)
    defined || super
  end
end

#method_defined_within?(name, klass, superklass = klass.superclass) ⇒ Boolean

:nodoc:

Returns:

  • (Boolean)


605
606
607
608
609
610
611
612
613
614
615
# File 'lib/active_entity/attribute_methods.rb', line 605

def method_defined_within?(name, klass, superklass = klass.superclass) # :nodoc:
  if klass.method_defined?(name) || klass.private_method_defined?(name)
    if superklass.method_defined?(name) || superklass.private_method_defined?(name)
      klass.instance_method(name).owner != superklass.instance_method(name).owner
    else
      true
    end
  else
    false
  end
end

#undefine_attribute_methodsObject

:nodoc:



562
563
564
565
566
567
# File 'lib/active_entity/attribute_methods.rb', line 562

def undefine_attribute_methods # :nodoc:
  generated_attribute_methods.synchronize do
    super if defined?(@attribute_methods_generated) && @attribute_methods_generated
    @attribute_methods_generated = false
  end
end