Class: Object

Inherits:
BasicObject
Defined in:
lib/ruby_core_extensions/object.rb,
lib/ruby_core_extensions/recursive/object.rb

Instance Method Summary collapse

Instance Method Details

#booleanize(name, options) ⇒ Object

Raises:

  • (ArgumentError)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruby_core_extensions/object.rb', line 45

def booleanize(name, options)
  raise ArgumentError, ":rescue option is required" if options[:rescue].blank?
  if !options[:rescue].is_a?(Array)
    options[:rescue] = [options[:rescue]]
  end

  normal_name = name.to_s.gsub('!', '')

  class_eval <<-EVAL
    attr_accessor :reason_not_#{normal_name}
    def #{normal_name}?(*args)
      #{name}(*args)
      @reason_not_#{normal_name} = nil
      true
    rescue #{options[:rescue].map(&:to_s).join(', ')} => e
      @reason_not_#{normal_name} = e.message
      false
    end
  EVAL
end

#convert(&converter) ⇒ Object Also known as: convert_values_recursively, convert_recursively



2
3
4
# File 'lib/ruby_core_extensions/recursive/object.rb', line 2

def convert(&converter)
  converter.call(self)
end

#phonetic_codeObject

Convert this object into a string, then convert that to phonetic code



8
9
10
# File 'lib/ruby_core_extensions/object.rb', line 8

def phonetic_code
  self.to_s.phonetic_code
end

#return_selfObject Also known as: convert_keys_recursively, symbolize_keys_recursively, make_indifferent_access_recursively



6
7
8
# File 'lib/ruby_core_extensions/recursive/object.rb', line 6

def return_self
  self
end

#return_to_sObject Also known as: stringify_values_recursively



10
11
12
# File 'lib/ruby_core_extensions/recursive/object.rb', line 10

def return_to_s
  to_s
end

#sounds_like?(other) ⇒ Boolean

Does this object sound like another - phonetically?

Returns:

  • (Boolean)


3
4
5
# File 'lib/ruby_core_extensions/object.rb', line 3

def sounds_like?(other)
  self.phonetic_code == other.phonetic_code
end

#to_boolObject



67
68
69
# File 'lib/ruby_core_extensions/object.rb', line 67

def to_bool
  self.to_s.to_bool
end

#to_long_sObject



12
13
14
# File 'lib/ruby_core_extensions/object.rb', line 12

def to_long_s
  to_s
end

#virtual_belongs_to(*associations) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ruby_core_extensions/object.rb', line 16

def virtual_belongs_to(*associations)
  options = associations.extract_options!

  for association in associations
    class_eval <<-EVAL
      attr_accessor :#{association}, :#{association}_id

      def #{association}=(#{association})
        @#{association}_id = #{association}.id if #{association}
        @#{association} = #{association}
      end

      def #{association}_id=(#{association}_id)
        @#{association} = nil
        @#{association}_id = #{association}_id
      end

      def #{association}
        @#{association} ||= #{(options[:class_name] || association.to_s).classify}.find_by_id(@#{association}_id)
      end

      def #{association}_id
        @#{association}.id if #{association}
      end
    EVAL
  end
end