Class: LinkedData::Client::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ontologies_api_client/base.rb

Constant Summary collapse

HTTP =
LinkedData::Client::HTTP

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



31
32
33
34
35
36
37
38
39
# File 'lib/ontologies_api_client/base.rb', line 31

def initialize(options = {})
  read_only = options.delete(:read_only) || false
  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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/ontologies_api_client/base.rb', line 82

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

Class Attribute Details

.attrs_always_presentObject

Returns the value of attribute attrs_always_present.



9
10
11
# File 'lib/ontologies_api_client/base.rb', line 9

def attrs_always_present
  @attrs_always_present
end

.include_attrsObject

Returns the value of attribute include_attrs.



9
10
11
# File 'lib/ontologies_api_client/base.rb', line 9

def include_attrs
  @include_attrs
end

.include_attrs_fullObject

Returns the value of attribute include_attrs_full.



9
10
11
# File 'lib/ontologies_api_client/base.rb', line 9

def include_attrs_full
  @include_attrs_full
end

.media_typeObject

Returns the value of attribute media_type.



9
10
11
# File 'lib/ontologies_api_client/base.rb', line 9

def media_type
  @media_type
end

Instance Attribute Details

#contextObject

Returns the value of attribute context.



6
7
8
# File 'lib/ontologies_api_client/base.rb', line 6

def context
  @context
end

#instance_values=(value) ⇒ Object (writeonly)

Sets the attribute instance_values

Parameters:

  • value

    the value to set the attribute instance_values to.



5
6
7
# File 'lib/ontologies_api_client/base.rb', line 5

def instance_values=(value)
  @instance_values = value
end

Returns the value of attribute links.



6
7
8
# File 'lib/ontologies_api_client/base.rb', line 6

def links
  @links
end

Class Method Details

.attributes(full = false) ⇒ Object

Passing full: true to explore methods will give you more attributes



14
15
16
17
18
19
20
# File 'lib/ontologies_api_client/base.rb', line 14

def self.attributes(full = false)
  if full && @include_attrs_full
    @include_attrs_full
  else
    @include_attrs
  end
end

.class_for_type(media_type) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/ontologies_api_client/base.rb', line 22

def self.class_for_type(media_type)
  classes = LinkedData::Client::Models.constants
  classes.each do |cls|
    media_type_cls = LinkedData::Client::Models.const_get(cls)
    return media_type_cls if media_type_cls.media_type.eql?(media_type)
  end
  nil
end

Instance Method Details

#[](key) ⇒ Object



97
98
99
100
# File 'lib/ontologies_api_client/base.rb', line 97

def [](key)
  key = "@#{key}" unless key.to_s.start_with?("@")
  instance_variable_get(key)
end

#[]=(key, value) ⇒ Object



102
103
104
105
# File 'lib/ontologies_api_client/base.rb', line 102

def []=(key, value)
  create_attributes([key])
  populate_attributes({key.to_sym => value})
end

#exploreObject

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



53
54
55
# File 'lib/ontologies_api_client/base.rb', line 53

def explore
  LinkedData::Client::LinkExplorer.new(@links, self)
end

#idObject



41
42
43
# File 'lib/ontologies_api_client/base.rb', line 41

def id
  @id
end

#marshal_dumpObject



72
73
74
# File 'lib/ontologies_api_client/base.rb', line 72

def marshal_dump
  Hash[self.instance_variables.map { |v| [v, self.instance_variable_get("#{v}")] }]
end

#marshal_load(data) ⇒ Object



76
77
78
79
80
# File 'lib/ontologies_api_client/base.rb', line 76

def marshal_load(data)
  data ||= {}
  create_attributes(data.keys)
  populate_attributes(data)
end

#respond_to?(meth, private = false) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/ontologies_api_client/base.rb', line 93

def respond_to?(meth, private = false)
  true
end

#to_hashObject Also known as: to_param



57
58
59
60
61
62
63
64
65
# File 'lib/ontologies_api_client/base.rb', line 57

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

#to_jsonldObject



68
69
70
# File 'lib/ontologies_api_client/base.rb', line 68

def to_jsonld
  HTTP.get(self.id, {}, {raw: true})
end

#typeObject



45
46
47
# File 'lib/ontologies_api_client/base.rb', line 45

def type
  @type
end