Sha256: 3f9dce5fe1a57284a1462eef514f5d680f1f319d47b92dd65758a2e7af84fb90

Contents?: true

Size: 1.95 KB

Versions: 8

Compression:

Stored size: 1.95 KB

Contents

module CassandraObject
  module AttributeMethods
    extend ActiveSupport::Concern
    include ActiveModel::AttributeMethods

    included do
      attribute_method_suffix("", "=")
      
      # (Alias for the protected read_attribute method).
      def [](attr_name)
        read_attribute(attr_name)
      end

      # Updates the attribute identified by <tt>attr_name</tt> with the specified +value+.
      # (Alias for the protected write_attribute method).
      def []=(attr_name, value)
        write_attribute(attr_name, value)
      end
    end

    module ClassMethods
      def define_attribute_methods
        return if attribute_methods_generated?
        super(attribute_definitions.keys)
        @attribute_methods_generated = true
      end

      def attribute_methods_generated?
        @attribute_methods_generated ||= false
      end
    end

    def write_attribute(name, value)
      @attributes[name.to_s] = self.class.typecast_attribute(self, name, value)
    end

    def read_attribute(name)
      @attributes[name.to_s]
    end

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

    def attributes
      Hash[@attributes.map { |name, _| [name, read_attribute(name)] }]
    end

    def attributes=(attributes)
      attributes.each do |(name, value)|
        send("#{name}=", value)
      end
    end

    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

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

    protected
      def attribute_method?(name)
        !!attribute_definitions[name.to_sym]
      end

    private
      def attribute(name)
        read_attribute(name)
      end
    
      def attribute=(name, value)
        write_attribute(name, value)
      end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
gotime-cassandra_object-2.12.2 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.12.1 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.12.0 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.11.9 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.11.8 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.11.7 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.11.6 lib/cassandra_object/attribute_methods.rb
gotime-cassandra_object-2.11.5 lib/cassandra_object/attribute_methods.rb