forked from ontoportal/ontologies_api_ruby_client
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.rb
More file actions
167 lines (143 loc) · 4.53 KB
/
base.rb
File metadata and controls
167 lines (143 loc) · 4.53 KB
1
2
3
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module LinkedData
module Client
class Base
HTTP = LinkedData::Client::HTTP
attr_writer :instance_values
attr_accessor :context, :links
class << self
attr_accessor :act_as_media_type, :include_attrs, :include_attrs_full, :attrs_always_present
def media_types
Array(@media_type)
end
def media_type
media_types.first
end
end
##
# Passing full: true to explore methods will give you more attributes
def self.attributes(full = false)
if full && @include_attrs_full
@include_attrs_full
else
@include_attrs
end
end
def self.class_for_type(media_type)
if defined? @media_type_class_map
return @media_type_class_map[media_type]
end
@media_type_class_map = map_classes
return @media_type_class_map[media_type]
end
def self.map_classes
map = {}
classes = LinkedData::Client::Models.constants.map {|c| LinkedData::Client::Models.const_get(c)}
classes.each do |media_type_cls|
next if map[media_type_cls] || !media_type_cls.respond_to?(:media_types) || !media_type_cls.ancestors.include?(LinkedData::Client::Base)
media_type_cls.media_types.each do |type|
next if map[type]
map[type] = media_type_cls
end
media_type_cls.act_as_media_type.each {|mt| map[mt] = media_type_cls} if media_type_cls.act_as_media_type
end
return map
end
def initialize(options = {})
values = options[:values]
if values.is_a?(Hash) && !values.empty?
create_attributes(values.keys)
populate_attributes(values)
end
create_attributes(self.class.attrs_always_present || [])
end
def id
@id
end
def type
@type
end
def self.explore(id)
path = self.respond_to?(:collection_path) ? collection_path : ''
id = "#{path}/#{id}" unless id.include?(path)
inst = self.new(values: {id: id})
LinkedData::Client::LinkExplorer.new({}, inst)
end
##
# Retrieve a set of data using a link provided on an object
# This instantiates an instance of this class and uses
# method missing to determine which link to follow
def explore
LinkedData::Client::LinkExplorer.new(@links, self)
end
def to_hash
dump = marshal_dump
dump.keys.each do |k|
next unless k.to_s[0].eql?("@")
dump[k[1..-1].to_sym] = dump[k]
dump.delete(k)
end
dump
end
alias :to_param :to_hash
def to_jsonld
HTTP.get(self.id, {}, {raw: true})
end
def marshal_dump
Hash[self.instance_variables.map { |v| [v, self.instance_variable_get("#{v}")] }]
end
def marshal_load(data)
data ||= {}
create_attributes(data.keys)
populate_attributes(data)
end
def method_missing(meth, *args, &block)
if meth.to_s[-1].eql?("=")
# This enables OpenStruct-like behavior for setting attributes that aren't defined
attr = meth.to_s.chomp("=").to_sym
create_attributes([attr])
populate_attributes(attr => args.first)
else
nil
end
end
def respond_to?(meth, private = false)
true
end
def [](key)
key = "@#{key}" unless key.to_s.start_with?("@")
instance_variable_get(key)
end
def []=(key, value)
create_attributes([key])
populate_attributes({key.to_sym => value})
end
private
def create_attributes(attributes)
attributes.each do |attr|
attr = attr.to_s[1..-1].to_sym if attr.to_s.start_with?("@")
attr_exists = self.public_methods(false).include?(attr)
unless attr_exists
self.class.class_eval do
unless method_defined?(attr.to_sym)
define_method attr.to_sym do
instance_variable_get("@#{attr}")
end
end
unless method_defined?("#{attr}=".to_sym)
define_method "#{attr}=" do |val|
instance_variable_set("@#{attr}", val)
end
end
end
end
end
end
def populate_attributes(hash)
hash.each do |k,v|
k = "@#{k}" unless k.to_s.start_with?("@")
instance_variable_set(k, v)
end
end
end
end
end