Module: ActsAsDescriptable

Included in:
LabEvent
Defined in:
lib/acts_as_descriptable.rb

Overview

name: 'acts_as_descriptoable',

description: 'Allows model fields and values to be dynamically extended.',
homepage: 'http://www.sanger.ac.uk/Users/mw4/ruby/rails/acts_as_descriptable',
version: '1.0',
author_link: 'http://www.sanger.ac.uk/Users/mw4/',
author_name: 'Matt Wood'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


32
33
34
35
36
37
# File 'lib/acts_as_descriptable.rb', line 32

def self.included(base)
  base.class_eval do
    serialize :descriptors
    serialize :descriptor_fields, Array
  end
end

Instance Method Details

#add_descriptor(descriptor) ⇒ Object


51
52
53
54
# File 'lib/acts_as_descriptable.rb', line 51

def add_descriptor(descriptor)
  write_attribute(:descriptors,       descriptor_hash.merge(descriptor.name => descriptor.value))
  write_attribute(:descriptor_fields, descriptor_fields.push(descriptor.name))
end

#descriptor_hashObject


56
57
58
# File 'lib/acts_as_descriptable.rb', line 56

def descriptor_hash
  read_attribute(:descriptors) || {}
end

#descriptor_value(key) ⇒ Object


47
48
49
# File 'lib/acts_as_descriptable.rb', line 47

def descriptor_value(key)
  descriptor_hash.fetch(key, '')
end

#descriptorsObject


39
40
41
42
43
44
45
# File 'lib/acts_as_descriptable.rb', line 39

def descriptors
  [].tap do |response|
    each_descriptor do |field, value|
      response.push(Descriptor.new(name: field, value: value))
    end
  end
end