Class: Arclight::Repository

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

Overview

Static information about a given repository identified by a unique ‘slug`. These data are loaded from config/repositories.yml

Constant Summary collapse

DEFAULTS =
{
  request_types: {},
  contact_html: '',
  location_html: '',
  visit_note: nil
}.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Repository

Returns a new instance of Repository.



19
20
21
# File 'lib/arclight/repository.rb', line 19

def initialize(attributes = {})
  @attributes = DEFAULTS.merge(attributes).with_indifferent_access
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(field, *args, &block) ⇒ Object



26
27
28
29
30
# File 'lib/arclight/repository.rb', line 26

def method_missing(field, *args, &block)
  return attributes[field] if attributes.include?(field)

  super
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



23
24
25
# File 'lib/arclight/repository.rb', line 23

def attributes
  @attributes
end

#collection_countObject

Returns the value of attribute collection_count.



24
25
26
# File 'lib/arclight/repository.rb', line 24

def collection_count
  @collection_count
end

Class Method Details

.all(yaml_file = nil) ⇒ Array<Repository>

Mimics ActiveRecord’s ‘all` behavior

Returns:



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

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

Parameters:

  • `slug` (String)

    or ‘name`

Returns:



94
95
96
97
98
99
100
101
102
# File 'lib/arclight/repository.rb', line 94

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!(**kwargs) ⇒ 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



109
110
111
112
113
114
# File 'lib/arclight/repository.rb', line 109

def self.find_by!(**kwargs)
  repository = find_by(**kwargs)
  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:



73
74
75
76
77
78
79
80
# File 'lib/arclight/repository.rb', line 73

def self.from_yaml(file)
  repos = {}
  data = YAML.safe_load(File.read(file))
  data.each_key do |slug|
    repos[slug] = new(data[slug].merge(slug: slug))
  end
  repos
end

Instance Method Details

#available_request_typesObject



63
64
65
66
67
# File 'lib/arclight/repository.rb', line 63

def available_request_types
  return [] unless request_types.present?

  request_types.keys
end

#contactObject

rubocop:disable Rails/OutputSafety



37
38
39
# File 'lib/arclight/repository.rb', line 37

def contact
  contact_html.html_safe
end

#locationObject



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

def location
  location_html.html_safe
end

#request_config_for_type(type) ⇒ Object



59
60
61
# File 'lib/arclight/repository.rb', line 59

def request_config_for_type(type)
  request_types.fetch(type, {})
end

#request_config_present?Boolean

rubocop:enable Rails/OutputSafety

Returns:

  • (Boolean)


46
47
48
49
50
# File 'lib/arclight/repository.rb', line 46

def request_config_present?
  request_configs = request_types.values || []
  request_configs.dig(0, 'request_url').present? &&
    request_configs.dig(0, 'request_mappings').present?
end

#request_config_present_for_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


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

def request_config_present_for_type?(type)
  config = request_config_for_type(type)

  config['request_url'].present? &&
    config['request_mappings'].present?
end

#respond_to_missing?(field, *args) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/arclight/repository.rb', line 32

def respond_to_missing?(field, *args)
  attributes.include?(field) || super
end