Sha256: 5cdfd37be83a127daf87e12e7aa08faa9202eb694d913eb4465ca422ddcc009a

Contents?: true

Size: 1.52 KB

Versions: 3

Compression:

Stored size: 1.52 KB

Contents

require 'reactor/cm/xml_attribute'
require 'reactor/tools/response_handler/xml_attribute'

module Reactor

  module XmlAttributes

    extend ActiveSupport::Concern

    included do
      class_attribute :_attributes
      self._attributes = {}

      class_attribute :response_handler
      self.response_handler = ResponseHandler::XmlAttribute.new
    end

    module ClassMethods

      # This method can act as both getter and setter.
      # I admit, that it is not the best design ever.
      # But it makes a pretty good DSL
      def primary_key(new_value = nil)
        if new_value.nil?
          @primary_key
        else
          @primary_key = new_value.to_s
          @primary_key
        end
      end

      def attribute(name, options = {})
        xml_name = options.delete(:name).presence || name
        type = options.delete(:type).presence

        attribute = Reactor::Cm::XmlAttribute.new(xml_name, type, options)

        self._attributes[name.to_sym] = attribute

        attr_accessor name
      end

      def attributes(scopes = [])
        scopes = Array(scopes)
        attributes = self._attributes

        if scopes.present?
          attributes.reject { |_, xml_attribute| (xml_attribute.scopes & scopes).blank? }
        else
          attributes
        end
      end

      def xml_attribute(name)
        self._attributes[name.to_sym]
      end

      def xml_attribute_names
        self._attributes.values.map(&:name)
      end

      def attribute_names
        self._attributes.keys
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
infopark_reactor_migrations-1.6.1 lib/reactor/tools/xml_attributes.rb
infopark_reactor_migrations-1.5.2 lib/reactor/tools/xml_attributes.rb
infopark_reactor_migrations-1.5.1 lib/reactor/tools/xml_attributes.rb