Class: Ken::Resource
- Inherits:
-
Object
- Object
- Ken::Resource
- Extended by:
- Extlib::Assertions
- Includes:
- Extlib::Assertions
- Defined in:
- lib/ken/resource.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
Class Method Summary collapse
-
.get(id) ⇒ Object
Executes an Mql Query against the Freebase API and returns the result wrapped in a
ResourceObject.
Instance Method Summary collapse
-
#attribute(name) ⇒ Object
search for an attribute by name and return it.
-
#attributes ⇒ Object
returns all attributes for every type the resource is an instance of.
-
#attributes_loaded? ⇒ Boolean
returns true if attributes are already loaded.
-
#data_fetched? ⇒ Boolean
returns true if json data is already loaded.
-
#guid ⇒ Object
resource guid.
-
#id ⇒ Object
resource id.
-
#initialize(data) ⇒ Resource
constructor
initializes a resource using a json result.
- #inspect ⇒ Object
-
#name ⇒ Object
resource name.
-
#properties ⇒ Object
returns all the properties from all assigned types.
-
#schema_loaded? ⇒ Boolean
returns true if type information is already loaded.
- #to_s ⇒ Object
-
#type(type) ⇒ Object
returns individual type based on the requested type id.
-
#types ⇒ Object
returns all assigned types.
-
#view(type) ⇒ Object
returns individual view based on the requested type id.
-
#views ⇒ Object
returns all available views based on the assigned types.
Constructor Details
#initialize(data) ⇒ Resource
initializes a resource using a json result
8 9 10 11 12 |
# File 'lib/ken/resource.rb', line 8 def initialize(data) assert_kind_of 'data', data, Hash @schema_loaded, @attributes_loaded, @data = false, false, data @data_fechted = data["/type/reflect/any_master"] != nil end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
5 6 7 |
# File 'lib/ken/resource.rb', line 5 def data @data end |
Class Method Details
.get(id) ⇒ Object
Executes an Mql Query against the Freebase API and returns the result wrapped in a Resource Object.
Examples
Ken::Resource.get('/en/the_police') => #<Resource id="/en/the_police" name="The Police">
21 22 23 24 25 26 |
# File 'lib/ken/resource.rb', line 21 def self.get(id) assert_kind_of 'id', id, String result = Ken.session.mqlread(FETCH_DATA_QUERY.merge!(:id => id)) raise ResourceNotFound unless result Ken::Resource.new(result) end |
Instance Method Details
#attribute(name) ⇒ Object
search for an attribute by name and return it
102 103 104 105 |
# File 'lib/ken/resource.rb', line 102 def attribute(name) attributes.each { |a| return a if a.property.id == name } nil end |
#attributes ⇒ Object
returns all attributes for every type the resource is an instance of
95 96 97 98 |
# File 'lib/ken/resource.rb', line 95 def attributes load_attributes! unless attributes_loaded? @attributes.values end |
#attributes_loaded? ⇒ Boolean
returns true if attributes are already loaded
115 116 117 |
# File 'lib/ken/resource.rb', line 115 def attributes_loaded? @attributes_loaded end |
#data_fetched? ⇒ Boolean
returns true if json data is already loaded
120 121 122 |
# File 'lib/ken/resource.rb', line 120 def data_fetched? @data_fetched end |
#guid ⇒ Object
resource guid
36 37 38 |
# File 'lib/ken/resource.rb', line 36 def guid @data['guid'] || "" end |
#id ⇒ Object
resource id
30 31 32 |
# File 'lib/ken/resource.rb', line 30 def id @data["id"] || "" end |
#inspect ⇒ Object
52 53 54 |
# File 'lib/ken/resource.rb', line 52 def inspect result = "#<Resource id=\"#{id}\" name=\"#{name || "nil"}\">" end |
#name ⇒ Object
resource name
42 43 44 |
# File 'lib/ken/resource.rb', line 42 def name @data["name"] || "" end |
#properties ⇒ Object
returns all the properties from all assigned types
85 86 87 88 89 90 91 |
# File 'lib/ken/resource.rb', line 85 def properties @properties = Ken::Collection.new types.each do |type| @properties.concat(type.properties) end @properties end |
#schema_loaded? ⇒ Boolean
returns true if type information is already loaded
109 110 111 |
# File 'lib/ken/resource.rb', line 109 def schema_loaded? @schema_loaded end |
#to_s ⇒ Object
47 48 49 |
# File 'lib/ken/resource.rb', line 47 def to_s name || id || "" end |
#type(type) ⇒ Object
returns individual type based on the requested type id
78 79 80 81 |
# File 'lib/ken/resource.rb', line 78 def type(type) types.each { |t| return t if t.id =~ /^#{Regexp.escape(type)}$/} nil end |
#types ⇒ Object
returns all assigned types
58 59 60 61 |
# File 'lib/ken/resource.rb', line 58 def types load_schema! unless schema_loaded? @types end |
#view(type) ⇒ Object
returns individual view based on the requested type id
71 72 73 74 |
# File 'lib/ken/resource.rb', line 71 def view(type) views.each { |v| return v if v.type.id =~ /^#{Regexp.escape(type)}$/} nil end |
#views ⇒ Object
returns all available views based on the assigned types
65 66 67 |
# File 'lib/ken/resource.rb', line 65 def views @views ||= Ken::Collection.new(types.map { |type| Ken::View.new(self, type) }) end |