Class: Workarea::Segment::Rules::GeolocationOption
- Inherits:
-
Object
- Object
- Workarea::Segment::Rules::GeolocationOption
- Defined in:
- app/models/workarea/segment/rules/geolocation_option.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Class Method Summary collapse
- .all ⇒ Object
- .from_country(country) ⇒ Object
- .from_subdivision(country, subdivision) ⇒ Object
- .search(query) ⇒ Object
Instance Method Summary collapse
-
#initialize(options) ⇒ GeolocationOption
constructor
A new instance of GeolocationOption.
Constructor Details
#initialize(options) ⇒ GeolocationOption
Returns a new instance of GeolocationOption.
45 46 47 48 49 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 45 def initialize() @id = [:id] @name = [:name] @model = [:model] end |
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
43 44 45 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 43 def id @id end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
43 44 45 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 43 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
43 44 45 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 43 def name @name end |
Class Method Details
.all ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 8 def all @all ||= Country.all.reduce({}) do |memo, country| memo[country.alpha2] = from_country(country) country.subdivisions.each do |id, subdivision| if subdivision.name.present? location = from_subdivision(country, subdivision) memo[location.id] = location end end memo end end |
.from_country(country) ⇒ Object
23 24 25 26 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 23 def from_country(country) return if country.blank? new(id: country.alpha2, name: country.name, model: country) end |
.from_subdivision(country, subdivision) ⇒ Object
28 29 30 31 32 33 34 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 28 def from_subdivision(country, subdivision) return if country.blank? || subdivision.blank? id = "#{country.alpha2}-#{country.subdivisions.invert[subdivision]}" name = "#{subdivision.name}, #{country.alpha2}" new(id: id, name: name, model: subdivision) end |
.search(query) ⇒ Object
36 37 38 39 40 |
# File 'app/models/workarea/segment/rules/geolocation_option.rb', line 36 def search(query) return [] if query.blank? regex = /^#{::Regexp.quote(query.to_s)}/i all.values.select { |r| r.name =~ regex } end |