Module: Dynabix::Metadata
- Defined in:
- lib/dynabix/metadata.rb
Overview
Extending ActiveRecord with dynamic accessors for serialization
Instance Method Summary collapse
-
#has_metadata(serializer = :metadata, *attributes) ⇒ void
Set up the model for serialization to a HashWithIndifferentAccess.
-
#metadata_accessor(*attrs) ⇒ void
Default read/write accessor, user defined accessors are available under Ruby 1.9+.
-
#metadata_reader(*attrs) ⇒ void
Default read accessor (getter), user defined accessors are available under Ruby 1.9+.
-
#metadata_writer(*attrs) ⇒ void
Default write accessor (setter), user defined accessors are available under Ruby 1.9+.
Instance Method Details
#has_metadata(serializer = :metadata, *attributes) ⇒ void
This method returns an undefined value.
Set up the model for serialization to a HashWithIndifferentAccess.
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+
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+
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+
119 120 121 122 123 |
# File 'lib/dynabix/metadata.rb', line 119 def (*attrs) attrs.each do |attr| create_writer(:metadata, attr) end end |