Class: Faat::FaatObject

Inherits:
Object
  • Object
show all
Defined in:
lib/faat/faat_object/faat_object.rb

Overview

base FaatClass, in this class made, by the main logic of this class of objects, inherited basic resources and services

Direct Known Subclasses

Services::Base

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ FaatObject

Returns a new instance of FaatObject.



7
8
9
10
11
12
13
14
15
16
# File 'lib/faat/faat_object/faat_object.rb', line 7

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *attr, &block) ⇒ Object

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



21
22
23
24
# File 'lib/faat/faat_object/faat_object.rb', line 21

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

Class Method Details

.method_missing(name, *attr, &block) ⇒ Object

singleton method :method_missing



38
39
40
41
42
43
44
# File 'lib/faat/faat_object/faat_object.rb', line 38

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

.respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/faat/faat_object/faat_object.rb', line 46

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

Instance Method Details

#respond_to_missing?(name, include_private = false) ⇒ Boolean

initialize :respond_to_missing? method for working with ActiveRecord instance methods

Returns:

  • (Boolean)


29
30
31
32
33
# File 'lib/faat/faat_object/faat_object.rb', line 29

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