42
43
44
45
46
47
48
49
50
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
80
|
# File 'lib/mongo_doc/attributes.rb', line 42
def attr_accessor_with_mongo(*args)
return attr_accessor_without_mongo(*args) if args.first == :validation_context
opts = args.
default = opts.delete(:default)
type = opts.delete(:type)
args.each do |name|
_keys << name unless _keys.include?(name)
attr_writer name
if default
define_method("_default_#{name}", default.kind_of?(Proc) ? default : proc { default })
private "_default_#{name}"
module_eval(" def \#{name} # def birth_date\n unless defined? @\#{name} # unless defined? @birth_date\n @\#{name} = _default_\#{name} # @birth_date = _default_birth_date\n end # end\n class << self; attr_reader :\#{name} end # class << self; attr_reader :birth_date end\n @\#{name} # @birth_date\n end # end\n RUBY\n else\n attr_reader name\n end\n\n if type and type.respond_to?(:cast_from_string)\n module_eval(<<-RUBY, __FILE__, __LINE__)\n def \#{name}_with_type=(value) # def birth_date_with_type=(value)\n if value.kind_of?(String) # if value.kind_of?(String)\n value = \#{type}.cast_from_string(value) # value = Date.cast_from_string(value)\n end # end\n self.\#{name}_without_type = value # self.birth_date_without_type = value\n end # end\n RUBY\n alias_method_chain \"\#{name}=\", :type\n end\n end\nend\n", __FILE__, __LINE__)
|