Class: Arclight::Repository

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Conversion
Defined in:
lib/arclight/repository.rb

Overview

Static information about a given repository identified by a unique ‘slug`

Constant Summary collapse

FIELDS =
%i[name
description
visit_note
building
address1
address2
city
state
zip
country
phone
contact_info
thumbnail_url
google_request_url
google_request_mappings
collection_count].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(slug, data = {}) ⇒ Repository

Returns a new instance of Repository.

Parameters:

  • `slug` (String)

    the unique identifier for the repository

  • `data` (Hash)


31
32
33
34
35
36
37
# File 'lib/arclight/repository.rb', line 31

def initialize(slug, data = {})
  @slug = slug
  FIELDS.each do |field|
    value = data[field.to_s]
    send("#{field}=", value) if value.present?
  end
end

Class Method Details

.allArray<Repository>

Mimics ActiveRecord’s ‘all` behavior

Returns:



62
63
64
# File 'lib/arclight/repository.rb', line 62

def self.all
  from_yaml(ENV['REPOSITORY_FILE'] || 'config/repositories.yml').values
end

.find_by(slug: nil, name: nil) ⇒ Repository

Mimics ActiveRecord dynamic ‘find_by` behavior for the slug or name

Parameters:

  • `slug` (String)

    or ‘name`

Returns:



70
71
72
73
74
75
76
77
78
# File 'lib/arclight/repository.rb', line 70

def self.find_by(slug: nil, name: nil)
  if slug
    all.find { |repo| repo.slug == slug }
  elsif name
    all.find { |repo| repo.name == name }
  else
    raise ArgumentError, 'Requires either slug or name parameters to find_by'
  end
end

.find_by!(*args) ⇒ Repository

Mimics ActiveRecord dynamic ‘find_by!` behavior for the slug or name

Parameters:

  • `slug` (String)

    or ‘name` – same as `find_by`

Returns:

Raises:

  • (ActiveRecord::RecordNotFound)

    if cannot find repository



85
86
87
88
89
# File 'lib/arclight/repository.rb', line 85

def self.find_by!(*args)
  repository = find_by(*args)
  raise ActiveRecord::RecordNotFound if repository.blank?
  repository
end

.from_yaml(file) ⇒ Hash<Slug,Repository>

Load repository information from a YAML file

Parameters:

  • `filename` (String)

Returns:



50
51
52
53
54
55
56
57
# File 'lib/arclight/repository.rb', line 50

def self.from_yaml(file)
  repos = {}
  data = YAML.safe_load(File.read(file))
  data.keys.each do |slug|
    repos[slug] = new(slug, data[slug])
  end
  repos
end

Instance Method Details

#city_state_zip_countryString

Returns handles the formatting of “city, state zip, country”.

Returns:

  • (String)

    handles the formatting of “city, state zip, country”



40
41
42
43
44
# File 'lib/arclight/repository.rb', line 40

def city_state_zip_country
  state_zip = state
  state_zip += " #{zip}" if zip
  [city, state_zip, country].compact.join(', ')
end