Module: NoBrainer::Document::Types::ClassMethods
- Defined in:
- lib/no_brainer/document/types.rb
Instance Method Summary collapse
- #cast_db_to_model_for(attr, value) ⇒ Object
- #cast_model_to_db_for(attr, value) ⇒ Object
- #cast_user_to_db_for(attr, value) ⇒ Object
- #cast_user_to_model_for(attr, value) ⇒ Object
- #field(attr, options = {}) ⇒ Object
- #persistable_value(k, v, options = {}) ⇒ Object
- #remove_field(attr, options = {}) ⇒ Object
Instance Method Details
#cast_db_to_model_for(attr, value) ⇒ Object
49 50 51 52 53 |
# File 'lib/no_brainer/document/types.rb', line 49 def cast_db_to_model_for(attr, value) type = fields[attr.to_sym].try(:[], :type) return value if type.nil? || value.nil? || !type.respond_to?(:nobrainer_cast_db_to_model) type.nobrainer_cast_db_to_model(value) end |
#cast_model_to_db_for(attr, value) ⇒ Object
43 44 45 46 47 |
# File 'lib/no_brainer/document/types.rb', line 43 def cast_model_to_db_for(attr, value) type = fields[attr.to_sym].try(:[], :type) return value if type.nil? || value.nil? || !type.respond_to?(:nobrainer_cast_model_to_db) type.nobrainer_cast_model_to_db(value) end |
#cast_user_to_db_for(attr, value) ⇒ Object
55 56 57 58 |
# File 'lib/no_brainer/document/types.rb', line 55 def cast_user_to_db_for(attr, value) value = cast_user_to_model_for(attr, value) cast_model_to_db_for(attr, value) end |
#cast_user_to_model_for(attr, value) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/no_brainer/document/types.rb', line 26 def cast_user_to_model_for(attr, value) type = fields[attr.to_sym].try(:[], :type) return value if type.nil? || value.nil? || value.is_a?(NoBrainer::Document::AtomicOps::PendingAtomic) || value.is_a?(RethinkDB::RQL) if type.respond_to?(:nobrainer_cast_user_to_model) type.nobrainer_cast_user_to_model(value) else raise NoBrainer::Error::InvalidType unless value.is_a?(type) value end rescue NoBrainer::Error::InvalidType => error error.update(:model => self, :value => value, :attr_name => attr, :type => type) raise error end |
#field(attr, options = {}) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/no_brainer/document/types.rb', line 64 def field(attr, ={}) if (type = [:type]).is_a?(::Array) raise ArgumentError, "Expected Array type to have single element, got #{types.inspect}" unless type.length == 1 [:type] = NoBrainer::TypedArray.of(type.first) end super type = [:type] return unless type raise "Please use a class for the type option" unless type.is_a?(Class) case type.to_s when "NoBrainer::Geo::Circle" then raise "Cannot store circles :(" when "NoBrainer::Geo::Polygon", "NoBrainer::Geo::LineString" raise "Make a request on github if you'd like to store polygons/linestrings" end NoBrainer::Document::Types.load_type_extensions(type) if type inject_in_layer :types do define_method("#{attr}=") do |value| begin value = self.class.cast_user_to_model_for(attr, value) @pending_type_errors.try(:delete, attr) rescue NoBrainer::Error::InvalidType => error @pending_type_errors ||= {} @pending_type_errors[attr] = error end super(value) end end if type.respond_to?(:nobrainer_field_defined) type.nobrainer_field_defined(self, attr, ) end end |
#persistable_value(k, v, options = {}) ⇒ Object
60 61 62 |
# File 'lib/no_brainer/document/types.rb', line 60 def persistable_value(k, v, ={}) cast_model_to_db_for(k, super) end |
#remove_field(attr, options = {}) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/no_brainer/document/types.rb', line 102 def remove_field(attr, ={}) if type = fields[attr][:type] inject_in_layer :types do remove_method("#{attr}=") end if type.respond_to?(:nobrainer_field_undefined) type.nobrainer_field_undefined(self, attr, ) end end super end |