Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moves dev dependencies into gemspec #247

Merged
merged 2 commits into from
Nov 16, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 11 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ sudo: false
language: ruby

cache: bundler

bundler_args: --without development

rvm:
- 2.3.1
- 2.3.0
- 2.2
- 2.1
- 2.0.0
- ruby-head
- jruby-head
- rbx-2
bundler_args: --without test

matrix:
include:
- rvm: 2.3.1
script:
- bundle exec danger
- rvm: 2.3.1
- rvm: 2.3.0
- rvm: 2.2
- rvm: 2.1
- rvm: ruby-head
- rvm: jruby-9.1.2.0
- rvm: jruby-head
- rvm: rbx-2

allow_failures:
- rvm: ruby-head
- rvm: jruby-9.1.2.0
- rvm: jruby-head
- rvm: rbx-2
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features

* [#247](https://github.com/ruby-grape/grape-entity/pull/247): Updates dependencies; refactores to make specs green - [@LeFnord](https://github.com/LeFnord).
* Your contribution here.

#### Fixes
Expand Down
29 changes: 7 additions & 22 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,19 @@ source 'http://rubygems.org'

gemspec

current_ruby_version = Gem::Version.new(RUBY_VERSION)

if Gem::Requirement.new('>= 2.2.2').satisfied_by? current_ruby_version
gem 'activesupport', '~> 5.0'
gem 'rack', '~> 2.0', group: [:development, :test]
else
gem 'activesupport', '~> 4.0'
gem 'rack', '< 2', group: [:development, :test]
if RUBY_VERSION < '2.2.2'
gem 'rack', '<2.0.0'
gem 'activesupport', '<5.0.0'
end

gem 'json', '< 2', group: [:development, :test]
group :development, :test do
gem 'ruby-grape-danger', '~> 0.1.0', require: false
end

group :development do
gem 'pry'
group :test do
gem 'guard'
gem 'guard-rspec'
gem 'guard-bundler'
gem 'rb-fsevent'
gem 'growl'
end

group :development, :test do
gem 'rake'
gem 'rspec'
gem 'rack-test', '~> 0.6.2', require: 'rack/test'
gem 'rubocop', '0.31.0'
end

group :test do
gem 'ruby-grape-danger', '~> 0.1.0', require: false
end
20 changes: 9 additions & 11 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
# encoding: utf-8

require 'rubygems'
require 'bundler'
Bundler.setup :default, :test, :development

Bundler.setup(:default, :development)

require 'rake'

Bundler::GemHelper.install_tasks

require 'rspec/core'
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
end

RSpec::Core::RakeTask.new(:rcov) do |spec|
spec.pattern = 'spec/**/*_spec.rb'
spec.rcov = true
end
RSpec::Core::RakeTask.new(:spec)

task :spec
require 'rainbow/ext/string' unless String.respond_to?(:color)
require 'rubocop/rake_task'
RuboCop::RakeTask.new(:rubocop)

task default: [:spec, :rubocop]
task default: [:rubocop, :spec]
13 changes: 9 additions & 4 deletions grape-entity.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,19 @@ Gem::Specification.new do |s|
s.rubyforge_project = 'grape-entity'

s.add_runtime_dependency 'multi_json', '>= 1.3.2'
s.add_runtime_dependency 'activesupport'

s.add_development_dependency 'bundler'
s.add_development_dependency 'rake'
s.add_development_dependency 'rubocop', '~> 0.40'
s.add_development_dependency 'rspec', '~> 3.0'
s.add_development_dependency 'rack-test'
s.add_development_dependency 'maruku'
s.add_development_dependency 'yard'
s.add_development_dependency 'rspec', '~> 2.9'
s.add_development_dependency 'bundler'
s.add_development_dependency 'pry' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')
s.add_development_dependency 'pry-byebug' unless RUBY_PLATFORM.eql?('java') || RUBY_ENGINE.eql?('rbx')

s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
s.executables = `git ls-files -- bin/*`.split("\n").map { |f| File.basename(f) }
s.test_files = `git ls-files -- {test,spec}/*`.split("\n")
s.require_paths = ['lib']
end
4 changes: 2 additions & 2 deletions lib/grape_entity/condition/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def initialize(inverse = false)
end

def ==(other)
(self.class == other.class) && (self.inversed? == other.inversed?)
(self.class == other.class) && (inversed? == other.inversed?)
end

def inversed?
Expand All @@ -23,7 +23,7 @@ def met?(entity, options)
end

def if_value(_entity, _options)
fail NotImplementedError
raise NotImplementedError
end

def unless_value(entity, options)
Expand Down
18 changes: 9 additions & 9 deletions lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ def self.expose(*args, &block)
options = merge_options(args.last.is_a?(Hash) ? args.pop : {})

if args.size > 1
fail ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as]
fail ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given?
raise ArgumentError, 'You may not use the :as option on multi-attribute exposures.' if options[:as]
raise ArgumentError, 'You may not use block-setting on multi-attribute exposures.' if block_given?
end

fail ArgumentError, 'You may not use block-setting when also using format_with' if block_given? && options[:format_with].respond_to?(:call)
raise ArgumentError, 'You may not use block-setting when also using format_with' if block_given? && options[:format_with].respond_to?(:call)

if block_given?
if block.parameters.any?
Expand Down Expand Up @@ -214,7 +214,7 @@ def self.can_unexpose?
end

def self.cannot_unexpose!
fail "You cannot call 'unexpose` inside of nesting exposure!"
raise "You cannot call 'unexpose` inside of nesting exposure!"
end

# Set options that will be applied to any exposures declared inside the block.
Expand Down Expand Up @@ -270,7 +270,7 @@ def self.documentation
# end
#
def self.format_with(name, &block)
fail ArgumentError, 'You must pass a block for formatters' unless block_given?
raise ArgumentError, 'You must pass a block for formatters' unless block_given?
formatters[name.to_sym] = block
end

Expand Down Expand Up @@ -392,8 +392,8 @@ def self.present_collection(present_collection = false, collection_name = :items
# @option options :only [Array] all the fields that should be returned
# @option options :except [Array] all the fields that should not be returned
def self.represent(objects, options = {})
if objects.respond_to?(:to_ary) && ! @present_collection
root_element = root_element(:collection_root)
if objects.respond_to?(:to_ary) && !@present_collection
root_element = root_element(:collection_root)
inner = objects.to_ary.map { |object| new(object, options.reverse_merge(collection: true)).presented }
else
objects = { @collection_name => objects } if @present_collection
Expand Down Expand Up @@ -485,7 +485,7 @@ def delegate_attribute(attribute)
end
end

alias_method :as_json, :serializable_hash
alias as_json serializable_hash

def to_json(options = {})
options = options.to_h if options && options.respond_to?(:to_h)
Expand Down Expand Up @@ -536,7 +536,7 @@ def self.merge_options(options)
# @param options [Hash] Exposure options.
def self.valid_options(options)
options.keys.each do |key|
fail ArgumentError, "#{key.inspect} is not a valid option." unless OPTIONS.include?(key)
raise ArgumentError, "#{key.inspect} is not a valid option." unless OPTIONS.include?(key)
end

options[:using] = options.delete(:with) if options.key?(:with)
Expand Down
4 changes: 2 additions & 2 deletions lib/grape_entity/exposure/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def valid?(entity)
if @is_safe
is_delegatable
else
is_delegatable || fail(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
is_delegatable || raise(NoMethodError, "#{entity.class.name} missing attribute `#{@attribute}' on #{entity.object}")
end
end

def value(_entity, _options)
fail NotImplementedError
raise NotImplementedError
end

def serializable_value(entity, options)
Expand Down
6 changes: 1 addition & 5 deletions lib/grape_entity/exposure/nesting_exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,7 @@ def normalized_exposures(entity, options)
# For the given key if the last candidates for exposing are nesting then combine them.
nesting_tail = []
exposures.reverse_each do |exposure|
if exposure.nesting?
nesting_tail.unshift exposure
else
break
end
nesting_tail.unshift exposure if exposure.nesting?
end
new_nested_exposures = nesting_tail.flat_map(&:nested_exposures)
NestingExposure.new(key, {}, [], new_nested_exposures).tap do |new_exposure|
Expand Down
16 changes: 6 additions & 10 deletions lib/grape_entity/exposure/nesting_exposure/output_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,20 @@ def add(exposure, result)
# Save a result array in collections' array if it should be merged
if result.is_a?(Array) && exposure.for_merge
@output_collection << result
else

elsif exposure.for_merge
# If we have an array which should not be merged - save it with a key as a hash
# If we have hash which should be merged - save it without a key (merge)
if exposure.for_merge
return unless result
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
else
@output_hash[exposure.key] = result
end

return unless result
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
else
@output_hash[exposure.key] = result
end
end

def kind_of?(klass)
klass == output.class || super
end
alias_method :is_a?, :kind_of?
alias is_a? kind_of?

def __getobj__
output
Expand Down
66 changes: 33 additions & 33 deletions lib/grape_entity/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,11 @@ def empty?
end

def ==(other)
if other.is_a? Options
@opts_hash == other.opts_hash
else
@opts_hash == other
end
@opts_hash == if other.is_a? Options
other.opts_hash
else
other
end
end

def should_return_key?(key)
Expand All @@ -79,42 +79,20 @@ def only_fields(for_key = nil)
return nil unless @has_only

@only_fields ||= @opts_hash[:only].each_with_object({}) do |attribute, allowed_fields|
if attribute.is_a?(Hash)
attribute.each do |attr, nested_attrs|
allowed_fields[attr] ||= []
allowed_fields[attr] += nested_attrs
end
else
allowed_fields[attribute] = true
end
end.symbolize_keys

if for_key && @only_fields[for_key].is_a?(Array)
@only_fields[for_key]
elsif for_key.nil?
@only_fields
build_symbolized_hash(attribute, allowed_fields)
end

only_for_given(for_key, @only_fields)
end

def except_fields(for_key = nil)
return nil unless @has_except

@except_fields ||= @opts_hash[:except].each_with_object({}) do |attribute, allowed_fields|
if attribute.is_a?(Hash)
attribute.each do |attr, nested_attrs|
allowed_fields[attr] ||= []
allowed_fields[attr] += nested_attrs
end
else
allowed_fields[attribute] = true
end
end.symbolize_keys

if for_key && @except_fields[for_key].is_a?(Array)
@except_fields[for_key]
elsif for_key.nil?
@except_fields
build_symbolized_hash(attribute, allowed_fields)
end

only_for_given(for_key, @except_fields)
end

def with_attr_path(part)
Expand All @@ -141,6 +119,28 @@ def build_for_nesting(key)

Options.new(new_opts_hash)
end

def build_symbolized_hash(attribute, hash)
if attribute.is_a?(Hash)
attribute.each do |attr, nested_attrs|
hash[attr.to_sym] = build_symbolized_hash(nested_attrs, {})
end
elsif attribute.is_a?(Array)
return attribute.each { |x| build_symbolized_hash(x, {}) }
else
hash[attribute.to_sym] = true
end

hash
end

def only_for_given(key, fields)
if key && fields[key].is_a?(Array)
fields[key]
elsif key.nil?
fields
end
end
end
end
end
Loading