Module: HasMetadata

Extended by:
ActiveSupport::Concern
Defined in:
lib/has_metadata.rb,
lib/has_metadata/model.rb

Overview

Provides the ClassMethods#has_metadata method to subclasses of ‘ActiveRecord::Base`.

Defined Under Namespace

Modules: ClassMethods, WithMetadata Classes: Model

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.metadata_typecast(value, type) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/has_metadata.rb', line 26

def self.(value, type)
  if value.kind_of?(String) then
    if type == Integer or type == Integer 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
    end
  end
  return value
end

Instance Method Details

#as_json(options = {}) ⇒ Object



109
110
111
112
113
114
# File 'lib/has_metadata.rb', line 109

def as_json(options={})
  options           ||= Hash.new # the JSON encoder can sometimes give us nil options?
  options[:except]  = Array.wrap(options[:except]) + [:metadata_id]
  options[:methods] = Array.wrap(options[:methods]) + .keys - options[:except].map(&:to_sym)
  super options
end

#assign_multiparameter_attributes(pairs) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/has_metadata.rb', line 124

def assign_multiparameter_attributes(pairs)
  fake_attributes = pairs.select { |(field, _)| self.class..include? field[0, field.index('(')].to_sym }
  result = super(pairs.except(fake_attributes.keys))

  fake_attributes.group_by { |(field, _)| field[0, field.index('(')] }.each do |field_name, parts|
    options = self.class.[field_name.to_sym]
    if options[: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
      args.compact!
      send :"#{field_name}=", options[:type].new(*args) unless args.empty?
    else
      raise "#{field_name} has no type and cannot be used for multiparameter assignment"
    end
  end

  return result
end

#inspectObject



167
168
169
# File 'lib/has_metadata.rb', line 167

def inspect
  "#<#{self.class.to_s} #{attributes.merge(.try(:data).try(:stringify_keys) || {}).map { |k, v| "#{k}: #{v.inspect}" }.join(', ')}>"
end

#metadata!Metadata

Returns An existing associated Metadata instance, or new, saved one if none was found.

Returns:

  • (Metadata)

    An existing associated Metadata instance, or new, saved one if none was found.



154
155
156
157
158
159
160
161
162
163
164
# File 'lib/has_metadata.rb', line 154

def metadata!
  if instance_variables.include?(:@metadata) &&  then
    
  else
    if new_record? then
       || 
    else
       || Metadata.transaction {  ||  }
    end
  end.set_fields self.class.
end

#to_xml(options = {}) ⇒ Object



117
118
119
120
121
# File 'lib/has_metadata.rb', line 117

def to_xml(options={})
  options[:except]  = Array.wrap(options[:except]) + [:metadata_id]
  options[:methods] = Array.wrap(options[:methods]) + .keys - options[:except].map(&:to_sym)
  super options
end