Skip to content

Instantly share code, notes, and snippets.

@ecmelkytz
Created November 23, 2018 07:46
Show Gist options
  • Save ecmelkytz/d081e18ac242e2fce9c284fdec400eef to your computer and use it in GitHub Desktop.
Save ecmelkytz/d081e18ac242e2fce9c284fdec400eef to your computer and use it in GitHub Desktop.
class CalendarTitle < ApplicationRecord
cattr_accessor :definition_file
self.definition_file = "#{Rails.root}/db/static_data/event_titles.yml"
include Findable
end
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