Module: ActiveRecord::MagicMetaMethods

Defined in:
lib/magic_meta_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/magic_meta_methods.rb', line 5

def self.included(base)
  base.extend(ClassMethods)
  class << base
    attr_accessor :meta_methods_column
    attr_accessor :meta_methods_declarations
  end
  base.send(:before_create, :init_meta_methods)
end

Instance Method Details

#convert_to_type(val, type) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/magic_meta_methods.rb', line 51

def convert_to_type (val, type)
  return nil if val.nil? or (val.is_a?(String) and val.blank?)
  converted_value = nil
  case type
  when :string
    converted_value = val.to_s
  when :integer
    converted_value = val.to_i
  when :float
    converted_value = val.to_f
  when :date
    converted_value = val.is_a?(Date) ? val : Date.parse(val.to_s, true)
  when :time
    converted_value = val.is_a?(Time) ? val : Time.parse(val.to_s, true)
  when :datetime
    converted_value = val.is_a?(DateTime) ? val : DateTime.parse(val.to_s)
  when :indifferent_hash
    converted_value = val.is_a?(HashWithIndifferentAccess) ? val : (raise InvalidMetaTypeError.new("Invalid HashWithIndifferentAccess #{val}"))
  when :hash
    converted_value = val.is_a?(Hash) ? val : (raise InvalidMetaTypeError.new("Invalid Hash #{val}"))
  when :array
    converted_value = val.is_a?(Array) ? val : [val]
  when val.is_a?(type)
    converted_value = val
  else
    raise InvalidMetaTypeError.new("Invalid type #{type.name}")
  end
  converted_value
end

#get_meta_value(method_name, type) ⇒ Object



46
47
48
49
# File 'lib/magic_meta_methods.rb', line 46

def get_meta_value(method_name, type)
  return nil unless self[self.class.meta_methods_column].kind_of?(Hash)
  convert_to_type(self[self.class.meta_methods_column][method_name], type)
end

#init_meta_methodsObject



37
38
39
# File 'lib/magic_meta_methods.rb', line 37

def init_meta_methods
  self[self.class.meta_methods_column] = {} unless self[self.class.meta_methods_column].kind_of?(Hash)
end

#set_meta_value(method_name, val, type) ⇒ Object



41
42
43
44
# File 'lib/magic_meta_methods.rb', line 41

def set_meta_value(method_name, val, type)
  init_meta_methods #should never happen, but a failsafe
  self[self.class.meta_methods_column][method_name] = convert_to_type(val, type)
end