Class: Object
- Inherits:
- BasicObject
- Defined in:
- lib/ruby_core_extensions/object.rb,
lib/ruby_core_extensions/recursive/object.rb
Instance Method Summary collapse
- #booleanize(name, options) ⇒ Object
- #convert(&converter) ⇒ Object (also: #convert_values_recursively, #convert_recursively)
-
#phonetic_code ⇒ Object
Convert this object into a string, then convert that to phonetic code.
- #return_self ⇒ Object (also: #convert_keys_recursively, #symbolize_keys_recursively, #make_indifferent_access_recursively)
- #return_to_s ⇒ Object (also: #stringify_values_recursively)
-
#sounds_like?(other) ⇒ Boolean
Does this object sound like another - phonetically?.
- #to_bool ⇒ Object
- #to_long_s ⇒ Object
- #virtual_belongs_to(*associations) ⇒ Object
Instance Method Details
#booleanize(name, options) ⇒ Object
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, ) raise ArgumentError, ":rescue option is required" if [:rescue].blank? if ![:rescue].is_a?(Array) [:rescue] = [[: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 #{[: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_code ⇒ Object
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_self ⇒ Object 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_s ⇒ Object 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?
3 4 5 |
# File 'lib/ruby_core_extensions/object.rb', line 3 def sounds_like?(other) self.phonetic_code == other.phonetic_code end |
#to_bool ⇒ Object
67 68 69 |
# File 'lib/ruby_core_extensions/object.rb', line 67 def to_bool self.to_s.to_bool end |
#to_long_s ⇒ Object
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) = associations. 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} ||= #{([:class_name] || association.to_s).classify}.find_by_id(@#{association}_id) end def #{association}_id @#{association}.id if #{association} end EVAL end end |