diff --git a/.circleci/config.yml b/.circleci/config.yml index 4845990..c1e2235 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -33,6 +33,9 @@ jobs: - run: name: Bundle install command: bundle install + - run: + name: Standard + command: bundle exec standard - run: name: Rspec command: bundle exec rspec diff --git a/.standard.yml b/.standard.yml new file mode 100644 index 0000000..8fe1b87 --- /dev/null +++ b/.standard.yml @@ -0,0 +1,2 @@ +ignore: + - 'performance/**/*' diff --git a/Gemfile b/Gemfile index 8b9f464..8f96582 100644 --- a/Gemfile +++ b/Gemfile @@ -1,14 +1,15 @@ -source 'https://rubygems.org' +source "https://rubygems.org" gemspec -gem 'rspec' -gem 'byebug' -gem 'rails', '~> 6.0.0' -gem 'analyzer', git: 'https://github.com/thoughtbot/analyzer.git' -gem 'multi_json' -gem 'rabl' -gem 'turbostreamer' -gem 'jbuilder' -gem 'active_model_serializers' -gem 'jsonapi-serializer' +gem "rspec" +gem "byebug" +gem "rails", "~> 6.0.0" +gem "analyzer", git: "https://github.com/thoughtbot/analyzer.git" +gem "multi_json" +gem "rabl" +gem "turbostreamer" +gem "jbuilder" +gem "active_model_serializers" +gem "jsonapi-serializer" +gem "standard", group: [:development, :test] diff --git a/Rakefile b/Rakefile index 558e191..ae0c7e7 100644 --- a/Rakefile +++ b/Rakefile @@ -1,37 +1,37 @@ -require 'bundler/setup' +require "bundler/setup" require "bundler/gem_tasks" - -require 'fileutils' +require "standard/rake" +require "fileutils" require "rake/testtask" task :performance do - require 'analyzer' + require "analyzer" base = File.expand_path("../performance/rolftimmermans", __FILE__) - output_file = File.join(base, 'report.png') + output_file = File.join(base, "report.png") files = [ - 'props_template/oj.rb', - 'jbuilder/oj.rb', - 'rabl/oj.rb', - 'ams/oj.rb', - 'fast_jsonapi/oj.rb', - 'turbostreamer/oj.rb', + "props_template/oj.rb", + "jbuilder/oj.rb", + "rabl/oj.rb", + "ams/oj.rb", + "fast_jsonapi/oj.rb", + "turbostreamer/oj.rb" # 'props_base/oj.rb', # 'poro/oj.rb', # 'just_oj/oj.rb', - ].map{ |i| File.join(base, i) } - analyzer = Analyzer.new(*files, lib: File.join(base, 'lib.rb')) + ].map { |i| File.join(base, i) } + analyzer = Analyzer.new(*files, lib: File.join(base, "lib.rb")) analyzer.plot(output_file) base = File.expand_path("../performance/dirk", __FILE__) - output_file = File.join(base, 'report.png') + output_file = File.join(base, "report.png") files = [ - 'props_template/oj.rb', - 'ams/oj.rb', - 'rabl/oj.rb', - 'jbuilder/oj.rb', - 'turbostreamer/oj.rb', - ].map{ |i| File.join(base, i) } - analyzer = Analyzer.new(*files, lib: File.join(base, 'lib.rb')) + "props_template/oj.rb", + "ams/oj.rb", + "rabl/oj.rb", + "jbuilder/oj.rb", + "turbostreamer/oj.rb" + ].map { |i| File.join(base, i) } + analyzer = Analyzer.new(*files, lib: File.join(base, "lib.rb")) analyzer.plot(output_file) end diff --git a/lib/props_template.rb b/lib/props_template.rb index cf5f04e..88eba7d 100644 --- a/lib/props_template.rb +++ b/lib/props_template.rb @@ -1,17 +1,17 @@ -require 'props_template/base' -require 'props_template/extensions/partial_renderer' -require 'props_template/extensions/cache' -require 'props_template/extensions/deferment' -require 'props_template/extensions/fragment' -require 'props_template/base_with_extensions' -require 'props_template/extension_manager' -require 'active_support/core_ext/string/output_safety' -require 'active_support/core_ext/array' -require 'props_template/searcher' -require 'props_template/handler' -require 'props_template/version' - -require 'active_support' +require "props_template/base" +require "props_template/extensions/partial_renderer" +require "props_template/extensions/cache" +require "props_template/extensions/deferment" +require "props_template/extensions/fragment" +require "props_template/base_with_extensions" +require "props_template/extension_manager" +require "active_support/core_ext/string/output_safety" +require "active_support/core_ext/array" +require "props_template/searcher" +require "props_template/handler" +require "props_template/version" + +require "active_support" module Props class Template @@ -19,7 +19,7 @@ class << self attr_accessor :template_lookup_options end - self.template_lookup_options = { handlers: [:props] } + self.template_lookup_options = {handlers: [:props]} delegate :result!, :array!, :deferred!, @@ -34,7 +34,7 @@ def initialize(context = nil, options = {}) end def set!(key, options = {}, &block) - if block_given? && options[:search] && !@builder.is_a?(Searcher) + if block && options[:search] && !@builder.is_a?(Searcher) prev_builder = @builder @builder = Searcher.new(self, options[:search], @context) @@ -60,4 +60,4 @@ def builder! end end -require 'props_template/railtie' if defined?(Rails) +require "props_template/railtie" if defined?(Rails) diff --git a/lib/props_template/base.rb b/lib/props_template/base.rb index 0a78f3a..650bd62 100644 --- a/lib/props_template/base.rb +++ b/lib/props_template/base.rb @@ -1,8 +1,9 @@ -require 'oj' -require 'active_support' +require "oj" +require "active_support" module Props class InvalidScopeForArrayError < StandardError; end + class InvalidScopeForObjError < StandardError; end class Base @@ -34,7 +35,7 @@ def format_key(key) def set!(key, value = nil) if @scope == :array - raise InvalidScopeForObjError.new('Attempted to set! on an array! scope') + raise InvalidScopeForObjError.new("Attempted to set! on an array! scope") end if @scope.nil? @@ -80,19 +81,19 @@ def handle_collection(collection, options) collection.each_with_index do |item, index| pass_opts = all_opts[index] handle_collection_item(collection, item, index, pass_opts) do - #todo: remove index? + # todo: remove index? yield item, index end end end - #todo, add ability to define contents of array + # todo, add ability to define contents of array def array!(collection, options = {}) if @scope.nil? @scope = :array @stream.push_array else - raise InvalidScopeForArrayError.new('array! expects exclusive use of this block') + raise InvalidScopeForArrayError.new("array! expects exclusive use of this block") end handle_collection(collection, options) do |item, index| @@ -107,10 +108,8 @@ def array!(collection, options = {}) def result! if @scope.nil? @stream.push_object - @stream.pop - else - @stream.pop end + @stream.pop json = @stream.raw_json @stream.reset diff --git a/lib/props_template/base_with_extensions.rb b/lib/props_template/base_with_extensions.rb index 34d3034..017fe85 100644 --- a/lib/props_template/base_with_extensions.rb +++ b/lib/props_template/base_with_extensions.rb @@ -5,7 +5,7 @@ class BaseWithExtensions < Base def initialize(builder, context = nil, options = {}) @context = context @builder = builder - #todo: refactor so deferred can be its own class + # todo: refactor so deferred can be its own class @em = ExtensionManager.new(self) @traveled_path = [] @key_cache = {} @@ -50,7 +50,7 @@ def format_key(key) end def set!(key, options = {}, &block) - if block_given? + if block options = @em.refine_options(options) end @@ -60,7 +60,7 @@ def set!(key, options = {}, &block) def handle_set_block(key, options) @traveled_path.push(key) n = 1 - if suffix = options[:path_suffix] + if (suffix = options[:path_suffix]) n += suffix.length @traveled_path.push(suffix) end @@ -68,7 +68,7 @@ def handle_set_block(key, options) super @traveled_path.pop(n) - return + nil end def handle_collection_item(collection, item, index, options) @@ -80,14 +80,14 @@ def handle_collection_item(collection, item, index, options) if id.nil? @traveled_path.push(index) else - @traveled_path.push("#{id.to_s}=#{val}") + @traveled_path.push("#{id}=#{val}") end end super @traveled_path.pop - return + nil end def refine_all_item_options(all_options) @@ -97,7 +97,7 @@ def refine_all_item_options(all_options) def refine_item_options(item, options) return options if options.empty? - if key = options[:key] + if (key = options[:key]) val = if item.respond_to? key item.send(key) elsif item.is_a? Hash @@ -111,4 +111,3 @@ def refine_item_options(item, options) end end end - diff --git a/lib/props_template/core_ext.rb b/lib/props_template/core_ext.rb index 2501086..30fe72b 100644 --- a/lib/props_template/core_ext.rb +++ b/lib/props_template/core_ext.rb @@ -7,7 +7,7 @@ def member_at(index) end def member_by(attribute, value) - raise NotImplementedError, 'Implement member_by(attr, value) in your own delegate' + raise NotImplementedError, "Implement member_by(attr, value) in your own delegate" end end end diff --git a/lib/props_template/debug_writer.rb b/lib/props_template/debug_writer.rb deleted file mode 100644 index 9ba1960..0000000 --- a/lib/props_template/debug_writer.rb +++ /dev/null @@ -1,55 +0,0 @@ -require 'oj' -require 'active_support' -require 'byebug' - -module Props - class DebugWriter - attr_accessor :commands - - def initialize(opts) - @stream = Oj::StringWriter.new(opts) - @commands = [] - end - - def push_object - @commands.push([:push_object]) - end - - def push_key(key) - @commands.push([:push_key, key]) - end - - def push_value(value, key = nil) - if key - @commands.push([:push_value, value, key]) - else - @commands.push([:push_value, value]) - end - end - - def push_array - @commands.push([:push_array]) - end - - def pop - @commands.push([:pop]) - end - - def reset - @commands = [] - @stream.reset - end - - def to_s - @commands.each do |command| - begin - @stream.send(*command) - rescue => e - byebug - end - end - - @stream.to_s - end - end -end diff --git a/lib/props_template/dependency_tracker.rb b/lib/props_template/dependency_tracker.rb index bf82e7c..66dda66 100644 --- a/lib/props_template/dependency_tracker.rb +++ b/lib/props_template/dependency_tracker.rb @@ -1,16 +1,15 @@ # This was taken from jbuilder -require 'props_template' - +require "props_template" dependency_tracker = false begin - require 'action_view' - require 'action_view/dependency_tracker' + require "action_view" + require "action_view/dependency_tracker" dependency_tracker = ::ActionView::DependencyTracker rescue LoadError begin - require 'cache_digests' + require "cache_digests" dependency_tracker = ::CacheDigests::DependencyTracker rescue LoadError end diff --git a/lib/props_template/extension_manager.rb b/lib/props_template/extension_manager.rb index 18116cc..0a785c7 100644 --- a/lib/props_template/extension_manager.rb +++ b/lib/props_template/extension_manager.rb @@ -2,7 +2,7 @@ module Props class ExtensionManager attr_reader :base, :builder, :context - def initialize(base, defered=[], fragments=[]) + def initialize(base, defered = [], fragments = []) @base = base @context = base.context @builder = base.builder @@ -15,16 +15,14 @@ def initialize(base, defered=[], fragments=[]) def refine_options(options, item = nil) options = @partialer.refine_options(options, item) options = @deferment.refine_options(options, item) - options = Cache.refine_options(options, item) - options + Cache.refine_options(options, item) end def refine_all_item_options(all_options) return all_options if all_options.empty? all_options = @partialer.find_and_add_template(all_options) - all_options = @cache.multi_fetch_and_add_results(all_options) - all_options + @cache.multi_fetch_and_add_results(all_options) end def deferred @@ -79,10 +77,10 @@ def handle_cache(options) yield meta = Oj.dump([deferred_paths, fragment_paths]).strip json_in_progress = base.stream.to_s - if json_in_progress[start] == ',' + if json_in_progress[start] == "," start += 1 end - raw = base.stream.to_s[start..-1].strip + raw = base.stream.to_s[start..].strip result = "#{meta}\n#{raw}" } result diff --git a/lib/props_template/extensions/cache.rb b/lib/props_template/extensions/cache.rb index 4f139a8..da6a239 100644 --- a/lib/props_template/extensions/cache.rb +++ b/lib/props_template/extensions/cache.rb @@ -21,9 +21,7 @@ def initialize(context) @context = context end - def context - @context - end + attr_reader :context def multi_fetch(keys, options = {}) result = {} @@ -38,12 +36,12 @@ def multi_fetch(keys, options = {}) payload = { controller_name: controller.controller_name, - action_name: controller.action_name, + action_name: controller.action_name } read_caches = {} - ActiveSupport::Notifications.instrument('read_multi_fragments.action_view', payload) do |payload| + ActiveSupport::Notifications.instrument("read_multi_fragments.action_view", payload) do |payload| read_caches = ::Rails.cache.read_multi(*ckeys, options) payload[:read_caches] = read_caches end @@ -60,31 +58,30 @@ def multi_fetch_and_add_results(all_options) first_opts = all_options[0] if first_opts[:cache] && controller.perform_caching - keys = all_options.map{|i| i[:cache][0]} + keys = all_options.map { |i| i[:cache][0] } c_opts = first_opts[:cache][1] result = multi_fetch(keys, c_opts) all_options.map do |opts| - key = opts[:cache][0] + key = opts[:cache][0] if result.key? key opts[:cache][1][:result] = result[key] - opts - else - opts end + + opts end else all_options end end - #Copied from jbuilder + # Copied from jbuilder # - def cache(key=nil, options={}) + def cache(key = nil, options = {}) if controller.perform_caching - value = cache_fragment_for(key, options) do + cache_fragment_for(key, options) do yield end else @@ -120,12 +117,11 @@ def cache_key(key, options) if @context.respond_to?(:combined_fragment_cache_key) key = @context.combined_fragment_cache_key(key) - else - key = url_for(key).split('://', 2).last if ::Hash === key + elsif ::Hash === key + key = url_for(key).split("://", 2).last end ::ActiveSupport::Cache.expand_cache_key(key, :props) end end end - diff --git a/lib/props_template/extensions/deferment.rb b/lib/props_template/extensions/deferment.rb index 2bca240..8891432 100644 --- a/lib/props_template/extensions/deferment.rb +++ b/lib/props_template/extensions/deferment.rb @@ -17,7 +17,7 @@ def refine_options(options, item = nil) } if item - type = Proc === type ? type.call(item) : type + type = (Proc === type) ? type.call(item) : type end if type @@ -44,10 +44,10 @@ def handle(options) end request_path = @base.context.controller.request.fullpath - path = @base.traveled_path.join('.') + path = @base.traveled_path.join(".") uri = ::URI.parse(request_path) - qry = ::URI.decode_www_form(uri.query || '') - .reject{|x| x[0] == 'props_at' } + qry = ::URI.decode_www_form(uri.query || "") + .reject { |x| x[0] == "props_at" } .push(["props_at", path]) uri.query = ::URI.encode_www_form(qry) @@ -55,7 +55,7 @@ def handle(options) deferral = { url: uri.to_s, path: path, - type: type.to_s, + type: type.to_s } # camelize for JS land diff --git a/lib/props_template/extensions/fragment.rb b/lib/props_template/extensions/fragment.rb index 1500652..96faec5 100644 --- a/lib/props_template/extensions/fragment.rb +++ b/lib/props_template/extensions/fragment.rb @@ -2,7 +2,7 @@ module Props class Fragment attr_reader :fragments - def initialize(base, fragments=[]) + def initialize(base, fragments = []) @base = base @fragments = fragments end @@ -14,11 +14,11 @@ def handle(options) if String === fragment || Symbol === fragment fragment_name = fragment.to_s - path = @base.traveled_path.join('.') + path = @base.traveled_path.join(".") @name = fragment_name @fragments.push( - { type: fragment_name, partial: partial_name, path: path } + {type: fragment_name, partial: partial_name, path: path} ) end end diff --git a/lib/props_template/extensions/partial_renderer.rb b/lib/props_template/extensions/partial_renderer.rb index 66575da..823dd20 100644 --- a/lib/props_template/extensions/partial_renderer.rb +++ b/lib/props_template/extensions/partial_renderer.rb @@ -1,4 +1,4 @@ -require 'action_view' +require "action_view" module Props class RenderedTemplate @@ -23,7 +23,6 @@ class Partialer IDENTIFIER_ERROR_MESSAGE = "The partial name (%s) is not a valid Ruby identifier; " \ "make sure your partial name starts with underscore." - def initialize(base, context, builder) @context = context @builder = builder @@ -67,7 +66,7 @@ def find_template(partial_opts) template_keys = retrieve_template_keys(partial_opts) details = extract_details(partial_opts) - prefixes = partial.include?(?/) ? [] : @context.lookup_context.prefixes + prefixes = partial.include?("/") ? [] : @context.lookup_context.prefixes @context.lookup_context.find_template(partial, prefixes, true, template_keys, details) end @@ -86,7 +85,7 @@ def block_opts_to_render_opts(builder, options) pass_opts.delete(:handlers) if !(String === partial) - raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % (partial.inspect)) + raise ArgumentError.new(INVALID_PARTIAL_MESSAGE % partial.inspect) end pass_opts @@ -120,15 +119,15 @@ def instrument(name, **options) # :doc: end def raise_invalid_option_as(as) - raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % (as)) + raise ArgumentError.new(OPTION_AS_ERROR_MESSAGE % as) end def raise_invalid_identifier(path) - raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % (path)) + raise ArgumentError.new(IDENTIFIER_ERROR_MESSAGE % path) end def retrieve_variable(path) - base = path[-1] == "/" ? "" : File.basename(path) + base = (path[-1] == "/") ? "" : File.basename(path) raise_invalid_identifier(path) unless base =~ /\A_?(.*?)(?:\.\w+)*\z/ $1.to_sym end @@ -152,7 +151,7 @@ def refine_options(options, item = nil) locals[as] = item - if fragment_name = rest[:fragment] + if (fragment_name = rest[:fragment]) rest[:fragment] = fragment_name.to_s end end diff --git a/lib/props_template/handler.rb b/lib/props_template/handler.rb index cf0481c..79d71b2 100644 --- a/lib/props_template/handler.rb +++ b/lib/props_template/handler.rb @@ -1,4 +1,4 @@ -require 'active_support' +require "active_support" module Props class Handler diff --git a/lib/props_template/railtie.rb b/lib/props_template/railtie.rb index 0981e90..b8ce520 100644 --- a/lib/props_template/railtie.rb +++ b/lib/props_template/railtie.rb @@ -1,13 +1,13 @@ -require 'rails/railtie' -require 'props_template' +require "rails/railtie" +require "props_template" module Props class Railtie < ::Rails::Railtie initializer :props_template do ActiveSupport.on_load :action_view do ActionView::Template.register_template_handler :props, Props::Handler - require 'props_template/dependency_tracker' - require 'props_template/layout_patch' + require "props_template/dependency_tracker" + require "props_template/layout_patch" end end end diff --git a/lib/props_template/searcher.rb b/lib/props_template/searcher.rb index 1d87841..9518ac5 100644 --- a/lib/props_template/searcher.rb +++ b/lib/props_template/searcher.rb @@ -2,7 +2,7 @@ module Props class Searcher attr_reader :builder, :context, :fragments, :traveled_path - def initialize(builder, path=[], context = nil) + def initialize(builder, path = [], context = nil) @search_path = path @depth = 0 @context = context @@ -24,7 +24,7 @@ def fragments! def found! pass_opts = @found_options.clone || {} pass_opts.delete(:defer) - traveled_path = @traveled_path[1..-1] || [] + traveled_path = @traveled_path[1..] || [] if !traveled_path.empty? pass_opts[:path_suffix] = traveled_path end @@ -36,8 +36,8 @@ def set_block_content!(*args) yield end - def set!(key, options={}, &block) - return if @found_block || !block_given? + def set!(key, options = {}, &block) + return if @found_block || !block if @search_path[@depth] == key.to_s @traveled_path.push(key) @@ -64,7 +64,7 @@ def array!(collection, options = {}, &block) return if @found_block key_index = @search_path[@depth] - id_name, id_val = key_index.to_s.split('=') + id_name, id_val = key_index.to_s.split("=") if id_val id_val = id_val.to_i @@ -80,7 +80,7 @@ def array!(collection, options = {}, &block) if @depth == @search_path.size - 1 @found_options = pass_opts - @found_block = Proc.new { + @found_block = proc { yield item, 0 } return diff --git a/props_template.gemspec b/props_template.gemspec index e657320..09f6ef8 100644 --- a/props_template.gemspec +++ b/props_template.gemspec @@ -2,20 +2,19 @@ $LOAD_PATH << File.expand_path("lib", __dir__) require "props_template/version" Gem::Specification.new do |s| - s.name = 'props_template' - s.version = Props::VERSION - s.author = 'Johny Ho' - s.email = 'johny@thoughtbot.com' - s.license = 'MIT' - s.homepage = 'https://github.com/thoughtbot/props_template/' - s.summary = 'A fast JSON builder' - s.description = 'PropsTemplate is a direct-to-Oj, JBuilder-like DSL for building JSON. It has support for Russian-Doll caching, layouts, and can be queried by giving the root a key path.' - s.files = Dir['MIT-LICENSE', 'README.md', 'lib/**/*'] - s.test_files = Dir["spec/*"] + s.name = "props_template" + s.version = Props::VERSION + s.author = "Johny Ho" + s.email = "johny@thoughtbot.com" + s.license = "MIT" + s.homepage = "https://github.com/thoughtbot/props_template/" + s.summary = "A fast JSON builder" + s.description = "PropsTemplate is a direct-to-Oj, JBuilder-like DSL for building JSON. It has support for Russian-Doll caching, layouts, and can be queried by giving the root a key path." + s.files = Dir["MIT-LICENSE", "README.md", "lib/**/*"] - s.required_ruby_version = '>= 2.5' + s.required_ruby_version = ">= 2.5" - s.add_dependency 'activesupport', '>= 6.0.0' - s.add_dependency 'actionview', '>= 6.0.0' - s.add_dependency 'oj', '>= 3.9' + s.add_dependency "activesupport", ">= 6.0.0" + s.add_dependency "actionview", ">= 6.0.0" + s.add_dependency "oj", ">= 3.9" end diff --git a/spec/extensions/defer_extension_spec.rb b/spec/extensions/defer_extension_spec.rb index 1f90623..6355983 100644 --- a/spec/extensions/defer_extension_spec.rb +++ b/spec/extensions/defer_extension_spec.rb @@ -1,13 +1,13 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template' do +RSpec.describe "Props::Template" do before do Rails.cache.clear - @controller.request.path = '/some_url' + @controller.request.path = "/some_url" end - it 'defers a block from loading' do + it "defers a block from loading" do json = render(<<~PROPS) json.outer do json.inner(defer: :auto) do @@ -25,12 +25,12 @@ inner: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ] }) end - it 'defers a block from loading, and replaces with a custom placeholder' do + it "defers a block from loading, and replaces with a custom placeholder" do json = render(<<~PROPS) json.outer do json.inner(defer: [:auto, placeholder: []]) do @@ -48,12 +48,12 @@ inner: [] }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ] }) end - it 'defers a block from loading, and passes success and fail types' do + it "defers a block from loading, and passes success and fail types" do json = render(<<~PROPS) json.outer do json.inner(defer: [:auto, success_action: 'SUCCESS', fail_action: 'FAIL']) do @@ -71,12 +71,12 @@ inner: nil }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto', successAction: 'SUCCESS', failAction: 'FAIL'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto", successAction: "SUCCESS", failAction: "FAIL"} ] }) end - it 'defers a block from loading and populates with a manual type' do + it "defers a block from loading and populates with a manual type" do json = render(<<~PROPS) json.outer do json.inner(defer: :manual) do @@ -94,12 +94,12 @@ inner: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'manual'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "manual"} ] }) end - it 'defers siblings from loading' do + it "defers siblings from loading" do json = render(<<~PROPS) json.outer do json.inner(defer: :auto) do @@ -117,13 +117,13 @@ inner2: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'}, - {url: '/some_url?props_at=outer.inner2', path: 'outer.inner2', type: 'auto'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"}, + {url: "/some_url?props_at=outer.inner2", path: "outer.inner2", type: "auto"} ] }) end - it 'defers an array from loading' do + it "defers an array from loading" do json = render(<<~PROPS) json.outer do json.inner(defer: :auto) do @@ -141,12 +141,12 @@ inner: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'}, + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ] }) end - it 'defers array elements from loading, replacing it with an empty obj' do + it "defers array elements from loading, replacing it with an empty obj" do json = render(<<~PROPS) json.outer do json.inner do @@ -169,13 +169,13 @@ ] }, deferred: [ - {url: '/some_url?props_at=outer.inner.0', path: 'outer.inner.0', type: 'auto'}, - {url: '/some_url?props_at=outer.inner.1', path: 'outer.inner.1', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.0", path: "outer.inner.0", type: "auto"}, + {url: "/some_url?props_at=outer.inner.1", path: "outer.inner.1", type: "auto"} ] }) end - it 'defers array elements from loading, populating with a id=xyz when collection responds_to :key and the array element is a hash' do + it "defers array elements from loading, populating with a id=xyz when collection responds_to :key and the array element is a hash" do json = render(<<~PROPS) collection = [ {id: 1}, @@ -205,13 +205,13 @@ ] }, deferred: [ - {url: '/some_url?props_at=outer.inner.id%3D1', path: 'outer.inner.id=1', type: 'auto'}, - {url: '/some_url?props_at=outer.inner.id%3D2', path: 'outer.inner.id=2', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.id%3D1", path: "outer.inner.id=1", type: "auto"}, + {url: "/some_url?props_at=outer.inner.id%3D2", path: "outer.inner.id=2", type: "auto"} ] }) end - it 'defers array elements from loading, populating with a id=xyz when collection responds_to :key and the array element is an object' do + it "defers array elements from loading, populating with a id=xyz when collection responds_to :key and the array element is an object" do json = render(<<~PROPS) klass = Struct.new(:id) collection = [ @@ -242,13 +242,13 @@ ] }, deferred: [ - {url: '/some_url?props_at=outer.inner.id%3D1', path: 'outer.inner.id=1', type: 'auto'}, - {url: '/some_url?props_at=outer.inner.id%3D2', path: 'outer.inner.id=2', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.id%3D1", path: "outer.inner.id=1", type: "auto"}, + {url: "/some_url?props_at=outer.inner.id%3D2", path: "outer.inner.id=2", type: "auto"} ] }) end - it 'manually defers array elements from loading, replacing it with an empty obj, and populating deferred with a manual type' do + it "manually defers array elements from loading, replacing it with an empty obj, and populating deferred with a manual type" do json = render(<<~PROPS) json.outer do json.inner do @@ -271,13 +271,13 @@ ] }, deferred: [ - {url: '/some_url?props_at=outer.inner.0', path: 'outer.inner.0', type: 'manual'}, - {url: '/some_url?props_at=outer.inner.1', path: 'outer.inner.1', type: 'manual'}, + {url: "/some_url?props_at=outer.inner.0", path: "outer.inner.0", type: "manual"}, + {url: "/some_url?props_at=outer.inner.1", path: "outer.inner.1", type: "manual"} ] }) end - it 'selectively defers an array element from loading' do + it "selectively defers an array element from loading" do json = render(<<~PROPS) json.outer do json.inner do @@ -301,12 +301,12 @@ ] }, deferred: [ - {url: '/some_url?props_at=outer.inner.0', path: 'outer.inner.0', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.0", path: "outer.inner.0", type: "auto"} ] }) end - it 'defers multiple nodes at different depths' do + it "defers multiple nodes at different depths" do json = render(<<~PROPS) json.outer do json.inner do @@ -334,13 +334,13 @@ sibling: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner.0', path: 'outer.inner.0', type: 'auto'}, - {url: '/some_url?props_at=outer.sibling', path: 'outer.sibling', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.0", path: "outer.inner.0", type: "auto"}, + {url: "/some_url?props_at=outer.sibling", path: "outer.sibling", type: "auto"} ] }) end - it 'does not defer nested nodes' do + it "does not defer nested nodes" do json = render(<<~PROPS) json.outer do json.inner defer: :auto do @@ -358,12 +358,12 @@ inner: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'}, + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ] }) end - it 'defers is inactive in a search' do + it "defers is inactive in a search" do json = render(<<~PROPS) json.outer(search: ['outer', 'inner', 'greeting']) do json.inner defer: :auto do @@ -378,14 +378,13 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" }, - deferred: [ - ] + deferred: [] }) end - it 'makes the defer option inactive on the found node, this differs from every other extension' do + it "makes the defer option inactive on the found node, this differs from every other extension" do json = render(<<~PROPS) json.outer(search: ['outer', 'inner']) do json.inner defer: :auto do @@ -401,14 +400,14 @@ expect(json).to eql_json({ outer: { greeting: { - foo: 'bar' + foo: "bar" } }, deferred: [] }) end - it 'is reenabled on the children of the found node' do + it "is reenabled on the children of the found node" do json = render(<<~PROPS) json.outer(search: ['outer', 'inner']) do json.inner do @@ -426,12 +425,12 @@ greeting: {} }, deferred: [ - {url: '/some_url?props_at=outer.inner.greeting', path: 'outer.inner.greeting', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.greeting", path: "outer.inner.greeting", type: "auto"} ] }) end - it 'is reenabled on the children of a found node' do + it "is reenabled on the children of a found node" do json = render(<<~PROPS) json.hit1 do json.hit2 do @@ -457,12 +456,12 @@ } }, deferred: [ - {url: '/some_url?props_at=hit1.hit2.outer.inner.greeting', path: 'hit1.hit2.outer.inner.greeting', type: 'auto'}, + {url: "/some_url?props_at=hit1.hit2.outer.inner.greeting", path: "hit1.hit2.outer.inner.greeting", type: "auto"} ] }) end - it 'is included even when part of a cached subtree' do + it "is included even when part of a cached subtree" do props = <<~PROPS json.outer do json.inner(cache: 'some_key') do @@ -485,7 +484,7 @@ } }, deferred: [ - {url: '/some_url?props_at=outer.inner.greeting', path: 'outer.inner.greeting', type: 'auto'}, + {url: "/some_url?props_at=outer.inner.greeting", path: "outer.inner.greeting", type: "auto"} ] }) end diff --git a/spec/extensions/fragments_spec.rb b/spec/extensions/fragments_spec.rb index 36506fe..8cd020b 100644 --- a/spec/extensions/fragments_spec.rb +++ b/spec/extensions/fragments_spec.rb @@ -1,8 +1,8 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template fragments' do - it 'renders with a partial and populates fragments' do +RSpec.describe "Props::Template fragments" do + it "renders with a partial and populates fragments" do json = render(<<~PROPS) json.outer do json.inner(partial: ['simple', fragment: :simple]) do @@ -17,20 +17,20 @@ expect(json).to eql_json({ outer: { inner: { - foo: 'bar' + foo: "bar" }, inner2: { - foo: 'bar' + foo: "bar" } }, fragments: [ - { type: :simple, partial: 'simple', path: 'outer.inner' }, - { type: :simple, partial: 'simple', path: 'outer.inner2' } + {type: :simple, partial: "simple", path: "outer.inner"}, + {type: :simple, partial: "simple", path: "outer.inner2"} ] }) end - it 'renders with a partial and populates fragments even when caching' do + it "renders with a partial and populates fragments even when caching" do render(<<~PROPS) json.outer do json.inner(cache: 'foobar') do @@ -55,17 +55,17 @@ outer: { inner: { simple: { - foo: 'bar' + foo: "bar" } - }, + } }, fragments: [ - { type: :simple, partial: 'simple', path: 'outer.inner.simple' } + {type: :simple, partial: "simple", path: "outer.inner.simple"} ] }) end - it 'renders an array of partials with fragments' do + it "renders an array of partials with fragments" do json = render(<<~PROPS) users = [ { name: 'joe', id: 1}, @@ -85,17 +85,17 @@ expect(json).to eql_json({ data: [ - {firstName: 'joe'}, - {firstName: 'foo'} + {firstName: "joe"}, + {firstName: "foo"} ], fragments: [ - { type: 'user_list_item', partial: 'customer', path: 'data.0' }, - { type: 'user_list_item', partial: 'customer', path: 'data.1' } + {type: "user_list_item", partial: "customer", path: "data.0"}, + {type: "user_list_item", partial: "customer", path: "data.1"} ] }) end - it 'renders an array of partials with fragments using the :key as the path' do + it "renders an array of partials with fragments using the :key as the path" do json = render(<<~PROPS) users = [ { name: 'joe', id: 1}, @@ -117,22 +117,22 @@ expect(json).to eql_json({ data: [ { - firstName: 'joe', + firstName: "joe", id: 1 }, { - firstName: 'foo', + firstName: "foo", id: 2 } ], fragments: [ - { type: 'user', partial: 'customer', path: 'data.id=1' }, - { type: 'user', partial: 'customer', path: 'data.id=2' } + {type: "user", partial: "customer", path: "data.id=1"}, + {type: "user", partial: "customer", path: "data.id=2"} ] }) end - it 'renders an array of partials with fragments using the :key as the method_name' do + it "renders an array of partials with fragments using the :key as the method_name" do json = render(<<~PROPS) klass = Struct.new(:email, :id) @@ -156,19 +156,18 @@ expect(json).to eql_json({ data: [ { - email: 'joe@red.com', + email: "joe@red.com", id: 1 }, { - email: 'foo@red.com', + email: "foo@red.com", id: 2 } ], fragments: [ - { type: 'user', partial: 'person', path: 'data.id=1' }, - { type: 'user', partial: 'person', path: 'data.id=2' } + {type: "user", partial: "person", path: "data.id=1"}, + {type: "user", partial: "person", path: "data.id=2"} ] }) end end - diff --git a/spec/extensions/key_extension_spec.rb b/spec/extensions/key_extension_spec.rb index f8563ba..6c9f373 100644 --- a/spec/extensions/key_extension_spec.rb +++ b/spec/extensions/key_extension_spec.rb @@ -1,8 +1,8 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template fragments' do - it 'renders an array of partials with fragments using the :key as the method_name' do +RSpec.describe "Props::Template fragments" do + it "renders an array of partials with fragments using the :key as the method_name" do json = render(<<~PROPS) klass = Struct.new(:email, :id) @@ -21,14 +21,14 @@ expect(json).to eql_json({ data: [ { - email: 'joe@red.com', + email: "joe@red.com", id: 1 }, { - email: 'foo@red.com', + email: "foo@red.com", id: 2 } - ], + ] }) end end diff --git a/spec/extensions/partial_defer_extension_spec.rb b/spec/extensions/partial_defer_extension_spec.rb index ac5f2f4..600092c 100644 --- a/spec/extensions/partial_defer_extension_spec.rb +++ b/spec/extensions/partial_defer_extension_spec.rb @@ -1,13 +1,13 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template fragments' do +RSpec.describe "Props::Template fragments" do before do Rails.cache.clear - @controller.request.path = '/some_url' + @controller.request.path = "/some_url" end - it 'defers work together with partials' do + it "defers work together with partials" do json = render(<<~PROPS) json.outer do json.inner(partial: ['simple', fragment: :simple], defer: :auto) do @@ -23,16 +23,16 @@ inner: {} }, defers: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ], fragments: [ - {type: :simple, partial: 'simple', path: 'outer.inner'} + {type: :simple, partial: "simple", path: "outer.inner"} ] }) end - it 'overrides existing props_at paramenters' do - @controller.request.path = '/some_url?props_at=outer' + it "overrides existing props_at paramenters" do + @controller.request.path = "/some_url?props_at=outer" json = render(<<~PROPS) json.outer do @@ -49,10 +49,10 @@ inner: {} }, defers: [ - {url: '/some_url?props_at=outer.inner', path: 'outer.inner', type: 'auto'} + {url: "/some_url?props_at=outer.inner", path: "outer.inner", type: "auto"} ], fragments: [ - {type: :simple, partial: 'simple', path: 'outer.inner'} + {type: :simple, partial: "simple", path: "outer.inner"} ] }) end diff --git a/spec/extensions/partial_render_spec.rb b/spec/extensions/partial_render_spec.rb index 4c8aae1..1ed42f5 100644 --- a/spec/extensions/partial_render_spec.rb +++ b/spec/extensions/partial_render_spec.rb @@ -1,14 +1,14 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template' do - it 'renders on actionview' do +RSpec.describe "Props::Template" do + it "renders on actionview" do json = render(<<~PROPS) json.hello 'world' PROPS expect(json).to eql_json({ - hello: 'world' + hello: "world" }) end @@ -32,7 +32,7 @@ # ) # end - it 'renders with a partial' do + it "renders with a partial" do json = render(<<~PROPS) json.outer(partial: 'simple') do end @@ -40,12 +40,12 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders with a partial starting with a capital' do + it "renders with a partial starting with a capital" do json = render(<<~PROPS) json.outer(partial: 'FooBar') do end @@ -53,12 +53,12 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders with a partial with a hyphen' do + it "renders with a partial with a hyphen" do json = render(<<~PROPS) json.outer(partial: 'a-in') do end @@ -66,12 +66,12 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders with a partial with unicode_text' do + it "renders with a partial with unicode_text" do json = render(<<~PROPS) json.outer(partial: "🍣") do end @@ -84,27 +84,27 @@ }) end - it 'errors out on missing partials' do + it "errors out on missing partials" do expect { - json = render(<<~PROPS) + render(<<~PROPS) json.outer(partial: 'missing') do end PROPS }.to raise_error.with_message(/Missing partial \/_missing/) end - it 'errors out on bad syntax' do + it "errors out on bad syntax" do expect { render(<<~PROPS) json.outer(partial: 'syntax_error') do end PROPS - }.to raise_error {|e| + }.to raise_error { |e| expect(e.annotated_source_code[0]).to eql(" 1: does_nothing()") } end - it 'renders with formats: [json], always' do + it "renders with formats: [json], always" do json = render(<<~PROPS) json.outer(partial: ['simple', formats: [:foobar]]) do end @@ -112,12 +112,12 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders with locale' do + it "renders with locale" do json = render(<<~PROPS) json.outer(partial: ['simple', locale: :de]) do end @@ -125,12 +125,12 @@ expect(json).to eql_json({ outer: { - foo: 'Kein' + foo: "Kein" } }) end - it 'renders with variants' do + it "renders with variants" do json = render(<<~PROPS) json.outer(partial: ['simple', variants: :grid]) do end @@ -138,12 +138,12 @@ expect(json).to eql_json({ outer: { - foo: 'Grid' + foo: "Grid" } }) end - it 'renders partials from the top level' do + it "renders partials from the top level" do json = render(<<~PROPS) json.outer(partial: '/simple') do end @@ -151,12 +151,12 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders numeric partials' do + it "renders numeric partials" do json = render(<<~PROPS) json.outer(partial: '200') do end @@ -164,12 +164,12 @@ expect(json).to eql_json({ outer: { - success: 'ok' + success: "ok" } }) end - it 'renders, ignoring handlers' do + it "renders, ignoring handlers" do json = render(<<~PROPS) json.outer(partial: ['simple', handlers: :foobar]) do end @@ -177,24 +177,23 @@ expect(json).to eql_json({ outer: { - foo: 'bar' + foo: "bar" } }) end - it 'renders with an empty partial' do + it "renders with an empty partial" do json = render(<<~PROPS) json.outer(partial: 'noop') do end PROPS expect(json).to eql_json({ - outer: { - } + outer: {} }) end - it 'renders with a partial with locals' do + it "renders with a partial with locals" do json = render(<<~PROPS) opts = { partial: ['profile', locals: {email: 'joe@joe.com'}] @@ -205,12 +204,12 @@ expect(json).to eql_json({ outer: { - email: 'joe@joe.com' + email: "joe@joe.com" } }) end - it 'renders an array of partials' do + it "renders an array of partials" do json = render(<<~PROPS) emails = [ 'joe@j.com', @@ -225,12 +224,12 @@ PROPS expect(json).to eql_json([ - {email: 'joe@j.com'}, - {email: 'foo@f.com'} + {email: "joe@j.com"}, + {email: "foo@f.com"} ]) end - it 'renders an array of partials with a default local named after the file' do + it "renders an array of partials with a default local named after the file" do json = render(<<~PROPS) emails = [ {name: 'joe'}, @@ -246,24 +245,24 @@ PROPS expect(json).to eql_json([ - {firstName: 'joe'}, - {firstName: 'foo'} + {firstName: "joe"}, + {firstName: "foo"} ]) end - it 'renders without a :as option when not rendering collections' do + it "renders without a :as option when not rendering collections" do expect { render(<<~PROPS) json.outer(partial: 'customer') do end PROPS - }.to raise_error {|e| - expect(e.message).to match(/undefined local variable or method \`customer\'/) + }.to raise_error { |e| + expect(e.message).to match(/undefined local variable or method `customer'/) } end - it 'errors out on an invalid use of as' do - json = expect{ + it "errors out on an invalid use of as" do + expect { render(<<~PROPS) emails = [ 'joe@j.com', @@ -277,13 +276,13 @@ end PROPS }.to raise_error.with_message( - "The value (foo-bar) of the option `as` is not a valid Ruby identifier; " \ - "make sure it starts with lowercase letter, " \ - "and is followed by any combination of letters, numbers and underscores." + "The value (foo-bar) of the option `as` is not a valid Ruby identifier; " \ + "make sure it starts with lowercase letter, " \ + "and is followed by any combination of letters, numbers and underscores." ) end - it 'renders an array without :as' do + it "renders an array without :as" do json = render(<<~PROPS) emails = [ 'joe@j.com', @@ -295,12 +294,12 @@ PROPS expect(json).to eql_json([ - {foo: 'bar'}, - {foo: 'bar'} + {foo: "bar"}, + {foo: "bar"} ]) end - it 'renders an an empty array' do + it "renders an an empty array" do json = render(<<~PROPS) json.array! [], {partial: 'simple'} do end diff --git a/spec/extensions/prop_template_search_spec.rb b/spec/extensions/prop_template_search_spec.rb index f71afb2..13fec2c 100644 --- a/spec/extensions/prop_template_search_spec.rb +++ b/spec/extensions/prop_template_search_spec.rb @@ -1,12 +1,12 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe('searching the template') do +RSpec.describe("searching the template") do before do - @controller.request.path = '/some_url' + @controller.request.path = "/some_url" end - it 'finds the correct node and merges it to the top' do + it "finds the correct node and merges it to the top" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 'details']) do json.comment do @@ -20,13 +20,13 @@ expect(json).to eql_json({ data: { - name: 'john' + name: "john" }, - foo: 'bar' + foo: "bar" }) end - it 'finds an empty child node and returns an empty object' do + it "finds an empty child node and returns an empty object" do json = render(<<~PROPS) json.data(search: ['data', 'inner']) do json.inner do @@ -36,13 +36,12 @@ PROPS expect(json).to eql_json({ - data: { - }, - foo: 'bar' + data: {}, + foo: "bar" }) end - it 'searching for a non-existant child does not set the parent, simulating undefined in JS' do + it "searching for a non-existant child does not set the parent, simulating undefined in JS" do json = render(<<~PROPS) json.data(search: ['data', 'does_not_exist']) do json.inner do @@ -52,11 +51,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'searching for nil does nothing' do + it "searching for nil does nothing" do json = render(<<~PROPS) json.data(search: nil) do json.inner do @@ -69,11 +68,11 @@ data: { inner: {} }, - foo: 'bar' + foo: "bar" }) end - it 'searching for an empty array means we found nothing' do + it "searching for an empty array means we found nothing" do json = render(<<~PROPS) json.data(search: []) do json.inner do @@ -83,11 +82,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'searching for a child node with siblings in back' do + it "searching for a child node with siblings in back" do json = render(<<~PROPS) json.outer(search: ['outer', 'inner']) do json.inner do @@ -108,8 +107,7 @@ }) end - - it 'searching for a child node with siblings in front' do + it "searching for a child node with siblings in front" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner']) do json.bad do @@ -130,7 +128,7 @@ }) end - it 'searches on multiple siblings' do + it "searches on multiple siblings" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner']) do json.inner do @@ -150,12 +148,12 @@ foo: 32 }, first: { - bar: 'cool' + bar: "cool" } }) end - it 'reenables search functionality at the contents of found obj nodes' do + it "reenables search functionality at the contents of found obj nodes" do json = render(<<-PROPS) json.outer(search: ['outer']) do json.inner(search: ['inner', 'foo']) do @@ -169,13 +167,13 @@ expect(json).to eql_json({ outer: { inner: { - firstName: 'john' + firstName: "john" } } }) end - it 'ignores search functionality in between levels of traversal' do + it "ignores search functionality in between levels of traversal" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner', 'foo']) do json.inner(search: ['does_not_exist']) do @@ -188,12 +186,12 @@ expect(json).to eql_json({ outer: { - firstName: 'john' + firstName: "john" } }) end - it 'find the correct node in a partial' do + it "find the correct node in a partial" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 'details']) do json.comment(partial: 'comment') do @@ -203,12 +201,12 @@ expect(json).to eql_json({ data: { - body: 'hello world' + body: "hello world" } }) end - it 'finds a subtree' do + it "finds a subtree" do json = render(<<-PROPS) json.outer(search: ['outer','inner', 'deep']) do json.inner do @@ -230,7 +228,7 @@ }) end - it 'searching for a leaf node is unsupported' do + it "searching for a leaf node is unsupported" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner', 'foo']) do json.inner do @@ -241,12 +239,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - - it 'searching for a node beyond whats available is equivalent to not finding anything' do + it "searching for a node beyond whats available is equivalent to not finding anything" do json = render(<<-PROPS) json.outer(search: ['inner', 'a', 'b']) do json.inner do @@ -257,11 +254,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'finds an array' do + it "finds an array" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner']) do json.inner do @@ -280,7 +277,7 @@ }) end - it 'searching for an item inside an array' do + it "searching for an item inside an array" do json = render(<<-PROPS) json.outer(search: ['outer', 0]) do json.array! ['hello', 'world'] do |item| @@ -291,12 +288,12 @@ expect(json).to eql_json({ outer: { - foo: 'hello' - }, + foo: "hello" + } }) end - it 'searching for an node beyond an array' do + it "searching for an node beyond an array" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner', 1, 'foo']) do json.inner do @@ -316,7 +313,7 @@ }) end - it 'searching for an node outside the length of the array, is equivalent to not finding anything' do + it "searching for an node outside the length of the array, is equivalent to not finding anything" do json = render(<<-PROPS) json.outer(search: ['inner', 10, 'foo']) do json.inner do @@ -331,11 +328,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'searching for an node outside the length of the array, is equivalent to not finding anything' do + it "searching for an node outside the length of the array, is equivalent to not finding anything" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner', 10, 'foo']) do json.inner do @@ -350,11 +347,11 @@ PROPS expect(json).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'searching for nested array' do + it "searching for nested array" do json = render(<<-PROPS) json.outer(search: ['outer', 'inner', 1, 'foo']) do json.inner do @@ -372,11 +369,12 @@ expect(json).to eql_json({ outer: [ {bar: 1}, - {bar: 5}, - ]}) + {bar: 5} + ] + }) end - it 'searching for object inside a nested array' do + it "searching for object inside a nested array" do json = render(<<-PROPS) json.outer(search: ['outer','inner', 1, 'foo', 0]) do json.inner do @@ -396,8 +394,8 @@ }) end - context 'when searching through a partial' do - it 'returns the correct node in an object' do + context "when searching through a partial" do + it "returns the correct node in an object" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 'details']) do json.comment(partial: 'comment') do @@ -407,12 +405,12 @@ expect(json).to eql_json({ data: { - body: 'hello world' + body: "hello world" } }) end - it 'ignores the fragment option' do + it "ignores the fragment option" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 'details']) do json.comment(partial: ['comment', fragment: 'foobar']) do @@ -423,13 +421,13 @@ expect(json).to eql_json({ data: { - body: 'hello world' + body: "hello world" }, fragments: [] }) end - it 'passes the found child obj options back to the parent' do + it "passes the found child obj options back to the parent" do json = render(<<~PROPS) json.data(search: ['data', 'comment']) do json.comment(partial: 'comment') do @@ -439,15 +437,15 @@ expect(json).to eql_json({ data: { - title: 'some title', + title: "some title", details: { - body: 'hello world' + body: "hello world" } } }) end - it 'passes the found child obj in array options back to the parent' do + it "passes the found child obj in array options back to the parent" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 1]) do json.comment do @@ -459,12 +457,12 @@ expect(json).to eql_json({ data: { - email: 'world', + email: "world" } }) end - it 'returns the correct node in an array' do + it "returns the correct node in an array" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 0]) do json.comment do @@ -476,12 +474,12 @@ expect(json).to eql_json({ data: { - foo: 'bar' + foo: "bar" } }) end - it 'returns the correct node beyond an array' do + it "returns the correct node beyond an array" do json = render(<<~PROPS) json.data(search: ['data','comment', 0, 'details']) do json.comment do @@ -493,12 +491,12 @@ expect(json).to eql_json({ data: { - body: 'hello world' + body: "hello world" } }) end - it 'returns the correct node across nested partials' do + it "returns the correct node across nested partials" do json = render(<<~PROPS) json.data(search: ['data', 'comment', 'details', 'contact', 'phone']) do json.comment(partial: 'complex') do @@ -508,13 +506,13 @@ expect(json).to eql_json({ data: { - home: '111', - cell: '222' + home: "111", + cell: "222" } }) end - it 'returns the correct node across nested partials' do + it "returns the correct node across nested partials" do json = render(<<~PROPS) json.data(search: ['data', 'comments', 0, 0, 'details', 'contact', 'phone']) do opts = { @@ -528,8 +526,8 @@ expect(json).to eql_json({ data: { - home: '111', - cell: '222' + home: "111", + cell: "222" } }) end diff --git a/spec/extensions/rails_cache_spec.rb b/spec/extensions/rails_cache_spec.rb index 3a3f518..c0189ef 100644 --- a/spec/extensions/rails_cache_spec.rb +++ b/spec/extensions/rails_cache_spec.rb @@ -1,16 +1,16 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' -require 'active_support/testing/time_helpers' +require_relative "../support/helper" +require_relative "../support/rails_helper" +require "active_support/testing/time_helpers" require "active_support/core_ext/string" -RSpec.describe 'Props::Template caching' do +RSpec.describe "Props::Template caching" do include ActiveSupport::Testing::TimeHelpers before do Rails.cache.clear end - it 'caches an object' do + it "caches an object" do json = render(<<~PROPS) json.author(cache: 'some_cache_key') do json.first_name 'hit' @@ -23,18 +23,18 @@ expect(json).to eql_json({ author: { - firstName: 'hit' + firstName: "hit" }, author2: { - firstName: 'hit' + firstName: "hit" } }) end - it 'caches an object with expiry' do + it "caches an object with expiry" do freeze_time - json = render(<<~PROPS) + render(<<~PROPS) opts = { cache: ['some_cache_key', expires_in: 1.minute] } @@ -58,8 +58,8 @@ expect(json).to eql_json({ author: { - firstName: 'hit' - }, + firstName: "hit" + } }) travel 31.seconds @@ -76,12 +76,12 @@ expect(json).to eql_json({ author: { - firstName: 'miss' - }, + firstName: "miss" + } }) end - it 'caches object in an array' do + it "caches object in an array" do json = render(<<~PROPS) json.authors do opts = { @@ -98,15 +98,15 @@ {id: 1}, {id: 2}, {id: 1}, - {id: 2}, + {id: 2} ] }) end - it 'caches object in an array with an expiry' do + it "caches object in an array with an expiry" do freeze_time - json = render(<<~PROPS) + render(<<~PROPS) json.authors do opts = { cache: [->(i) {['some_cache', i]}, expires_in: 1.minute] @@ -132,7 +132,7 @@ expect(json).to eql_json({ authors: [ - {id: 1}, + {id: 1} ] }) @@ -151,12 +151,12 @@ expect(json).to eql_json({ authors: [ - {id: 'miss'}, + {id: "miss"} ] }) end - it 'caches a partial' do + it "caches a partial" do json = render(<<~PROPS) opts = { partial: 'simple', @@ -173,15 +173,15 @@ expect(json).to eql_json({ author: { - foo: 'bar' + foo: "bar" }, author2: { - foo: 'bar' + foo: "bar" } }) end - it 'caches partials in an array' do + it "caches partials in an array" do json = render(<<~PROPS) json.authors do opts = { @@ -199,12 +199,12 @@ {email: 1}, {email: 2}, {email: 1}, - {email: 2}, + {email: 2} ] }) end - it 'has no effect when search is active' do + it "has no effect when search is active" do json = render(<<~PROPS) json.foo(cache:'some_key') do json.first_name 'dave' @@ -223,21 +223,20 @@ end PROPS - expect(json).to eql_json({ foo: { - firstName: 'dave' + firstName: "dave" }, bar: { - firstName: 'dave' + firstName: "dave" }, author: { - firstName: 'john' + firstName: "john" } }) end - it 'is reenabled at the found subtree' do + it "is reenabled at the found subtree" do json = render(<<~PROPS) json.foo(cache:'some_key') do json.first_name 'dave' @@ -254,17 +253,16 @@ end PROPS - expect(json).to eql_json({ foo: { - firstName: 'dave' + firstName: "dave" }, bar: { - firstName: 'dave' + firstName: "dave" }, author: { details: { - firstName: 'dave' + firstName: "dave" } } }) diff --git a/spec/extensions/traveled_path_spec.rb b/spec/extensions/traveled_path_spec.rb index f7f1933..b2771fd 100644 --- a/spec/extensions/traveled_path_spec.rb +++ b/spec/extensions/traveled_path_spec.rb @@ -1,12 +1,12 @@ -require_relative '../support/helper' -require_relative '../support/rails_helper' +require_relative "../support/helper" +require_relative "../support/rails_helper" -RSpec.describe 'Props::Template' do +RSpec.describe "Props::Template" do before do - @controller.request.path = '/some_url' + @controller.request.path = "/some_url" end - it 'returns the path of the node its called from' do + it "returns the path of the node its called from" do json = render(<<~PROPS) json.data do json.comment do @@ -21,10 +21,10 @@ data: { comment: { fullDetails: { - foo: 'data.comment.full_details' + foo: "data.comment.full_details" } } - }, + } }) end end diff --git a/spec/layout_spec.rb b/spec/layout_spec.rb index 4047065..b18b1bc 100644 --- a/spec/layout_spec.rb +++ b/spec/layout_spec.rb @@ -3,15 +3,15 @@ require "props_template/layout_patch" require "action_controller" -RSpec.describe "Props::Template" do - class TestController < ActionController::Base - protect_from_forgery +class TestController < ActionController::Base + protect_from_forgery - def self.controller_path - "" - end + def self.controller_path + "" end +end +RSpec.describe "Props::Template" do it "uses a layout to render" do view_path = File.join(File.dirname(__FILE__), "./fixtures") controller = TestController.new diff --git a/spec/props_template_spec.rb b/spec/props_template_spec.rb index 3477ca2..e121e47 100644 --- a/spec/props_template_spec.rb +++ b/spec/props_template_spec.rb @@ -1,49 +1,49 @@ -require_relative './support/helper' +require_relative "./support/helper" -RSpec.describe 'Props::Base' do - it 'initializes' do +RSpec.describe "Props::Base" do + it "initializes" do expect { Props::Base.new }.to_not raise_error end - context 'result!' do - it 'returns {} when empty' do + context "result!" do + it "returns {} when empty" do json = Props::Base.new - expect(json.result!.strip).to eql('{}') + expect(json.result!.strip).to eql("{}") end - it 'resets OJ' do + it "resets OJ" do json = Props::Base.new - json.set! :foo, 'bar' + json.set! :foo, "bar" attrs = json.result!.strip expect(attrs).to eql_json({ - foo: 'bar' + foo: "bar" }) expect(json.result!.strip).to eql_json({}) - json.set! :foo, 'bar' + json.set! :foo, "bar" attrs = json.result!.strip expect(attrs).to eql_json({ - foo: 'bar' + foo: "bar" }) end end - context 'set!' do - it 'sets a value' do + context "set!" do + it "sets a value" do json = Props::Base.new - json.set! :foo, 'bar' + json.set! :foo, "bar" attrs = json.result!.strip expect(attrs).to eql_json({ - foo: 'bar' + foo: "bar" }) end - it 'sets a empty obj when block is empty' do + it "sets a empty obj when block is empty" do json = Props::Base.new json.set! :foo do end @@ -54,7 +54,7 @@ }) end - it 'sets a empty obj when nested block is empty' do + it "sets a empty obj when nested block is empty" do json = Props::Base.new json.set! :foo do json.set! :bar do @@ -69,7 +69,7 @@ }) end - it 'sets a null value' do + it "sets a null value" do json = Props::Base.new json.set! :foo, nil attrs = json.result!.strip @@ -79,74 +79,74 @@ }) end - it 'sets multiple values' do + it "sets multiple values" do json = Props::Base.new - json.set! :foo, 'bar' - json.set! :steve, 'cool' + json.set! :foo, "bar" + json.set! :steve, "cool" attrs = json.result!.strip expect(attrs).to eql_json({ - foo: 'bar', - steve: 'cool' + foo: "bar", + steve: "cool" }) end - it 'sets multiple values with the same key, the last one wins' do + it "sets multiple values with the same key, the last one wins" do json = Props::Base.new - json.set! :foo, 'bar' - json.set! :foo, 'cool' + json.set! :foo, "bar" + json.set! :foo, "cool" attrs = JSON.parse(json.result!) expect(attrs).to eql({ - 'foo' => 'cool' + "foo" => "cool" }) end - it 'throws InvalidScopeForObjError when the current scope is an array' do + it "throws InvalidScopeForObjError when the current scope is an array" do json = Props::Base.new json.array! [1, 2] do |item| json.set! :foo, item end expect { - json.set! :bar, 'world' + json.set! :bar, "world" }.to raise_error(Props::InvalidScopeForObjError) end end - context 'set! with a block' do - it 'creates a new nested object' do + context "set! with a block" do + it "creates a new nested object" do json = Props::Base.new json.set! :outer do - json.set! :inner, 'baz' + json.set! :inner, "baz" end attrs = json.result!.strip expect(attrs).to eql_json({ outer: { - inner: 'baz' + inner: "baz" } }) end - it 'creates a nested object' do + it "creates a nested object" do json = Props::Base.new json.set! :outer do - json.set! :inner, 'baz' + json.set! :inner, "baz" end attrs = json.result!.strip expect(attrs).to eql_json({ outer: { - inner: 'baz' + inner: "baz" } }) end - it 'creates a nested array of objects' do + it "creates a nested array of objects" do json = Props::Base.new json.set! :outer do json.array! [1, 2] do |item| @@ -164,24 +164,24 @@ }) end - it 'treats the second argument as an options arg' do + it "treats the second argument as an options arg" do json = Props::Base.new - json.set! :outer, {some: 'setting'} do - json.set! :inner, 'baz' + json.set! :outer, {some: "setting"} do + json.set! :inner, "baz" end attrs = json.result!.strip expect(attrs).to eql_json({ outer: { - inner: 'baz' + inner: "baz" } }) end end - context 'array!' do - it 'creates an array of 1 object' do + context "array!" do + it "creates an array of 1 object" do json = Props::Base.new json.array! [1] do |num| json.set! :foo, num @@ -194,32 +194,31 @@ ]) end - it 'passes the index as the second argument of yield' do + it "passes the index as the second argument of yield" do json = Props::Base.new - json.array! ['a', 'b'] do |item, index| + json.array! ["a", "b"] do |item, index| json.set! :foo, [item, index] end attrs = json.result!.strip expect(attrs).to eql_json([ - {foo: ['a', 0]}, - {foo: ['b', 1]} + {foo: ["a", 0]}, + {foo: ["b", 1]} ]) end - it 'creates an empty array when passed an empty collection' do + it "creates an empty array when passed an empty collection" do json = Props::Base.new json.array! [] do |num| end attrs = json.result!.strip - expect(attrs).to eql_json([ - ]) + expect(attrs).to eql_json([]) end - it 'creates an array of empty arrays when passed an empty collection' do + it "creates an array of empty arrays when passed an empty collection" do json = Props::Base.new json.array! [1] do |num| json.array! [] do @@ -230,7 +229,7 @@ expect(attrs).to eql_json([[]]) end - it 'creates an array of empties if set did not get called in array' do + it "creates an array of empties if set did not get called in array" do json = Props::Base.new json.array! [1, 2, 3] do |num| end @@ -244,20 +243,20 @@ ]) end - it 'throws InvalidScopeForArray error when array is used twice' do + it "throws InvalidScopeForArray error when array is used twice" do json = Props::Base.new json.array! [1] do - json.set! :foo, 'first' + json.set! :foo, "first" end expect { json.array! [2] do |num| - json.set! :foo, 'second' + json.set! :foo, "second" end }.to raise_error(Props::InvalidScopeForArrayError) end - it 'creates an array of multiple objects' do + it "creates an array of multiple objects" do json = Props::Base.new json.array! [1, 2] do |num| json.set! :foo, num @@ -271,7 +270,7 @@ ]) end - it 'creates an array of arrays' do + it "creates an array of arrays" do json = Props::Base.new json.array! [[1, 2], [3, 4]] do |part| json.array! part do |num| @@ -287,12 +286,12 @@ ]) end - it 'throws InvalidScopeForArrayError when the current scope is already an object' do + it "throws InvalidScopeForArrayError when the current scope is already an object" do json = Props::Base.new - json.set! :foo, 'bar' + json.set! :foo, "bar" expect { json.array! [1, 2] do |num| - json.set! :foo, 'noop' + json.set! :foo, "noop" end }.to raise_error(Props::InvalidScopeForArrayError) end diff --git a/spec/searcher_spec.rb b/spec/searcher_spec.rb index a6d4c27..7acc579 100644 --- a/spec/searcher_spec.rb +++ b/spec/searcher_spec.rb @@ -1,13 +1,26 @@ -require_relative './support/helper' +require_relative "./support/helper" -RSpec.describe 'Searcher' do - it 'searching for a child node returns the proc, and refined found options' do - json = Props::Searcher.new(nil, ['outer', 'inner', 'hit']) - target_proc = Proc.new {} +class Collection + def initialize(ary, rspec) + @ary = ary + @rspec = rspec + end + + def member_by(key, value) + @rspec.expect(key).to @rspec.eql("id") + @rspec.expect(value).to @rspec.eql(1) + @ary.last + end +end + +RSpec.describe "Searcher" do + it "searching for a child node returns the proc, and refined found options" do + json = Props::Searcher.new(nil, ["outer", "inner", "hit"]) + target_proc = proc {} target_opts = {some_options: 1} - json.set!('outer') do - json.set!('inner') do - json.set!('hit', target_opts, &target_proc) + json.set!("outer") do + json.set!("inner") do + json.set!("hit", target_opts, &target_proc) end end @@ -15,186 +28,173 @@ expect(found_block).to eql(target_proc) expect(found_options).to eql({ some_options: 1, - path_suffix: ['inner', 'hit'] + path_suffix: ["inner", "hit"] }) end - it 'searching with an empty path means you found nothing' do + it "searching with an empty path means you found nothing" do json = Props::Searcher.new(nil, []) - target_proc = Proc.new do - json.set!('inner') do - json.set!('hit', {}, &target_proc) + target_proc = proc do + json.set!("inner") do + json.set!("hit", {}, &target_proc) end end - json.set!('outer', {}, &target_proc) + json.set!("outer", {}, &target_proc) - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to be_nil end - it 'searching for a child node with siblings in back' do - json = Props::Searcher.new(nil, ['outer', 'inner']) - target_proc = Proc.new do - json.set!('foo', 32) + it "searching for a child node with siblings in back" do + json = Props::Searcher.new(nil, ["outer", "inner"]) + target_proc = proc do + json.set!("foo", 32) end - json.set!('outer') do - json.set!('inner', {}, &target_proc) + json.set!("outer") do + json.set!("inner", {}, &target_proc) - json.set!('bad') do - json.set!('foo', 'should not touch') + json.set!("bad") do + json.set!("foo", "should not touch") end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to eql(target_proc) end - it 'searching for a child node with siblings in front' do - json = Props::Searcher.new(nil, ['outer', 'inner']) - target_proc = Proc.new do + it "searching for a child node with siblings in front" do + json = Props::Searcher.new(nil, ["outer", "inner"]) + target_proc = proc do json.foo 32 end - json.set!('outer') do - json.set!('bad') do - json.set!('foo', 'should not touch') + json.set!("outer") do + json.set!("bad") do + json.set!("foo", "should not touch") end - json.set!('inner', {}, &target_proc) + json.set!("inner", {}, &target_proc) end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to eql(target_proc) end - it 'searching for a subtree' do - json = Props::Searcher.new(nil, ['outer', 'inner', 'deep']) + it "searching for a subtree" do + json = Props::Searcher.new(nil, ["outer", "inner", "deep"]) - target_proc = Proc.new do - json.set!('deeper') do - json.set!('foo', 32) + target_proc = proc do + json.set!("deeper") do + json.set!("foo", 32) end end - json.set!('outer') do - json.set!('inner') do - json.set!('deep', {}, &target_proc) + json.set!("outer") do + json.set!("inner") do + json.set!("deep", {}, &target_proc) end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to eql(target_proc) end - it 'searching for a leaf node is unsupported' do - json = Props::Searcher.new(nil, ['outer', 'inner', 'foo']) - json.set!('outer') do - json.set!('inner') do - json.set!('foo', 32) + it "searching for a leaf node is unsupported" do + json = Props::Searcher.new(nil, ["outer", "inner", "foo"]) + json.set!("outer") do + json.set!("inner") do + json.set!("foo", 32) end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to be_nil end - it 'searching for a node beyond whats available is equivalent to not finding anything' do - json = Props::Searcher.new(nil, ['outer', 'inner', 'a', 'b']) - json.set!('outer') do - json.set!('inner') do - json.set!('foo', 32) + it "searching for a node beyond whats available is equivalent to not finding anything" do + json = Props::Searcher.new(nil, ["outer", "inner", "a", "b"]) + json.set!("outer") do + json.set!("inner") do + json.set!("foo", 32) end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to be_nil end - it 'searching for an item inside an array, includes refined found options' do - json = Props::Searcher.new(nil, ['outer', 1]) + it "searching for an item inside an array, includes refined found options" do + json = Props::Searcher.new(nil, ["outer", 1]) target_opts = {some_options: 1} - json.set!('outer') do - json.array!(['hello', 'world'], target_opts) do |item| + json.set!("outer") do + json.array!(["hello", "world"], target_opts) do |item| item end end found_block, found_options = json.found! expect(found_options).to eql({some_options: 1, path_suffix: [1]}) - expect(found_block.call).to eql('world') + expect(found_block.call).to eql("world") end - it 'searching for an item inside an array using an id=val keypath' do - json = Props::Searcher.new(nil, ['outer', 'id=1']) - - class Collection - def initialize(ary, rspec) - @ary = ary - @rspec = rspec - end - - def member_by(key, value) - @rspec.expect(key).to @rspec.eql('id') - @rspec.expect(value).to @rspec.eql(1) - @ary.last - end - end + it "searching for an item inside an array using an id=val keypath" do + json = Props::Searcher.new(nil, ["outer", "id=1"]) collection = Collection.new( [{id: 0}, {id: 1}], self ) - json.set!('outer') do + json.set!("outer") do json.array!(collection) do |item| item end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block.call).to eql({id: 1}) end - it 'searching for an node belonging an array' do - json = Props::Searcher.new(nil, ['outer', 'inner', 1, 'foo']) - json.set!('outer') do - json.set!('inner') do - json.array! ['hello', 'world'] do |item| - json.set!('foo') do + it "searching for an node belonging an array" do + json = Props::Searcher.new(nil, ["outer", "inner", 1, "foo"]) + json.set!("outer") do + json.set!("inner") do + json.array! ["hello", "world"] do |item| + json.set!("foo") do item end end end end - found_block, found_options = json.found! - expect(found_block.call).to eql('world') + found_block, _found_options = json.found! + expect(found_block.call).to eql("world") end - it 'searching for an node outside the length of the array, is equivalent to not finding anything' do - json = Props::Searcher.new(nil, ['outer','inner', 10, 'foo']) - json.set!('outer') do - json.set!('inner') do + it "searching for an node outside the length of the array, is equivalent to not finding anything" do + json = Props::Searcher.new(nil, ["outer", "inner", 10, "foo"]) + json.set!("outer") do + json.set!("inner") do json.array! [1, 2] do |item| - json.set!('foo') do - json.set!('bar', item) + json.set!("foo") do + json.set!("bar", item) end end end end - found_block, found_options = json.found! + found_block, _found_options = json.found! expect(found_block).to be_nil end - it 'searching for object inside a nested array' do - json = Props::Searcher.new(nil, ['outer', 'inner', 1, 'foo', 0]) - json.set!('outer') do - json.set!('inner') do + it "searching for object inside a nested array" do + json = Props::Searcher.new(nil, ["outer", "inner", 1, "foo", 0]) + json.set!("outer") do + json.set!("inner") do json.array! [0, 1] do |item| - json.set!('foo') do - json.array! ['hello', 'world'] do |inner_item| + json.set!("foo") do + json.array! ["hello", "world"] do |inner_item| inner_item end end @@ -202,8 +202,7 @@ def member_by(key, value) end end - found_block, found_options = json.found! - expect(found_block.call).to eql('hello') + found_block, _found_options = json.found! + expect(found_block.call).to eql("hello") end end - diff --git a/spec/support/helper.rb b/spec/support/helper.rb index 2c33a60..5d38e26 100644 --- a/spec/support/helper.rb +++ b/spec/support/helper.rb @@ -1,7 +1,7 @@ -require_relative '../../lib/props_template' -require_relative '../../lib/props_template/core_ext' +require_relative "../../lib/props_template" +require_relative "../../lib/props_template/core_ext" -require 'json' +require "json" def eql_json(obj) eql(obj.to_json) diff --git a/spec/support/rails_helper.rb b/spec/support/rails_helper.rb index 1353f74..425b09b 100644 --- a/spec/support/rails_helper.rb +++ b/spec/support/rails_helper.rb @@ -4,9 +4,9 @@ require "active_support/cache/memory_store" require "active_support/json" -require 'action_view' -require 'action_view/testing/resolvers' -require 'action_view/template/resolver' +require "action_view" +require "action_view/testing/resolvers" +require "action_view/template/resolver" class FakeView < ActionView::Base # include Rails.application.routes.url_helpers @@ -25,15 +25,20 @@ def compiled_method_container cattr_accessor :request_forgery self.request_forgery = false - def view_cache_dependencies; []; end - def protect_against_forgery?; false; end + def view_cache_dependencies + [] + end + + def protect_against_forgery? + false + end def form_authenticity_token "secret" end def combined_fragment_cache_key(key) - [ :views, key ] + [:views, key] end def asset_pack_path(asset) @@ -47,11 +52,11 @@ def cache end end -def render(source, options={}) +def render(source, options = {}) @controller.cache_store = Rails.cache - view_path = File.join(File.dirname(__FILE__),'../fixtures') + view_path = File.join(File.dirname(__FILE__), "../fixtures") file_resolver = ActionView::FileSystemResolver.new(view_path) - lookup_context = ActionView::LookupContext.new([ file_resolver ], {}, [""]) + lookup_context = ActionView::LookupContext.new([file_resolver], {}, [""]) lookup_context.formats = [:json] view = FakeView.new(lookup_context, {}, @controller) view.assign(options.fetch(:assigns, {})) @@ -66,4 +71,3 @@ def render(source, options={}) @controller = ActionView::TestCase::TestController.new end end -