lib/relaxo/model/properties/attribute.rb in relaxo-model-0.18.0 vs lib/relaxo/model/properties/attribute.rb in relaxo-model-0.19.0

- old
+ new

@@ -24,30 +24,30 @@ module Model module Properties # Handle conversions for standard datatypes. class Attribute @@attributes = {} - + def self.for_class(klass, &block) @@attributes[klass] = Proc.new(&block) end - + def self.[](klass, proc = nil) self.new(klass, &proc) end - + def initialize(klass, &serialization) @klass = klass - + if block_given? self.instance_eval(&serialization) else self.instance_eval(&@@attributes[klass]) end end end - + class Serialized def self.[](klass, proc = nil) self.new(klass, &proc) end @@ -61,96 +61,108 @@ def convert_from_primative(dataset, value) @klass.load(value) end end - + Required = Attribute - + class Optional def self.[] klass self.new(klass) end - + def initialize(klass) @klass = klass end + def blank?(value) + if value.nil? + return true + end + + if value.respond_to?(:empty?) && value.empty? + return true + end + + return false + end + def convert_to_primative(value) - if value.nil? or (value.respond_to?(:empty?) and value.empty?) + if blank?(value) nil else @klass.convert_to_primative(value) end end - + def convert_from_primative(dataset, value) - if value.nil? or value.empty? + if blank?(value) nil else @klass.convert_from_primative(dataset, value) end end end - + class Boolean end - + Attribute.for_class(Boolean) do def convert_to_primative(value) value ? true : false end - + def convert_from_primative(dataset, value) [true, "on", "true"].include?(value) end end - + Attribute.for_class(Integer) do def convert_to_primative(value) value.to_i end - + def convert_from_primative(dataset, value) value.to_i end end - + Attribute.for_class(Float) do def convert_to_primative(value) value.to_f end - + def convert_from_primative(dataset, value) value.to_f end end - + Attribute.for_class(Date) do def convert_to_primative(value) value.iso8601 end - + def convert_from_primative(dataset, value) Date.parse(value) end end - + Attribute.for_class(DateTime) do def convert_to_primative(value) value.iso8601 end - + def convert_from_primative(dataset, value) DateTime.parse(value) end end - + Attribute.for_class(String) do def convert_to_primative(value) value.to_s end - + def convert_from_primative(dataset, value) value.to_s end end end