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
# 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]
  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



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

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

#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)


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

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

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

Returns:

  • (Boolean)


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

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

#to_hashObject Also known as: to_h



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

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

#urlObject



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

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