Module: Immoscout::Models::Concerns::Propertiable
- Extended by:
- ActiveSupport::Concern
- Included in:
- ApartmentBuy, Immoscout::Models::Contact, Document, HouseBuy, Parts::Address, Parts::ApiSearchData, Parts::Contact, Parts::Coordinate, Parts::Courtage, Parts::EnergyCertificate, Parts::EnergySource, Parts::FiringType, Parts::GeoCode, Parts::GeoHierarchy, Parts::InternationalCountryRegion, Parts::Price, Parts::PublishChannel, Parts::RealEstate, Parts::Url, Parts::Urls, Picture, Publish
- Defined in:
- lib/immoscout/models/concerns/propertiable.rb
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) ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
|
# File 'lib/immoscout/models/concerns/propertiable.rb', line 16
def method_missing(method_name, *arguments, &)
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
50
51
52
53
54
|
# File 'lib/immoscout/models/concerns/propertiable.rb', line 50
def find_property(name)
properties[name] || properties.values.detect do |value|
value[:alias] == name
end
end
|
.property(name, **opts) ⇒ Object
39
40
41
42
43
44
45
46
47
48
|
# File 'lib/immoscout/models/concerns/propertiable.rb', line 39
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
#attributes ⇒ Object
29
30
31
|
# File 'lib/immoscout/models/concerns/propertiable.rb', line 29
def attributes
self.class.properties.keys.sort
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
33
34
35
|
# File 'lib/immoscout/models/concerns/propertiable.rb', line 33
def respond_to_missing?(method_name, include_private = false)
method_name.to_s.start_with?('build_') || super
end
|