Module: DatastaxRails::AttributeMethods

Extended by:
ActiveSupport::Autoload, ActiveSupport::Concern
Includes:
ActiveModel::AttributeMethods
Included in:
Base
Defined in:
lib/datastax_rails.rb,
lib/datastax_rails/attribute_methods.rb,
lib/datastax_rails/attribute_methods/read.rb,
lib/datastax_rails/attribute_methods/dirty.rb,
lib/datastax_rails/attribute_methods/write.rb,
lib/datastax_rails/attribute_methods/primary_key.rb,
lib/datastax_rails/attribute_methods/typecasting.rb

Overview

Methods for defining attributes on a model

Defined Under Namespace

Modules: ClassMethods, Dirty, PrimaryKey, Read, Typecasting, Write

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object


128
129
130
131
132
133
134
135
# File 'lib/datastax_rails/attribute_methods.rb', line 128

def method_missing(method_id, *args, &block)
  if !self.class.attribute_methods_generated?
    self.class.define_attribute_methods
    send(method_id, *args, &block)
  else
    super
  end
end

Instance Method Details

#attribute_exists?(name) ⇒ Boolean


124
125
126
# File 'lib/datastax_rails/attribute_methods.rb', line 124

def attribute_exists?(name)
  @attributes.key?(name.to_s)
end

#attribute_for_inspect(attr_name) ⇒ Object

Returns an #inspect-like string for the value of the attribute attr_name. String attributes are truncated upto 50 characters, and Date and Time attributes are returned in the :db format. Other attributes return the value of #inspect without modification.

person = Person.create!(name: 'David Heinemeier Hansson ' * 3)

person.attribute_for_inspect(:name)
# => "\"David Heinemeier Hansson David Heinemeier Hansson D...\""

person.attribute_for_inspect(:created_at)
# => "\"2012-10-22 00:15:07\""

159
160
161
162
163
164
165
166
167
168
169
# File 'lib/datastax_rails/attribute_methods.rb', line 159

def attribute_for_inspect(attr_name)
  value = read_attribute(attr_name)

  if value.is_a?(String) && value.length > 50
    "#{value[0..50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  else
    value.inspect
  end
end

#column_for_attribute(name) ⇒ Object


142
143
144
# File 'lib/datastax_rails/attribute_methods.rb', line 142

def column_for_attribute(name)
  self.class.column_for_attribute(name)
end

#respond_to?(*args) ⇒ Boolean


137
138
139
140
# File 'lib/datastax_rails/attribute_methods.rb', line 137

def respond_to?(*args)
  self.class.define_attribute_methods unless self.class.attribute_methods_generated?
  super
end