Sha256: dbc159752f06b77723d85ca7d795e4f4b63bfbd336307a0991b28a0284313f9d

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 KB

Contents

module Spread2RDF
  module Attributes
    extend ActiveSupport::Concern

    module ClassMethods
      def attributes
        if superclass.respond_to?(:attributes) and
            (super_attributes = superclass.attributes).is_a? Hash
          @attributes ||= {}
          @attributes.reverse_merge(super_attributes)
        else
          @attributes
        end
      end

      def attributes=(defaults)
        defaults.each { |attribute, default_value| attr_accessor attribute }
        @attributes = @attributes.try(:merge, defaults) || defaults
      end

      def normalize_attributes(values)
        values
      end
    end

    def init_attributes(initial_values)
      initial_values = self.class.normalize_attributes(initial_values)
      self.class.attributes.each do |attribute, default_value|
        instance_variable_set("@#{attribute}".to_sym,
          initial_values.delete(attribute) || default_value)
      end
      initial_values
    end

    def update_attributes(update_values)
      update_values = self.class.normalize_attributes(update_values)
      update_values.each do |attribute, value|
        next unless self.class.attributes.include? attribute
        instance_variable_set("@#{attribute}".to_sym, value)
      end
      update_values
    end

    def inspect
      "#{self}: " +
        self.class.attributes.map do |attribute, default_value|
          "#{attribute}=#{self.send(attribute)}"
        end.join(', ')
    end

  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
spread2rdf-0.1.7 lib/spread2rdf/attributes.rb
spread2rdf-0.1.6 lib/spread2rdf/attributes.rb
spread2rdf-0.1.5 lib/spread2rdf/attributes.rb
spread2rdf-0.1.4 lib/spread2rdf/attributes.rb
spread2rdf-0.1.3 lib/spread2rdf/attributes.rb
spread2rdf-0.1.2 lib/spread2rdf/attributes.rb
spread2rdf-0.1.1 lib/spread2rdf/attributes.rb
spread2rdf-0.1.0 lib/spread2rdf/attributes.rb