Class: RSpecHTTPFixtures::FixtureNamespace
- Inherits:
-
Object
- Object
- RSpecHTTPFixtures::FixtureNamespace
- Defined in:
- lib/rspec-http-fixtures/fixture-namespace.rb
Instance Method Summary collapse
- #build_fixture_path(path) ⇒ Object
-
#build_json_boolean(value, default = nil) ⇒ Object
Returns boolean value or null if nil for json fixtures.
- #build_json_collection_fixture(fixture, params_collection = []) ⇒ Object
-
#build_json_data(value, default = nil) ⇒ Object
To be used with arrays and objects for json fixtures and returns the actual data or null if nil.
-
#build_json_data_pairs(pairs) ⇒ Object
Used to build JSON pairs to add to an existing object.
- #build_json_epoch(value) ⇒ Object
- #build_json_iso8601(value, default = nil, precision: 0) ⇒ Object
- #build_json_null(value) ⇒ Object
-
#build_json_number(value, default = nil) ⇒ Object
Returns integer value or null if nil for json fixtures.
-
#build_json_string(value, default = nil) ⇒ Object
Returns string value enclosed in quotes or null if nil for json fixtures.
- #build_xml_collection_fixture(fixture, params_collection = []) ⇒ Object
-
#initialize(context:, path:, params: {}) ⇒ FixtureNamespace
constructor
A new instance of FixtureNamespace.
-
#method_missing(name, *args) ⇒ Object
rubocop:disable Style/MissingRespondToMissing.
- #set(key, value) ⇒ Object
Constructor Details
#initialize(context:, path:, params: {}) ⇒ FixtureNamespace
3 4 5 6 7 8 9 10 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 3 def initialize(context:, path:, params: {}) @context = context @path = path params.each do |key, value| set(key, value) end end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
rubocop:disable Style/MissingRespondToMissing
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 95 def method_missing(name, *args) instance_variable = "@#{name}" # First try to see if it's a param we're trying to access which is stored in an instance variable otherwise # try to see if there's a method currently on class (from includes) if instance_variable_defined?(instance_variable) instance_variable_get(instance_variable) elsif respond_to?(name) send(name, *args) end end |
Instance Method Details
#build_fixture_path(path) ⇒ Object
16 17 18 19 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 16 def build_fixture_path(path) # Can convert relative path to absolute path "#{File.(path, File.dirname(@path))}.erb" end |
#build_json_boolean(value, default = nil) ⇒ Object
Returns boolean value or null if nil for json fixtures. Can also be forced to be null by passing in the string: “null”. Can provide default if value is nil
65 66 67 68 69 70 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 65 def build_json_boolean(value, default = nil) return "null" if value == "null" return value unless value.nil? default.is_a?(TrueClass) || default.is_a?(FalseClass) ? default : "null" end |
#build_json_collection_fixture(fixture, params_collection = []) ⇒ Object
86 87 88 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 86 def build_json_collection_fixture(fixture, params_collection = []) @context.build_json_collection_http_fixture(build_fixture_path(fixture), params_collection) end |
#build_json_data(value, default = nil) ⇒ Object
To be used with arrays and objects for json fixtures and returns the actual data or null if nil
22 23 24 25 26 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 22 def build_json_data(value, default = nil) value ||= default build_json_null(value) || (value.is_a?(String) ? value : value.to_json) end |
#build_json_data_pairs(pairs) ⇒ Object
Used to build JSON pairs to add to an existing object
42 43 44 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 42 def build_json_data_pairs(pairs) build_json_data(pairs)[1..-2] end |
#build_json_epoch(value) ⇒ Object
78 79 80 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 78 def build_json_epoch(value) build_json_number(value&.to_i) end |
#build_json_iso8601(value, default = nil, precision: 0) ⇒ Object
72 73 74 75 76 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 72 def build_json_iso8601(value, default = nil, precision: 0) value ||= default build_json_null(value) || build_json_string(value.is_a?(String) ? value : value.iso8601(precision)) end |
#build_json_null(value) ⇒ Object
82 83 84 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 82 def build_json_null(value) value.nil? || value == "null" ? "null" : nil end |
#build_json_number(value, default = nil) ⇒ Object
Returns integer value or null if nil for json fixtures. Can also be forced to be null by passing in the string: “null”
57 58 59 60 61 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 57 def build_json_number(value, default = nil) value ||= default build_json_null(value) || value end |
#build_json_string(value, default = nil) ⇒ Object
Returns string value enclosed in quotes or null if nil for json fixtures. Can also be forced to be null by passing in the string: “null”
48 49 50 51 52 53 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 48 def build_json_string(value, default = nil) value ||= default # `dump` ensures everything is escaped (e.g. \ is converted to \\) and will also include escaped quotes around the string build_json_null(value) || value.to_s.dump end |
#build_xml_collection_fixture(fixture, params_collection = []) ⇒ Object
90 91 92 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 90 def build_xml_collection_fixture(fixture, params_collection = []) params_collection.map { |p| @context.read_http_fixture(build_fixture_path(fixture), p) }.join("") end |
#set(key, value) ⇒ Object
12 13 14 |
# File 'lib/rspec-http-fixtures/fixture-namespace.rb', line 12 def set(key, value) instance_variable_set("@#{key}", value) end |