Skip to content
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
12 changes: 9 additions & 3 deletions lib/rubyzen/collections/classes_collection.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@

module Rubyzen
module Collections
class ClassesCollection < BaseCollection
include Rubyzen::Providers::CollectionFilterProvider

undef_method(:methods)

def all_methods
instance_plus_class_methods = flat_map { |klass| klass.instance_methods + klass.class_methods }
MethodsCollection.new(instance_plus_class_methods)
Expand All @@ -16,10 +13,19 @@ def attributes
AttributesCollection.new(all_attributes)
end

def macros
all_macros = flat_map(&:macros)
MacrosCollection.new(all_macros)
end

def with_parent_prefix(prefix)
filter { |klass| klass.superclass_prefix?(prefix) }
end

def with_macro_name(macro_name)
filter { |klass| klass.macros.with_name(macro_name).any? }
end

def +(other)
merged = super(other)
self.class.new(merged)
Expand Down
7 changes: 7 additions & 0 deletions lib/rubyzen/collections/macros_collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Rubyzen
module Collections
class MacrosCollection < BaseCollection
include Rubyzen::Providers::CollectionFilterProvider
end
end
end
1 change: 1 addition & 0 deletions lib/rubyzen/declarations/class_declaration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class ClassDeclaration
include Rubyzen::Providers::ClassNameProvider
include Rubyzen::Providers::ConstantsProvider
include Rubyzen::Providers::AttributesProvider
include Rubyzen::Providers::MacrosProvider

attr_reader :node, :file_declaration

Expand Down
52 changes: 52 additions & 0 deletions lib/rubyzen/declarations/macro_declaration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
module Rubyzen
module Declarations
class MacroDeclaration
include Rubyzen::Providers::FilePathProvider
include Rubyzen::Providers::ClassNameProvider
include Rubyzen::Providers::LineNumberProvider
include Rubyzen::Providers::SourceCodeProvider

attr_reader :node, :parent

def initialize(node, parent)
@node = node
@parent = parent
end

def name
node.method_name.to_s
end

def symbols
node.arguments.select { |arg| arg.type == :sym }.map(&:value)
end

def strings
node.arguments.select { |arg| arg.type == :str }.map(&:value)
end

def keyword_args
extract_keyword_args(node)
end

def receiver
node.receiver&.type == :const ? node.receiver.const_name : nil
end

private

def extract_keyword_args(send_node)
send_node.arguments.flat_map do |arg|
if arg.hash_type?
arg.each_pair.map do |pair|
key_node = pair.key
key_node.type == :sym ? key_node.value : nil
end.compact
else
[]
end
end.uniq
end
end
end
end
16 changes: 16 additions & 0 deletions lib/rubyzen/providers/collection_filter_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ def with_name_starting_with(prefix)
filter { |item| item.name&.start_with?(prefix) }
end

def with_name_including(substring, case_sensitive: true)
if case_sensitive
filter { |item| item.name&.include?(substring) }
else
filter { |item| item.name&.downcase&.include?(substring.downcase) }
end
end

def without_name(*names)
filter { |item| !names.include?(item.name) }
end
Expand All @@ -24,6 +32,14 @@ def without_name_ending_with(suffix)
def without_name_starting_with(prefix)
filter { |item| !item.name&.start_with?(prefix) }
end

def without_name_including(substring, case_sensitive: true)
if case_sensitive
filter { |item| !item.name&.include?(substring) }
else
filter { |item| !item.name&.downcase&.include?(substring.downcase) }
end
end
end
end
end
15 changes: 15 additions & 0 deletions lib/rubyzen/providers/macros_provider.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Rubyzen
module Providers
module MacrosProvider
def macros
macros_nodes = node.each_descendant(:send).select(&:macro?)

macros_declarations = macros_nodes.map do |macro_node|
Rubyzen::Declarations::MacroDeclaration.new(macro_node, self)
end

Rubyzen::Collections::MacrosCollection.new(macros_declarations)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require_relative '../spec_helper'

RSpec.describe 'Requests validate required profile id' do
context 'given a request that uses validates_required' do
it 'validates required profile id' do
expect(requests.with_macro_name('validates_required')).to be_true { |klass|
klass.macros.with_name('validates_required').any? do |macro|
macro.symbols.include?(:profile_id)
end
}
end
end
end
1 change: 1 addition & 0 deletions sample_project/spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
let(:actions) { project.files.with_paths('src/actions/').classes }
let(:services) { project.files.with_paths('src/services/').classes }
let(:jobs) { project.files.with_paths('src/jobs/').classes }
let(:requests) { project.files.with_paths('src/requests/').classes }

let(:models_files) { project.files.with_paths('src/models/') }
let(:models) { models_files.classes }
Expand Down
14 changes: 14 additions & 0 deletions sample_project/src/requests/age_verification.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Requests
module AgeVerification
class Result < Base::Request
extracts :device_id, :profile_id, :session_id

validates device_id: Validators::Devices::DeviceId,
profile_id: Validators::Profiles::Id

validates_required :session_id

returns :profile_id, :session_id
end
end
end
13 changes: 13 additions & 0 deletions sample_project/src/requests/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Requests
module Photo
class Result < Base::Request
extracts :device_id, :profile_id, :photo_id

validates device_id: Validators::Devices::DeviceId

validates_required :photo_id, :profile_id

returns :profile_id
end
end
end
Loading