Class: Grom::Base
Class Method Summary
collapse
Instance Method Summary
collapse
create_hash_from_ttl, get_id, get_ttl_data, statement_mapper, through_split_graph
Methods included from Helpers
all_base_url_builder, associations_url_builder, create_class_name, create_plural_property_name, create_property_name, find_base_url_builder, order_list, order_list_by_through
Constructor Details
#initialize(attributes) ⇒ Base
Returns a new instance of Base.
10
11
12
13
14
15
16
17
18
19
|
# File 'lib/grom/base.rb', line 10
def initialize(attributes)
attributes.each do |k, v|
translated_key = self.class.property_translator[k]
v = self.class.create_property_name(self.class.get_id(v)) if (v =~ URI::regexp) == 0
unless v.nil? || translated_key.nil?
instance_variable_set("@#{translated_key}", v)
self.class.send(:attr_reader, translated_key)
end
end
end
|
Class Method Details
.all(*options) ⇒ Object
27
28
29
30
31
32
|
# File 'lib/grom/base.rb', line 27
def self.all(*options)
endpoint_url = "#{all_base_url_builder(self.name, *options)}.ttl"
ttl_data = get_ttl_data(endpoint_url)
self.object_array_maker(ttl_data)
end
|
.find(id) ⇒ Object
21
22
23
24
25
|
# File 'lib/grom/base.rb', line 21
def self.find(id)
endpoint_url = "#{find_base_url_builder(self.name, id)}.ttl"
ttl_data = get_ttl_data(endpoint_url)
self.object_single_maker(ttl_data)
end
|
.has_many(association) ⇒ Object
34
35
36
|
# File 'lib/grom/base.rb', line 34
def self.has_many(association)
self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_many_query(self, *options); end")
end
|
.has_many_query(owner_object, *options) ⇒ Object
47
48
49
50
51
|
# File 'lib/grom/base.rb', line 47
def self.has_many_query(owner_object, *options)
endpoint_url = associations_url_builder(owner_object, self.name, {optional: options })
ttl_data = get_ttl_data(endpoint_url)
self.object_array_maker(ttl_data)
end
|
.has_many_through(association, through_association) ⇒ Object
38
39
40
41
|
# File 'lib/grom/base.rb', line 38
def self.has_many_through(association, through_association)
self.has_many(through_association[:via])
self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_many_through_query(self, #{create_class_name(through_association[:via])}.new({}).class.name, *options); end")
end
|
.has_many_through_query(owner_object, through_class, *options) ⇒ Object
74
75
76
77
78
79
80
|
# File 'lib/grom/base.rb', line 74
def self.has_many_through_query(owner_object, through_class, *options)
through_property_plural = create_plural_property_name(through_class)
endpoint_url = associations_url_builder(owner_object, self.name, {optional: options })
self.through_getter_setter(through_property_plural)
ttl_data = get_ttl_data(endpoint_url)
self.map_hashes_to_objects(through_split_graph(ttl_data), through_property_plural)
end
|
.has_one(association) ⇒ Object
43
44
45
|
# File 'lib/grom/base.rb', line 43
def self.has_one(association)
self.class_eval("def #{association}(*options); #{create_class_name(association)}.has_one_query(self, *options); end")
end
|
.has_one_query(owner_object, *options) ⇒ Object
53
54
55
56
57
|
# File 'lib/grom/base.rb', line 53
def self.has_one_query(owner_object, *options)
endpoint_url = associations_url_builder(owner_object, self.name, {optional: options, single: true })
ttl_data = get_ttl_data(endpoint_url)
self.object_single_maker(ttl_data)
end
|
.map_hashes_to_objects(hashes, through_property_plural) ⇒ Object
82
83
84
85
86
87
88
89
90
91
92
|
# File 'lib/grom/base.rb', line 82
def self.map_hashes_to_objects(hashes, through_property_plural)
through_array = hashes[:through_class_hash].values
hashes[:associated_class_hash].values.map do |hash|
associated_object = self.new(hash)
through_obj_array, through_array = through_array.partition do |t_hash|
t_hash[:associated_object_id] == associated_object.id
end
associated_object.send((through_property_plural + '=').to_sym, through_obj_array)
associated_object
end
end
|
.object_array_maker(ttl_data) ⇒ Object
64
65
66
67
68
|
# File 'lib/grom/base.rb', line 64
def self.object_array_maker(ttl_data)
create_hash_from_ttl(ttl_data).map do |hash|
self.new(hash)
end
end
|
.object_single_maker(ttl_data) ⇒ Object
70
71
72
|
# File 'lib/grom/base.rb', line 70
def self.object_single_maker(ttl_data)
self.object_array_maker(ttl_data).first
end
|
.through_getter_setter(through_property_plural) ⇒ Object
59
60
61
62
|
# File 'lib/grom/base.rb', line 59
def self.through_getter_setter(through_property_plural)
self.class_eval("def #{through_property_plural}=(array); @#{through_property_plural} = array; end")
self.class_eval("def #{through_property_plural}; @#{through_property_plural}; end")
end
|