Class: Workarea::Segment::Rules::GeolocationOption

Inherits:
Object
  • Object
show all
Defined in:
app/models/workarea/segment/rules/geolocation_option.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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(options)
  @id = options[:id]
  @name = options[:name]
  @model = options[:model]
end

Instance Attribute Details

#idObject (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

#modelObject (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

#nameObject (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

.allObject



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