Created
November 23, 2018 07:46
-
-
Save ecmelkytz/d081e18ac242e2fce9c284fdec400eef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CalendarTitle < ApplicationRecord | |
cattr_accessor :definition_file | |
self.definition_file = "#{Rails.root}/db/static_data/event_titles.yml" | |
include Findable | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Findable | |
extend ActiveSupport::Concern | |
included do | |
findable = ActiveSupport::OrderedOptions.new | |
YAML.load_file(definition_file).each do |category, data| | |
data.each do |datum| | |
thing = Thing.new(path: [category], **datum.deep_symbolize_keys) | |
findable[thing.ident] = thing | |
end | |
end | |
class_attribute :findable, default: findable | |
end | |
class_methods do | |
def find_by_identifier(ident) | |
puts findable[ident].title | |
end | |
end | |
class Thing | |
attr_reader :path, :name, :title, :ident | |
def initialize(path:, name:, title:) | |
@path = path | |
@name = name | |
@title = title | |
@ident = build_identity | |
end | |
protected | |
PATH_SEPARATOR = '_' | |
def build_identity | |
[*path, name].map(&:to_s).join PATH_SEPARATOR | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment