Sha256: 4b0c7e220e2e3a8d5b701b1ae163f731505b8e42ebad12dd5a4b567c1dbc8857

Contents?: true

Size: 1.88 KB

Versions: 3

Compression:

Stored size: 1.88 KB

Contents

# root module Faat
module Faat
  # base FaatClass, in this class made,
  # by the main logic of this class of objects,
  # inherited basic resources and services
  class FaatObject
    def initialize(resource)
      # setup resource name for accessing from other methods
      @resource_name = resource.class.name.underscore

      # setup :attr_accessor for resource attributes
      self.class.send(:attr_accessor, resource.class.name.underscore)
      # setup :class_name for resource class
      self.class.class_eval { @class_name = resource.class.name }
      instance_variable_set("@#{resource.class.name.underscore}", resource)
    end

    # initialize :method_missing for ActiveRecord methods
    # like :save, :valid?, :destroy and others

    def method_missing(name, *attr, &block)
      # getting resource by instance variable :resource_name
      resource.send(name, *attr, &block) || super
    end

    # initialize :respond_to_missing? method for working with
    # ActiveRecord instance methods

    def respond_to_missing?(name, include_private = false)
      # getting resource by instance variable :resource_name,
      # for :respond_to? method
      resource.respond_to?(name) || super
    end

    # add class methods form resource class
    class << self
      # singleton method :method_missing
      def method_missing(name, *attr, &block)
        # initialize :method_missing for accessing for
        # ActiveRecord model class_methods
        class_eval do
          @class_name.constantize.send(name, *attr, &block) || super
        end
      end

      def respond_to_missing?(name, include_private = false)
        # getting respond_to? access to ActiveRecord model class_methods
        class_eval do
          @class_name.constantize.respond_to?(name) || super
        end
      end
    end

    private

    def resource
      instance_variable_get("@#{@resource_name}")
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
faat-0.1.7 lib/faat/faat_object/faat_object.rb
faat-0.1.6 lib/faat/faat_object/faat_object.rb
faat-0.1.5 lib/faat/faat_object/faat_object.rb