Module: DSL::Resource::ClassMethods

Defined in:
lib/rspec-api/dsl/resource.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.define_action(verb) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/rspec-api/dsl/resource.rb', line 11

def self.define_action(verb)
  define_method verb do |route, args = {}, &block|
    rspec_api.merge! array: args.delete(:array), verb: verb, route: route
    args.merge! rspec_api_dsl: :route
    describe("#{verb.upcase} #{route}", args, &block)
  end
end

Instance Method Details

#accepts_callback(callback_parameter) ⇒ Object



63
64
65
# File 'lib/rspec-api/dsl/resource.rb', line 63

def accepts_callback(callback_parameter)
  (rspec_api[:callbacks] ||= []) << {name: callback_parameter.to_s, value: 'a_callback'}
end

#accepts_filter(filter_parameter, options = {}) ⇒ Object



59
60
61
# File 'lib/rspec-api/dsl/resource.rb', line 59

def accepts_filter(filter_parameter, options={})
  (rspec_api[:filters] ||= []) << options.merge(name: filter_parameter)
end

#accepts_page(page_parameter) ⇒ Object



51
52
53
# File 'lib/rspec-api/dsl/resource.rb', line 51

def accepts_page(page_parameter)
  rspec_api[:page] = {name: page_parameter, value: 2}
end

#accepts_sort(sort_parameter, options = {}) ⇒ Object



55
56
57
# File 'lib/rspec-api/dsl/resource.rb', line 55

def accepts_sort(sort_parameter, options={})
  (rspec_api[:sorts] ||= []) << options.merge(name: 'sort', value: sort_parameter)
end

#has_attribute(name, options = {}, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rspec-api/dsl/resource.rb', line 26

def has_attribute(name, options = {}, &block)
  if block_given?
    # options[:type] can be symbol, hash or array
    # but if you have a block we must make it a hash
    options[:type] = Hash[*Array.wrap(options[:type]).map{|x| x.is_a?(Hash) ? [x.keys.first, x.values.first] : [x, {}]}.flatten] unless options[:type].is_a? Hash
    # we only set the block as the new format of Object and Array
    nest_attributes(options[:type], &Proc.new)
  end
  if @attribute_ancestors.present?
    hash = @attribute_ancestors.last
    hash.slice(:object, :array).each do |type, _|
      (hash[type] ||= {})[name] = options
    end
  else
    hash = (rspec_api[:attributes] ||= {})
    hash[name] = options
  end
end

#nest_attributes(hash, &block) ⇒ Object



45
46
47
48
49
# File 'lib/rspec-api/dsl/resource.rb', line 45

def nest_attributes(hash, &block)
  (@attribute_ancestors ||= []).push hash
  yield
  @attribute_ancestors.pop
end

#rspec_apiObject



7
8
9
# File 'lib/rspec-api/dsl/resource.rb', line 7

def rspec_api
  [:rspec_api]
end