Module: DatastaxRails::AttributeMethods::ClassMethods

Defined in:
lib/datastax_rails/attribute_methods.rb

Overview

:nodoc:

Instance Method Summary collapse

Instance Method Details

#attribute(name, options) ⇒ Object

attribute :name, :type => :string attribute :ammo, :type => Ammo, :coder => AmmoCodec



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/datastax_rails/attribute_methods.rb', line 76

def attribute(name, options)
  type  = options.delete :type
  coder = options.delete :coder
  default = options.delete :default

  lazy_attributes << name.to_sym if options[:lazy]
  readonly_attributes << name.to_sym if options[:readonly]

  column = Column.new(name, default, type, options)
  column.primary = (name.to_s == primary_key.to_s)
  if coder
    coder = coder.constantize rescue nil # rubocop:disable Style/RescueModifier
    if coder.class == Class && (coder.instance_methods & [:dump, :load]).size == 2
      column.coder = coder.new(self)
    else
      fail ArgumentError, 'Coder must be a class that responds the dump and load instance variables'
    end
  end
  attribute_definitions[name.to_sym] = column
end

#attribute_methods_generated?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/datastax_rails/attribute_methods.rb', line 68

def attribute_methods_generated?
  @attribute_methods_generated ||= false
end

#column_for_attribute(name) ⇒ Object

Returns the column object for the named attribute. Returns nil if the named attribute not exists.

class Person < DatastaxRails::Base
end

person = Person.new
person.column_for_attribute(:name)
# => #<DatastaxRails::Base:0x007ff4ab083980 @name="name", @sql_type="varchar(255)", @null=true, ...>

person.column_for_attribute(:nothing) # => nil



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/datastax_rails/attribute_methods.rb', line 109

def column_for_attribute(name)
  # FIXME: should this return a null object for columns that don't exist?
  column = columns_hash[name.to_s]
  unless column
    # Check for a dynamic column
    columns_hash.values.each do |col|
      next unless col.type == :map && name.to_s.starts_with?("#{col.name}")
      column = col
      break
    end
  end
  column
end

#define_attribute_methodsObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/datastax_rails/attribute_methods.rb', line 46

def define_attribute_methods
  # Use a mutex; we don't want two thread simultaneously trying to define
  # attribute methods.
  generated_attribute_methods.synchronize do
    return false if attribute_methods_generated?
    super(attribute_definitions.keys)
    # Remove setter methods from readonly attributes
    readonly_attributes.each do |attr|
      remove_method("#{attr}=".to_sym) if method_defined?("#{attr}=".to_sym)
    end
    @attribute_methods_generated = true
  end
  true
end

#inherited(child_class) ⇒ Object



24
25
26
27
# File 'lib/datastax_rails/attribute_methods.rb', line 24

def inherited(child_class)
  child_class.initialize_generated_modules
  super
end

#initialize_generated_modulesObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/datastax_rails/attribute_methods.rb', line 29

def initialize_generated_modules
  @generated_attribute_methods = Module.new do
    extend Mutex_m

    const_set :AttrNames, Module.new {
      def self.set_name_cache(name, value)
        const_name = "ATTR_#{name}"
        unless const_defined? const_name
          const_set const_name, value.dup.freeze
        end
      end
    }
  end
  @attribute_methods_generated = false
  include @generated_attribute_methods
end

#undefine_attribute_methodsObject



61
62
63
64
65
66
# File 'lib/datastax_rails/attribute_methods.rb', line 61

def undefine_attribute_methods
  generated_attribute_methods.synchronize do
    super if attribute_methods_generated?
    @attribute_methods_generated = false
  end
end