Class: RSpecFileFixtures::Fixture

Inherits:
Object
  • Object
show all
Defined in:
lib/rspec_file_fixtures/fixture.rb

Overview

Provides access to the contents and location of a file fixture.

Instance Method Summary collapse

Constructor Details

#initialize(raw_path) ⇒ Fixture

Returns a new instance of Fixture.



6
7
8
# File 'lib/rspec_file_fixtures/fixture.rb', line 6

def initialize(raw_path)
  @raw_path = raw_path
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (private)



55
56
57
58
59
# File 'lib/rspec_file_fixtures/fixture.rb', line 55

def method_missing(method_name, *args)
  return pathname.public_send(method_name, *args) if respond_to_missing?(method_name)

  super
end

Instance Method Details

#copy_to(destination) ⇒ Object



14
15
16
17
# File 'lib/rspec_file_fixtures/fixture.rb', line 14

def copy_to(destination)
  Pathname.new(destination).dirname.mkpath
  FileUtils.copy(pathname, destination)
end

#from_json(symbolize_names_shorthand = true, symbolize_names: true) ⇒ Object Also known as: json

rubocop:disable Style/OptionalBooleanParameter



35
36
37
# File 'lib/rspec_file_fixtures/fixture.rb', line 35

def from_json(symbolize_names_shorthand = true, symbolize_names: true)
  JSON.parse(read, symbolize_names: symbolize_names & symbolize_names_shorthand)
end

#from_xmlObject Also known as: xml



23
24
25
26
# File 'lib/rspec_file_fixtures/fixture.rb', line 23

def from_xml
  require 'nokogiri'
  Nokogiri::XML.parse(read)
end

#from_yaml(symbolize_names_shorthand = true, symbolize_names: true) ⇒ Object Also known as: from_yml, yaml



39
40
41
# File 'lib/rspec_file_fixtures/fixture.rb', line 39

def from_yaml(symbolize_names_shorthand = true, symbolize_names: true)
  YAML.safe_load(read, symbolize_names: symbolize_names & symbolize_names_shorthand)
end

#pathObject



28
29
30
31
32
# File 'lib/rspec_file_fixtures/fixture.rb', line 28

def path
  pathname.realpath.to_s
rescue Errno::ENOENT
  pathname.expand_path.to_s
end

#readObject



10
11
12
# File 'lib/rspec_file_fixtures/fixture.rb', line 10

def read
  pathname.read
end

#upload(content_type:) ⇒ Object



19
20
21
# File 'lib/rspec_file_fixtures/fixture.rb', line 19

def upload(content_type:)
  Rack::Test::UploadedFile.new(path, content_type)
end