Sha256: ac8a93922e16db90f2cb214bab058fe8878458bb2b984ce93e271b45c18cec12

Contents?: true

Size: 1.43 KB

Versions: 17

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module Tramway
  module Forms
    # This is `properties`. The main feature of Tramway Form
    module Properties
      # A collection of methods that would be using in users forms
      module ClassMethods
        def property(attribute)
          @properties << attribute

          delegate attribute, to: :object
        end

        def properties(*attributes)
          attributes.any? ? __set_properties(attributes) : __properties
        end

        def __set_properties(attributes)
          attributes.each do |attribute|
            property(attribute)
          end
        end

        def __properties
          (__ancestor_properties + @properties).uniq
        end

        # :reek:ManualDispatch { enabled: false }
        def __ancestor_properties(klass = superclass)
          superklass = klass.superclass

          return [] unless superklass.respond_to?(:properties)

          klass.properties + __ancestor_properties(superklass)
        end

        # :reek:UtilityFunction { enabled: false }
        def __initialize_properties(subclass)
          subclass.instance_variable_set(:@properties, [])
        end
      end

      def self.included(base)
        base.extend ClassMethods
      end

      def __apply_properties(params)
        self.class.properties.each do |attribute|
          public_send("#{attribute}=", params[attribute]) if params.key?(attribute)
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
tramway-0.5.2 lib/tramway/forms/properties.rb
tramway-0.5.1.5 lib/tramway/forms/properties.rb
tramway-0.5.1.3 lib/tramway/forms/properties.rb
tramway-0.5.1.2 lib/tramway/forms/properties.rb
tramway-0.5.1.1 lib/tramway/forms/properties.rb
tramway-0.5.1 lib/tramway/forms/properties.rb
tramway-0.5.0.1 lib/tramway/forms/properties.rb
tramway-0.5 lib/tramway/forms/properties.rb
tramway-0.4.9.3 lib/tramway/forms/properties.rb
tramway-0.4.9.2 lib/tramway/forms/properties.rb
tramway-0.4.9.1 lib/tramway/forms/properties.rb
tramway-0.4.8 lib/tramway/forms/properties.rb
tramway-0.4.7 lib/tramway/forms/properties.rb
tramway-0.4.4 lib/tramway/forms/properties.rb
tramway-0.4.3 lib/tramway/forms/properties.rb
tramway-0.4.2.1 lib/tramway/forms/properties.rb
tramway-0.4.2 lib/tramway/forms/properties.rb