Class: StripeMock::Data::List

Inherits:
Object
  • Object
show all
Defined in:
lib/stripe_mock/data/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ List

Returns a new instance of List.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/stripe_mock/data/list.rb', line 6

def initialize(data, options = {})
  @data = Array(data.clone)
  @limit = [[options[:limit] || 10, 100].min, 1].max # restrict @limit to 1..100
  @starting_after = options[:starting_after]
  @ending_before  = options[:ending_before]
  if @data.first.is_a?(Hash) && @data.first[:created]
    @data.sort_by! { |x| x[:created] }
    @data.reverse!
  elsif @data.first.respond_to?(:created)
    @data.sort_by { |x| x.created }
    @data.reverse!
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/stripe_mock/data/list.rb', line 33

def method_missing(method_name, *args, &block)
  hash = to_hash

  if hash.keys.include?(method_name)
    hash[method_name]
  else
    super
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/stripe_mock/data/list.rb', line 4

def data
  @data
end

#ending_beforeObject (readonly)

Returns the value of attribute ending_before.



4
5
6
# File 'lib/stripe_mock/data/list.rb', line 4

def ending_before
  @ending_before
end

#limitObject (readonly)

Returns the value of attribute limit.



4
5
6
# File 'lib/stripe_mock/data/list.rb', line 4

def limit
  @limit
end

#starting_afterObject (readonly)

Returns the value of attribute starting_after.



4
5
6
# File 'lib/stripe_mock/data/list.rb', line 4

def starting_after
  @starting_after
end

Instance Method Details

#has_more?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/stripe_mock/data/list.rb', line 29

def has_more?
  (offset + limit) < data.size
end

#respond_to?(method_name, priv = false) ⇒ Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/stripe_mock/data/list.rb', line 43

def respond_to?(method_name, priv = false)
  to_hash.keys.include?(method_name) || super
end

#to_hashObject Also known as: to_h



24
25
26
# File 'lib/stripe_mock/data/list.rb', line 24

def to_hash
  { object: "list", data: data_page, url: url, has_more: has_more? }
end

#urlObject



20
21
22
# File 'lib/stripe_mock/data/list.rb', line 20

def url
  "/v1/#{object_types}"
end