Module: Immoscout::Models::Concerns::Propertiable

Overview

Includes functionality to access and modify model attributes transparently.

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object

:reek:ControlParameter - standard stuff, reek! :reek:TooManyStatements



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/immoscout/models/concerns/propertiable.rb', line 18

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

Class Method Details

.find_property(name) ⇒ Object



53
54
55
56
57
# File 'lib/immoscout/models/concerns/propertiable.rb', line 53

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

.property(name, **opts) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/immoscout/models/concerns/propertiable.rb', line 42

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

Instance Method Details

#attributesObject



31
32
33
# File 'lib/immoscout/models/concerns/propertiable.rb', line 31

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

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

:reek:BooleanParameter - standard stuff, reek!

Returns:

  • (Boolean)


36
37
38
# File 'lib/immoscout/models/concerns/propertiable.rb', line 36

def respond_to_missing?(method_name, include_private = false)
  method_name.to_s.start_with?('build_') || super
end