Sha256: 251d0e4d49b85bb64c853cdc56d56e403ebc8dc3bde03f4da1c4a2969f4c994a

Contents?: true

Size: 1013 Bytes

Versions: 1

Compression:

Stored size: 1013 Bytes

Contents

module Gossiper
  module Concerns
    module Models
      module DynamicAttributes
        extend ActiveSupport::Concern

        included do
          serialize :dynamic_attributes, JSON
        end

        module ClassMethods
          def dynamic_attributes(*args)
            args.each do |method|

              # dynamic getters
              define_method method do
                dynamic_attribute_get(method)
              end

              # dynamic setters
              define_method "#{method}=" do |value|
                dynamic_attribute_set(method, value)
              end
            end
          end
        end

        def dynamic_attribute_set(name, value)
          self.dynamic_attributes = dynamic_attributes.merge({ name.to_s => value })
        end

        def dynamic_attribute_get(name)
          self.dynamic_attributes[name.to_s]
        end

        def dynamic_attributes
          read_attribute(:dynamic_attributes).presence || {}
        end

      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gossiper-0.5.2 lib/gossiper/concerns/models/dynamic_attributes.rb