Class: Ken::Resource
- Inherits:
-
Object
- Object
- Ken::Resource
- Includes:
- Extlib::Assertions
- Defined in:
- lib/ken/resource.rb
Instance Method Summary collapse
-
#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
-
#types ⇒ Object
returns all assigned types.
-
#view(type) ⇒ Object
returns individual view based on the requested type.
-
#views ⇒ Object
returns all available views based on the assigned types.
Constructor Details
#initialize(data) ⇒ Resource
initializes a resource using a json result
6 7 8 9 10 |
# File 'lib/ken/resource.rb', line 6 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 Method Details
#attributes ⇒ Object
returns all attributes for every type the resource is an instance of
72 73 74 75 |
# File 'lib/ken/resource.rb', line 72 def attributes load_attributes! unless attributes_loaded? @attributes.values end |
#attributes_loaded? ⇒ Boolean
returns true if attributes are already loaded
85 86 87 |
# File 'lib/ken/resource.rb', line 85 def attributes_loaded? @attributes_loaded end |
#data_fetched? ⇒ Boolean
returns true if json data is already loaded
90 91 92 |
# File 'lib/ken/resource.rb', line 90 def data_fetched? @data_fetched end |
#guid ⇒ Object
resource guid
20 21 22 |
# File 'lib/ken/resource.rb', line 20 def guid @data['guid'] || "" end |
#id ⇒ Object
resource id
14 15 16 |
# File 'lib/ken/resource.rb', line 14 def id @data["id"] || "" end |
#inspect ⇒ Object
36 37 38 |
# File 'lib/ken/resource.rb', line 36 def inspect result = "#<Resource id=\"#{id}\" name=\"#{name || "nil"}\">" end |
#name ⇒ Object
resource name
26 27 28 |
# File 'lib/ken/resource.rb', line 26 def name @data["name"] || "" end |
#properties ⇒ Object
returns all the properties from all assigned types
62 63 64 65 66 67 68 |
# File 'lib/ken/resource.rb', line 62 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
79 80 81 |
# File 'lib/ken/resource.rb', line 79 def schema_loaded? @schema_loaded end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/ken/resource.rb', line 31 def to_s name || id || "" end |
#types ⇒ Object
returns all assigned types
42 43 44 45 |
# File 'lib/ken/resource.rb', line 42 def types load_schema! unless schema_loaded? @types end |
#view(type) ⇒ Object
returns individual view based on the requested type
55 56 57 58 |
# File 'lib/ken/resource.rb', line 55 def view(type) types.each { |t| return Ken::View.new(self, t) if t.id =~ /^#{Regexp.escape(type)}$/ } raise ViewNotFound end |
#views ⇒ Object
returns all available views based on the assigned types
49 50 51 |
# File 'lib/ken/resource.rb', line 49 def views @views ||= Ken::Collection.new(types.map { |type| Ken::View.new(self, type) }) end |