Module: Dynabix::Metadata

Defined in:
lib/dynabix/metadata.rb

Overview

Extending ActiveRecord with dynamic accessors for serialization

Instance Method Summary collapse

Instance Method Details

#has_metadata(serializer = :metadata, *attributes) ⇒ void

This method returns an undefined value.

Set up the model for serialization to a HashWithIndifferentAccess.

Examples:

Using the default column name “:metadata”, specify the attributes in a separate step


class Thing < ActiveRecord::Base
  

  # full accessors
   :breakfast_food, :wheat_products, :needs_milk

  # read-only
   :friends_with_spoons
end

Specifying attributes for full attributes accessors in one step


class Thing < ActiveRecord::Base
   :metadata, :breakfast_food, :wheat_products, :needs_milk
end

Specifying multiple metadata serializers (Ruby 1.9 only)


class Thing < ActiveRecord::Base
   :cows
   :chickens, :tasty, :feather_count

  # read-only
  cow_reader :likes_milk, :hates_eggs

  # write-only
  cow_writer :no_wheat_products

  # extra full accessors for chickens
  chicken_accessor :color, :likes_eggs
end

Parameters:

  • serializer, (Symbol)

    the symbolized name (:metadata) of the database text column used for serialization

  • optional (Array<Symbol>)

    list of attribute names to add to the model as full accessors



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
# File 'lib/dynabix/metadata.rb', line 49

def (serializer=:metadata, *attributes)
  serialize(serializer, HashWithIndifferentAccess)

  if RUBY_VERSION < '1.9'
    raise "has_metadata serializer must be named ':metadata', this restriction is lifted in Ruby 1.9+" unless serializer == :metadata
  else
    # we can safely define additional accessors, Ruby 1.8 will only
    # be able to use the statically defined :metadata_accessor
    if serializer != :metadata

      # define the class accessor
      define_singleton_method "#{serializer}_accessor" do |*attrs|
        attrs.each do |attr|
          create_reader(serializer, attr)
          create_writer(serializer, attr)
        end
      end

      # define the class read accessor
      define_singleton_method "#{serializer}_reader" do |*attrs|
        attrs.each do |attr|
          create_reader(serializer, attr)
        end
      end

      # define the class write accessor
      define_singleton_method "#{serializer}_writer" do |*attrs|
        attrs.each do |attr|
          create_writer(serializer, attr)
        end
      end

    end
  end

  # Define each of the attributes for this serializer
  attributes.each do |attr|
    create_reader(serializer, attr)
    create_writer(serializer, attr)
  end
end

#metadata_accessor(*attrs) ⇒ void

This method returns an undefined value.

Default read/write accessor, user defined accessors are available under Ruby 1.9+

Parameters:

  • attributes (Array<Symbol>)


96
97
98
99
100
101
# File 'lib/dynabix/metadata.rb', line 96

def (*attrs)
  attrs.each do |attr|
    create_reader(:metadata, attr)
    create_writer(:metadata, attr)
  end
end

#metadata_reader(*attrs) ⇒ void

This method returns an undefined value.

Default read accessor (getter), user defined accessors are available under Ruby 1.9+

Parameters:

  • attributes (Array<Symbol>)


108
109
110
111
112
# File 'lib/dynabix/metadata.rb', line 108

def (*attrs)
  attrs.each do |attr|
    create_reader(:metadata, attr)
  end
end

#metadata_writer(*attrs) ⇒ void

This method returns an undefined value.

Default write accessor (setter), user defined accessors are available under Ruby 1.9+

Parameters:

  • attributes (Array<Symbol>)


119
120
121
122
123
# File 'lib/dynabix/metadata.rb', line 119

def (*attrs)
  attrs.each do |attr|
    create_writer(:metadata, attr)
  end
end