Sha256: bad69c2c4e38660efe19beee8c0dbfd9002299e582e8d8caff449eadcd91afc8

Contents?: true

Size: 1.66 KB

Versions: 1

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Immoscout
  module Models
    module Concerns
      module Propertiable
        extend ActiveSupport::Concern

        included do
          cattr_reader :properties, instance_accessor: false do
            {}
          end

          # :reek:ControlParameter - standard stuff, reek!
          # :reek:TooManyStatements
          def method_missing(method_name, *arguments, &block)
            if method_name =~ /build\_(\w+)/
              match         = Regexp.last_match(1).intern
              properties    = self.class.properties
              coerce_klass  = properties.fetch(match).fetch(:coerce, nil)
              return super if !properties.key?(match) || !coerce_klass

              send("#{match}=", coerce_klass.new)
            else
              super
            end
          end

          def attributes
            self.class.properties.keys.sort
          end

          # :reek:BooleanParameter - standard stuff, reek!
          def respond_to_missing?(method_name, include_private = false)
            method_name.to_s.start_with?('build_') || super
          end
        end

        class_methods do
          def property(name, **opts)
            attr_accessor(name)
            alias_name = opts.fetch(:alias, false)
            if alias_name
              alias_method alias_name, name
              alias_method "#{opts.fetch(:alias)}=", "#{name}="
            end
            properties[name] = opts
          end

          def find_property(name)
            properties[name] || properties.values.detect do |value|
              value[:alias] == name
            end
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
immoscout-1.3.2 lib/immoscout/models/concerns/propertiable.rb