Class: Grom::Base

Inherits:
Object
  • Object
show all
Extended by:
GraphMapper, Helpers
Defined in:
lib/grom/base.rb

Class Method Summary collapse

Instance Method Summary collapse

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, json_ld, json_ld_object_mapper, order_list, order_list_by_through

Methods included from GraphMapper

create_hash_from_ttl, get_id, get_ttl_data, statement_mapper, statements_with_mapper, through_split_graph

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

.all_with(*options, with_properties) ⇒ Object


107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/grom/base.rb', line 107

def self.all_with(*options, with_properties)
  endpoint_url = "#{all_base_url_builder(self.name, *options)}.ttl"
  ttl_data = get_ttl_data(endpoint_url)
  all_hashes = statements_with_mapper(ttl_data)
  with_properties.each do |property|
    self.property_getter_setter(property)
  end
  owner_object_hashes, associated_hashes = all_hashes.partition do |h|
    get_id(h[:type]) == self.name.to_s
  end
  object_with_array_maker(associated_hashes, owner_object_hashes)
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


87
88
89
90
91
92
93
# File 'lib/grom/base.rb', line 87

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.property_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


95
96
97
98
99
100
101
102
103
104
105
# File 'lib/grom/base.rb', line 95

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

.object_with_array_maker(associated_hashes, owner_object_hashes) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/grom/base.rb', line 74

def self.object_with_array_maker(associated_hashes, owner_object_hashes)
  owner_object_array = []
  owner_object_hashes.each do |owner_hash|
    object = self.new(owner_hash)
    associated_hashes.select { |h| h[:connect].include?(object.id) }.each do |associated_hash|
      associated_object_name = get_id(associated_hash[:type])
      object.send((create_property_name(associated_object_name) + '=').to_sym, associated_object_name.constantize.new(associated_hash))
    end
    owner_object_array << object
  end
  owner_object_array
end

.property_getter_setter(property_name) ⇒ Object


59
60
61
62
# File 'lib/grom/base.rb', line 59

def self.property_getter_setter(property_name)
  self.class_eval("def #{property_name}=(value); @#{property_name} = value; end")
  self.class_eval("def #{property_name}; @#{property_name}; end")
end