|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "csv" |
| 4 | + |
| 5 | +module Perron |
| 6 | + class Data < SimpleDelegator |
| 7 | + def initialize(identifier) |
| 8 | + @file_path = path_for(identifier) |
| 9 | + @records = records |
| 10 | + |
| 11 | + super(records) |
| 12 | + end |
| 13 | + |
| 14 | + private |
| 15 | + |
| 16 | + PARSER_METHODS = { |
| 17 | + ".yml" => :parse_yaml, ".yaml" => :parse_yaml, |
| 18 | + ".json" => :parse_json, ".csv" => :parse_csv |
| 19 | + }.freeze |
| 20 | + SUPPORTED_EXTENSIONS = PARSER_METHODS.keys |
| 21 | + |
| 22 | + def path_for(identifier) |
| 23 | + path = Pathname.new(identifier) |
| 24 | + |
| 25 | + return path.to_s if path.file? && path.absolute? |
| 26 | + |
| 27 | + path = SUPPORTED_EXTENSIONS.lazy.map { Rails.root.join("app", "content", "data").join("#{identifier}#{it}") }.find(&:exist?) |
| 28 | + path&.to_s or raise Errors::FileNotFoundError, "No data file found for '#{identifier}'" |
| 29 | + end |
| 30 | + |
| 31 | + def records |
| 32 | + content = File.read(@file_path) |
| 33 | + extension = File.extname(@file_path) |
| 34 | + parser = PARSER_METHODS.fetch(extension) do |
| 35 | + raise Errors::UnsupportedDataFormatError, "Unsupported data format: #{extension}" |
| 36 | + end |
| 37 | + |
| 38 | + data = send(parser, content) |
| 39 | + |
| 40 | + unless data.is_a?(Array) |
| 41 | + raise Errors::DataParseError, "Data in '#{@file_path}' must be an array of objects." |
| 42 | + end |
| 43 | + |
| 44 | + data.map { Item.new(it) } |
| 45 | + rescue Psych::SyntaxError, JSON::ParserError, CSV::MalformedCSVError => error |
| 46 | + raise Errors::DataParseError, "Failed to parse '#{@file_path}': #{error.message}" |
| 47 | + end |
| 48 | + |
| 49 | + def parse_yaml(content) |
| 50 | + YAML.safe_load(content, permitted_classes: [Symbol], aliases: true) |
| 51 | + end |
| 52 | + |
| 53 | + def parse_json(content) |
| 54 | + JSON.parse(content, symbolize_names: true) |
| 55 | + end |
| 56 | + |
| 57 | + def parse_csv(content) |
| 58 | + CSV.new(content, headers: true, header_converters: :symbol).to_a.map(&:to_h) |
| 59 | + end |
| 60 | + |
| 61 | + class Item |
| 62 | + def initialize(attributes) |
| 63 | + @attributes = attributes.transform_keys(&:to_sym) |
| 64 | + end |
| 65 | + |
| 66 | + def [](key) = @attributes[key.to_sym] |
| 67 | + |
| 68 | + def method_missing(method_name, *arguments, &block) |
| 69 | + return super if !@attributes.key?(method_name) || arguments.any? || block |
| 70 | + |
| 71 | + @attributes[method_name] |
| 72 | + end |
| 73 | + |
| 74 | + def respond_to_missing?(method_name, include_private = false) |
| 75 | + @attributes.key?(method_name) || super |
| 76 | + end |
| 77 | + end |
| 78 | + private_constant :Item |
| 79 | + end |
| 80 | +end |
| 81 | + |
| 82 | +# require "csv" |
| 83 | + |
| 84 | +# module Perron |
| 85 | +# class Data |
| 86 | +# include Enumerable |
| 87 | + |
| 88 | +# def initialize(resource) |
| 89 | +# @file_path = path_for(resource) |
| 90 | +# @data = data |
| 91 | +# end |
| 92 | + |
| 93 | +# def each(&block) |
| 94 | +# @data.each(&block) |
| 95 | +# end |
| 96 | + |
| 97 | +# private |
| 98 | + |
| 99 | +# PARSER_METHODS = { |
| 100 | +# ".csv" => :parse_csv, |
| 101 | +# ".json" => :parse_json, |
| 102 | +# ".yaml" => :parse_yaml, |
| 103 | +# ".yml" => :parse_yaml |
| 104 | +# }.freeze |
| 105 | +# SUPPORTED_EXTENSIONS = PARSER_METHODS.keys.freeze |
| 106 | + |
| 107 | +# def path_for(identifier) |
| 108 | +# path = Pathname.new(identifier) |
| 109 | + |
| 110 | +# return path.to_s if path.file? && path.absolute? |
| 111 | + |
| 112 | +# found_path = SUPPORTED_EXTENSIONS.lazy.map do |extension| |
| 113 | +# Rails.root.join("app", "content", "data").join("#{identifier}#{extension}") |
| 114 | +# end.find(&:exist?) |
| 115 | + |
| 116 | +# found_path&.to_s or raise Errors::FileNotFoundError, "No data file found for '#{identifier}'" |
| 117 | +# end |
| 118 | + |
| 119 | +# def data |
| 120 | +# content = File.read(@file_path) |
| 121 | +# extension = File.extname(@file_path) |
| 122 | +# parser = PARSER_METHODS.fetch(extension) do |
| 123 | +# raise Errors::UnsupportedDataFormatError, "Unsupported data format: #{extension}" |
| 124 | +# end |
| 125 | + |
| 126 | +# raw_data = send(parser, content) |
| 127 | + |
| 128 | +# unless raw_data.is_a?(Array) |
| 129 | +# raise Errors::DataParseError, "Data in '#{@file_path}' must be an array of objects." |
| 130 | +# end |
| 131 | + |
| 132 | +# struct = Struct.new(*raw_data.first.keys, keyword_init: true) |
| 133 | +# raw_data.map { struct.new(**it) } |
| 134 | +# rescue Psych::SyntaxError, JSON::ParserError, CSV::MalformedCSVError => error |
| 135 | +# raise Errors::DataParseError, "Failed to parse '#{@file_path}': #{error.message}" |
| 136 | +# end |
| 137 | + |
| 138 | +# def parse_yaml(content) = YAML.safe_load(content, permitted_classes: [Symbol], aliases: true) |
| 139 | + |
| 140 | +# def parse_json(content) = JSON.parse(content, symbolize_names: true) |
| 141 | + |
| 142 | +# def parse_csv(content) = CSV.new(content, headers: true, header_converters: :symbol).to_a.map(&:to_h) |
| 143 | +# end |
| 144 | +# end |
0 commit comments