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`
Instance Attribute Summary collapse
-
#collection_count ⇒ Object
Returns the value of attribute collection_count.
-
#slug ⇒ Object
Returns the value of attribute slug.
Class Method Summary collapse
-
.all(yaml_file = nil) ⇒ Array<Repository>
Mimics ActiveRecord’s ‘all` behavior.
-
.find_by(slug: nil, name: nil, yaml_file: 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
- #available_request_types ⇒ Object
-
#city_state_zip_country ⇒ String
Handles the formatting of “city, state zip, country”.
-
#initialize(slug, data = {}) ⇒ Repository
constructor
A new instance of Repository.
- #request_config_for_type(type) ⇒ Object
-
#request_config_present? ⇒ Boolean
Why are we using self#respond_to? below?.
- #request_config_present_for_type?(type) ⇒ Boolean
- #request_mappings_for_type(type) ⇒ Object
- #request_url_for_type(type) ⇒ Object
Constructor Details
#initialize(slug, data = {}) ⇒ Repository
Returns a new instance of Repository.
14 15 16 17 18 19 20 |
# File 'lib/arclight/repository.rb', line 14 def initialize(slug, data = {}) @slug = slug data.each do |field, value| self.class.attr_accessor field.to_sym send("#{field}=", value) if value.present? end end |
Instance Attribute Details
#collection_count ⇒ Object
Returns the value of attribute collection_count.
10 11 12 |
# File 'lib/arclight/repository.rb', line 10 def collection_count @collection_count end |
#slug ⇒ Object
Returns the value of attribute slug.
10 11 12 |
# File 'lib/arclight/repository.rb', line 10 def slug @slug end |
Class Method Details
.all(yaml_file = nil) ⇒ Array<Repository>
Mimics ActiveRecord’s ‘all` behavior
96 97 98 99 |
# File 'lib/arclight/repository.rb', line 96 def self.all(yaml_file = nil) yaml_file = ENV['REPOSITORY_FILE'] || 'config/repositories.yml' if yaml_file.nil? from_yaml(yaml_file).values end |
.find_by(slug: nil, name: nil, yaml_file: nil) ⇒ Repository
Mimics ActiveRecord dynamic ‘find_by` behavior for the slug or name
105 106 107 108 109 110 111 112 113 |
# File 'lib/arclight/repository.rb', line 105 def self.find_by(slug: nil, name: nil, yaml_file: nil) if slug all(yaml_file).find { |repo| repo.slug == slug } elsif name all(yaml_file).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
120 121 122 123 124 125 |
# File 'lib/arclight/repository.rb', line 120 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
84 85 86 87 88 89 90 91 |
# File 'lib/arclight/repository.rb', line 84 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
#available_request_types ⇒ Object
74 75 76 77 78 |
# File 'lib/arclight/repository.rb', line 74 def available_request_types return [] unless request_types.present? request_types.keys end |
#city_state_zip_country ⇒ String
Returns handles the formatting of “city, state zip, country”.
23 24 25 26 27 |
# File 'lib/arclight/repository.rb', line 23 def city_state_zip_country state_zip = state state_zip += " #{zip}" if zip [city, state_zip, country].compact.join(', ') end |
#request_config_for_type(type) ⇒ Object
54 55 56 57 58 |
# File 'lib/arclight/repository.rb', line 54 def request_config_for_type(type) return nil unless type && request_config_present_for_type?(type) request_types[type] end |
#request_config_present? ⇒ Boolean
Why are we using self#respond_to? below?
All the keys in the config hash from ‘repositories.yml` are on-the-fly added as attr_accessors up in #initialize. If the request_types key isn’t present, the method won’t be created.
Since the original data is thrown away, this is the best way to see if that key was present.
37 38 39 40 41 42 43 44 |
# File 'lib/arclight/repository.rb', line 37 def request_config_present? return false unless respond_to? :request_types return false if request_types.nil? || request_types.empty? request_configs = request_types.map { |_k, v| v } request_configs[0]&.fetch('request_url').present? && request_configs[0]&.fetch('request_mappings').present? end |
#request_config_present_for_type?(type) ⇒ Boolean
46 47 48 49 50 51 52 |
# File 'lib/arclight/repository.rb', line 46 def request_config_present_for_type?(type) return false unless type && request_config_present? config = request_types[type] config&.fetch('request_url').present? && config&.fetch('request_mappings').present? end |
#request_mappings_for_type(type) ⇒ Object
67 68 69 70 71 72 |
# File 'lib/arclight/repository.rb', line 67 def request_mappings_for_type(type) return nil unless type && request_config_present_for_type?(type) config = request_config_for_type(type) config.fetch('request_mappings') end |
#request_url_for_type(type) ⇒ Object
60 61 62 63 64 65 |
# File 'lib/arclight/repository.rb', line 60 def request_url_for_type(type) return nil unless type && request_config_present_for_type?(type) config = request_config_for_type(type) config.fetch('request_url') end |