Sha256: b0c8c64d77263d5988b35e07b3854e40af98eaaeef6b1bdb2c52c63a7df9e7dc

Contents?: true

Size: 1.49 KB

Versions: 8

Compression:

Stored size: 1.49 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)

        _attributes[name.to_sym] = attribute

        attr_accessor name
      end

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

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

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

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

      def attribute_names
        _attributes.keys
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
infopark_reactor-1.29.1 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.29.0 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.28.0 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.27.0.rc10 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.27.0.rc9 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.27.0.rc8 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.27.0.rc7 lib/reactor/tools/xml_attributes.rb
infopark_reactor-1.27.0.rc4 lib/reactor/tools/xml_attributes.rb