Class: TryApi::Base
- Inherits:
-
Object
show all
- Defined in:
- app/models/try_api/base.rb
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(hash) ⇒ Base
Returns a new instance of Base.
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'app/models/try_api/base.rb', line 64
def initialize(hash)
result_hash = {}
hash.to_h.each do |k, v|
if k.to_s == 'include!'
result_hash.merge! load_inclusion(v)
else
result_hash[k] = v
end
end
result_hash.to_h.each do |k, v|
v = self.project.variables[v.gsub('var:', '')] if v.is_a?(String) && v.start_with?('var:')
send("#{k}=", v) if respond_to?("#{k}=")
end
end
|
Instance Attribute Details
#id ⇒ Object
Returns the value of attribute id.
35
36
37
|
# File 'app/models/try_api/base.rb', line 35
def id
@id
end
|
Class Method Details
.load_inclusion(filename) ⇒ Object
80
81
82
83
84
85
86
87
|
# File 'app/models/try_api/base.rb', line 80
def self.load_inclusion(filename)
if File.exist?("#{ Rails.root }/config/try_api/#{ filename }.yml")
hash = YAML.load_file("#{ Rails.root }/config/try_api/#{ filename }.yml")
hash.with_indifferent_access
else
raise ConfigFileNotFound, "#{ Rails.root }/config/try_api/#{ filename }.yml"
end
end
|
.typesafe_accessor(name, type, options = {}) ⇒ Object
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
# File 'app/models/try_api/base.rb', line 4
def self.typesafe_accessor(name, type, options={})
define_method(name) do
instance_variable_get("@#{name}")
end
define_method("#{name}=") do |value|
if type.is_a?(Array) ? type.any?{|t| value.is_a?(t) } : value.is_a?(type) || value.nil?
if options[:items_type].try(:<, TryApi::Base)
items = []
if value.is_a? Array
value.each do |item|
item = load_inclusion(item[:include!]) if item[:include!].present?
items << options[:items_type].new({ parent: self }.merge(item))
end
end
instance_variable_set("@#{name}", items)
else
value = options[:default] if value.nil? && !options[:default].nil?
instance_variable_set("@#{name}", value)
end
else
raise TryApi::ArgumentError.new "#{ self.class.name }##{ name }=#{ value }(#{ value.class.name })"
end
end
end
|
Instance Method Details
#load_inclusion(filename) ⇒ Object
89
90
91
|
# File 'app/models/try_api/base.rb', line 89
def load_inclusion(filename)
self.class.load_inclusion filename
end
|
#project ⇒ Object
60
61
62
|
# File 'app/models/try_api/base.rb', line 60
def project
self.is_a?(TryApi::Project) ? self : self.parent.try(:project)
end
|
#to_json(id = 1) ⇒ Object
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/models/try_api/base.rb', line 37
def to_json(id = 1)
self.id = id
result = {}
self.instance_variables.each do |i|
value = self.instance_variable_get(i)
if value.instance_of?(Array)
result[i.to_s.delete('@')] = value.map do |v|
id += 1
v.to_json(id)
end
else
if i == :@parent
else
result[i.to_s.delete('@')] = value
end
end
end
result.with_indifferent_access
end
|