Module: DatastaxRails::AttributeMethods::ClassMethods
- Defined in:
- lib/datastax_rails/attribute_methods.rb
Instance Method Summary collapse
-
#attribute(name, options) ⇒ Object
attribute :name, :type => :string attribute :ammo, :type => Ammo, :coder => AmmoCodec.
- #attribute_methods_generated? ⇒ Boolean
-
#column_for_attribute(name) ⇒ Object
Returns the column object for the named attribute.
- #define_attribute_methods ⇒ Object
- #inherited(child_class) ⇒ Object
- #initialize_generated_modules ⇒ Object
- #undefine_attribute_methods ⇒ Object
Instance Method Details
#attribute(name, options) ⇒ Object
attribute :name, :type => :string attribute :ammo, :type => Ammo, :coder => AmmoCodec
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/datastax_rails/attribute_methods.rb', line 75 def attribute(name, ) type = .delete :type coder = .delete :coder default = .delete :default lazy_attributes << name.to_sym if [:lazy] readonly_attributes << name.to_sym if [:readonly] column = Column.new(name, default, type, ) 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
67 68 69 |
# File 'lib/datastax_rails/attribute_methods.rb', line 67 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
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/datastax_rails/attribute_methods.rb', line 108 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_methods ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/datastax_rails/attribute_methods.rb', line 45 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
23 24 25 26 |
# File 'lib/datastax_rails/attribute_methods.rb', line 23 def inherited(child_class) child_class.initialize_generated_modules super end |
#initialize_generated_modules ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/datastax_rails/attribute_methods.rb', line 28 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_methods ⇒ Object
60 61 62 63 64 65 |
# File 'lib/datastax_rails/attribute_methods.rb', line 60 def undefine_attribute_methods generated_attribute_methods.synchronize do super if attribute_methods_generated? @attribute_methods_generated = false end end |