Module: HasMetadataColumn
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/has_metadata_column.rb
Overview
Provides the ClassMethods#has_metadata_column method to subclasses of ‘ActiveRecord::Base`.
Defined Under Namespace
Modules: ClassMethods
Constant Summary collapse
- TYPES =
Valid values for the ‘:type` option.
[String, Fixnum, Integer, Float, Hash, Array, TrueClass, FalseClass, Boolean, NilClass, Date, Time]
Class Method Summary collapse
Instance Method Summary collapse
- #as_json(options = {}) ⇒ Object
- #assign_multiparameter_attributes(pairs) ⇒ Object
- #inspect ⇒ Object
- #reload ⇒ Object
- #to_xml(options = {}) ⇒ Object
Class Method Details
.metadata_typecast(value, type = nil) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/has_metadata_column.rb', line 26 def self.(value, type=nil) type ||= String raise ArgumentError, "Can't convert objects of type #{type.to_s}" unless TYPES.include?(type) if value.kind_of?(String) then if type == Integer or type == Fixnum then begin return Integer(value.sub(/^0+/, '')) # so that it doesn't think it's in octal rescue ArgumentError return value end elsif type == Float then begin return Float(value) rescue ArgumentError return value end elsif type == Boolean then return value.parse_bool elsif type == Date then return nil if value.nil? begin return Date.parse(value) rescue ArgumentError return value end elsif type == Time then return nil if value.nil? begin return Time.parse(value) rescue ArgumentError return value end end end return value end |
Instance Method Details
#as_json(options = {}) ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/has_metadata_column.rb', line 173 def as_json(={}) ||= Hash.new # the JSON encoder can sometimes give us nil options? [:except] = Array.wrap([:except]) + [self.class.] = self.class..keys &= Array.wrap([:only]) if [:only] -= Array.wrap([:except]) [:methods] = Array.wrap([:methods]) + super end |
#assign_multiparameter_attributes(pairs) ⇒ Object
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/has_metadata_column.rb', line 194 def assign_multiparameter_attributes(pairs) fake_attributes = pairs.select { |(field, _)| self.class..include? field[0, field.index('(')].to_sym } fake_attributes.group_by { |(field, _)| field[0, field.index('(')] }.each do |field_name, parts| = self.class.[field_name.to_sym] if [:type] then args = parts.each_with_object([]) do |(part_name, value), ary| part_ann = part_name[part_name.index('(') + 1, part_name.length] index = part_ann.to_i - 1 raise "Out-of-bounds multiparameter argument index" unless index >= 0 ary[index] = if value.blank? then nil elsif part_ann.ends_with?('i)') then value.to_i elsif part_ann.ends_with?('f)') then value.to_f else value end end send :"#{field_name}=", args.any? ? [:type].new(*args) : nil else raise "#{field_name} has no type and cannot be used for multiparameter assignment" end end super(pairs - fake_attributes) end |
#inspect ⇒ Object
224 225 226 |
# File 'lib/has_metadata_column.rb', line 224 def inspect "#<#{self.class.to_s} #{attributes.except(self.class..to_s).merge(.try!(:stringify_keys) || {}).map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>" end |
#reload ⇒ Object
229 230 231 232 233 |
# File 'lib/has_metadata_column.rb', line 229 def reload(*) super.tap do @_metadata_hash = nil end end |
#to_xml(options = {}) ⇒ Object
184 185 186 187 188 189 190 191 |
# File 'lib/has_metadata_column.rb', line 184 def to_xml(={}) [:except] = Array.wrap([:except]) + [self.class.] = self.class..keys &= Array.wrap([:only]) if [:only] -= Array.wrap([:except]) [:methods] = Array.wrap([:methods]) + super end |