Skip to content

Commit

Permalink
Add Ruby Standard
Browse files Browse the repository at this point in the history
  • Loading branch information
jho406 committed Jun 28, 2023
1 parent a2a35af commit 49b4ca3
Show file tree
Hide file tree
Showing 32 changed files with 557 additions and 622 deletions.
3 changes: 3 additions & 0 deletions .circleci/config.yml
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions .standard.yml
@@ -0,0 +1,2 @@
ignore:
- 'performance/**/*'
23 changes: 12 additions & 11 deletions 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]
42 changes: 21 additions & 21 deletions 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
34 changes: 17 additions & 17 deletions lib/props_template.rb
@@ -1,25 +1,25 @@
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
class << self
attr_accessor :template_lookup_options
end

self.template_lookup_options = { handlers: [:props] }
self.template_lookup_options = {handlers: [:props]}

delegate :result!, :array!,
:deferred!,
Expand All @@ -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)
Expand All @@ -60,4 +60,4 @@ def builder!
end
end

require 'props_template/railtie' if defined?(Rails)
require "props_template/railtie" if defined?(Rails)
17 changes: 8 additions & 9 deletions 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
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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|
Expand All @@ -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
Expand Down
15 changes: 7 additions & 8 deletions lib/props_template/base_with_extensions.rb
Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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

Expand All @@ -60,15 +60,15 @@ 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

super

@traveled_path.pop(n)
return
nil
end

def handle_collection_item(collection, item, index, options)
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -111,4 +111,3 @@ def refine_item_options(item, options)
end
end
end

2 changes: 1 addition & 1 deletion lib/props_template/core_ext.rb
Expand Up @@ -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
Expand Down
55 changes: 0 additions & 55 deletions lib/props_template/debug_writer.rb

This file was deleted.

9 changes: 4 additions & 5 deletions 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
Expand Down

0 comments on commit 49b4ca3

Please sign in to comment.