Class: Fakery::Fake
- Inherits:
-
JSON::GenericObject
- Object
- JSON::GenericObject
- Fakery::Fake
show all
- Includes:
- Change::Support
- Defined in:
- lib/fakery/fake.rb
Class Method Summary
collapse
Instance Method Summary
collapse
#record_change
Constructor Details
#initialize ⇒ Fake
Returns a new instance of Fake.
41
42
43
44
|
# File 'lib/fakery/fake.rb', line 41
def initialize(*)
super
@changes = Set[]
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(id, *args, &block) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
|
# File 'lib/fakery/fake.rb', line 87
def method_missing(id, *args, &block)
if id =~ /=\z/
args.size > 1 and
raise ArgumentError, "wrong number of arguments (#{len} for 1)", caller(1)
name = $`.to_sym
value = args.first
self.class.ignore_changes? or record_change(name, value)
modifiable[name] = value
else
super
end
end
|
Class Method Details
.cast(fake) ⇒ Object
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/fakery/fake.rb', line 5
def cast(fake)
case
when self === fake
fake
when Fakery.registered?(fake)
Fakery.build(fake)
when fake.respond_to?(:to_hash)
from_hash(fake.to_hash)
when fake.respond_to?(:read)
cast(fake.read)
when fake.respond_to?(:to_str)
from_json(fake.to_str)
when fake.respond_to?(:as_json)
from_hash(fake.as_json)
else
raise ArgumentError, "cannot cast #{fake.inspect}"
end
end
|
.from_hash(*a, &b) ⇒ Object
24
25
26
|
# File 'lib/fakery/fake.rb', line 24
def from_hash(*a, &b)
ignore_changes { super }
end
|
.from_json(json) ⇒ Object
28
29
30
|
# File 'lib/fakery/fake.rb', line 28
def from_json(json)
JSON.parse(json, object_class: self)
end
|
.seed_from_url(api_seed_url) ⇒ Object
32
33
34
35
36
37
38
|
# File 'lib/fakery/fake.rb', line 32
def seed_from_url(api_seed_url)
ignore_changes do
new.tap do |obj|
obj.__api_seed_url__ = api_seed_url
end.__send__(:reseed)
end
end
|
Instance Method Details
#as_json ⇒ Object
46
47
48
|
# File 'lib/fakery/fake.rb', line 46
def as_json(*)
to_hash
end
|