class Fixturama::Loader
@private Load fixture with some options
Constants
- JSON_EXTENSION
- YAML_EXTENSION
Matchers for YAML/YML/JSON in file extension like “data.yml.erb” etc.
Public Class Methods
new(example, path, opts = {})
click to toggle source
# File lib/fixturama/loader.rb 18 def initialize(example, path, opts = {}) 19 @example = example 20 @path = path 21 @opts = opts.to_h 22 end
Public Instance Methods
call()
click to toggle source
# File lib/fixturama/loader.rb 9 def call 10 return load_yaml if yaml? 11 return load_json if json? 12 13 content 14 end
Private Instance Methods
basename()
click to toggle source
# File lib/fixturama/loader.rb 24 def basename 25 @basename ||= Pathname.new(@path).basename.to_s 26 end
content()
click to toggle source
# File lib/fixturama/loader.rb 40 def content 41 bindings = context.instance_eval { binding } 42 content = File.read(@path) 43 44 ERB.new(content).result(bindings) 45 end
context()
click to toggle source
# File lib/fixturama/loader.rb 36 def context 37 @context ||= Context.new(@example, @opts) 38 end
finalize(data)
click to toggle source
Takes the nested data loaded from YAML or JSON-formatted fixture, and serializes its leafs to the corresponding values from a context
# File lib/fixturama/loader.rb 57 def finalize(data) 58 case data 59 when Array 60 data.map { |val| finalize(val) } 61 when Hash 62 data.each_with_object({}) { |(key, val), obj| obj[key] = finalize(val) } 63 when String 64 finalize_string(data) 65 else 66 data 67 end 68 end
finalize_string(string)
click to toggle source
Converts strings of sort `#<Fixturama::Loader::Context>` to the corresponding value by the key @param [String] string @return [Object]
# File lib/fixturama/loader.rb 74 def finalize_string(string) 75 Marshal.restore(string) 76 rescue StandardError 77 key = string.match(Value::MATCHER)&.captures&.first&.to_s 78 key ? context[key] : string 79 end
json?()
click to toggle source
# File lib/fixturama/loader.rb 32 def json? 33 !basename[JSON_EXTENSION].nil? 34 end
load_json()
click to toggle source
# File lib/fixturama/loader.rb 51 def load_json 52 finalize JSON.parse(content) 53 end
load_yaml()
click to toggle source
# File lib/fixturama/loader.rb 47 def load_yaml 48 finalize YAML.load(content) 49 end
yaml?()
click to toggle source
# File lib/fixturama/loader.rb 28 def yaml? 29 !basename[YAML_EXTENSION].nil? 30 end