Class: WorldDb::Model::Name

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/worlddb/models/name.rb,
lib/worlddb/models/forward.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_cities(name, country_id) ⇒ Object

note: requires country_id (for search scope) – add version w/ state_id scope - why?? why not??



45
46
47
48
49
50
51
52
# File 'lib/worlddb/models/name.rb', line 45

def self.find_cities( name, country_id ) # note: requires country_id (for search scope)  -- add version w/ state_id scope - why?? why not??
  Name.joins(
              :place => :city
            ).where(
              :name       => name,
              :place_kind => 'CITY',
              :'cities.country_id' => country_id )
end

.find_counties(name, state_id) ⇒ Object

note requires state_id (for search scope)



65
66
67
68
69
70
71
72
# File 'lib/worlddb/models/name.rb', line 65

def self.find_counties( name, state_id )  # note requires state_id (for search scope)
  Name.joins(
              :place => :county
            ).where(
              :name                => name,
              :place_kind          => 'COUN',
              :'counties.state_id' => state_id )
end

.find_munis(name, state_id) ⇒ Object

note requires state_id (for search scope)



74
75
76
77
78
79
80
81
# File 'lib/worlddb/models/name.rb', line 74

def self.find_munis( name, state_id )    # note requires state_id (for search scope)
  Name.joins(
              :place => :muni
            ).where(
              :name             => name,
              :place_kind       => 'MUNI',
              :'munis.state_id' => state_id )
end

.find_parts(name, state_id) ⇒ Object

– search scoped by state



56
57
58
59
60
61
62
63
# File 'lib/worlddb/models/name.rb', line 56

def self.find_parts( name, state_id )  # note requires state_id (for search scope)
  Name.joins(
              :place => :part
            ).where(
              :name                => name,
              :place_kind          => 'PART',
              :'parts.state_id' => state_id )
end

.find_states(name, country_id) ⇒ Object

finders

– search scoped by country



36
37
38
39
40
41
42
43
# File 'lib/worlddb/models/name.rb', line 36

def self.find_states( name, country_id )  # note: requires country_id (for search scope)
  Name.joins(
              :place => :state
            ).where(
              :name                => name,
              :place_kind          => 'STAT',
              :'states.country_id' => country_id )
end

.parse(*args) ⇒ Object

create



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/worlddb/models/name.rb', line 87

def self.parse( *args )
  ## remove (extract) attribs hash (if last arg is a hash n present)
  more_attribs = args.last.is_a?(Hash) ? args.pop : {}  ## extract_options!
  chunks       = args

  names = NameParser.new.parse( chunks )
  recs = []

  names.each do |name|
    attribs = more_attribs.merge( name: name )  ## overwrite (default) attribs (lang, etc.) plus add name
    puts "[Name.parse] adding Name record:"
    puts "  #{attribs.inspect}"
    rec = Name.create!( attribs )
    recs << rec
  end

  recs # note: returns an array of name records
end

Instance Method Details

#place_objectObject

returns “typed” place object e.g. state, part, county, muni, city, etc.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/worlddb/models/name.rb', line 12

def place_object  # returns "typed" place object e.g. state, part, county, muni, city, etc.

  case place_kind
    when 'STAT'  ## state
      place.state
    when 'PART'  ## part
      place.part
    when 'COUN'  ## county
      place.county
    when 'MUNI'  ## muni
      place.muni
    when 'CITY'  ## city
      place.city
  else
    puts "*** error [Name#place_object] - unknown place_kind #{place_kind}"
    fail "[Name#place_object] - unknown place_kind #{place_kind}"
  end # end case
end