Class: Arclight::Repository
- Inherits:
-
Object
- Object
- Arclight::Repository
- 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
-
.all ⇒ Array<Repository>
Mimics ActiveRecord’s ‘all` behavior.
-
.find_by(slug: nil, name: nil) ⇒ Repository
Mimics ActiveRecord dynamic ‘find_by` behavior for the slug or name.
-
.find_by!(*args) ⇒ Repository
Mimics ActiveRecord dynamic ‘find_by!` behavior for the slug or name.
-
.from_yaml(file) ⇒ Hash<Slug,Repository>
Load repository information from a YAML file.
Instance Method Summary collapse
-
#city_state_zip_country ⇒ String
Handles the formatting of “city, state zip, country”.
-
#initialize(slug, data = {}) ⇒ Repository
constructor
A new instance of Repository.
Constructor Details
#initialize(slug, data = {}) ⇒ Repository
Returns a new instance of Repository.
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
.all ⇒ Array<Repository>
Mimics ActiveRecord’s ‘all` behavior
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
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
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
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_country ⇒ String
Returns 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 |