Class: Ken::Resource

Inherits:
Object
  • Object
show all
Includes:
Extlib::Assertions
Defined in:
lib/ken/resource.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'lib/ken/resource.rb', line 5

def data
  @data
end

Instance Method Details

#attribute(name) ⇒ Object

search for an attribute by name and return it



81
82
83
84
# File 'lib/ken/resource.rb', line 81

def attribute(name)
  attributes.each { |a| return a if a.property.id == name }
  nil
end

#attributesObject

returns all attributes for every type the resource is an instance of



74
75
76
77
# File 'lib/ken/resource.rb', line 74

def attributes
  load_attributes! unless attributes_loaded?
  @attributes.values
end

#attributes_loaded?Boolean

returns true if attributes are already loaded



94
95
96
# File 'lib/ken/resource.rb', line 94

def attributes_loaded?
  @attributes_loaded
end

#data_fetched?Boolean

returns true if json data is already loaded



99
100
101
# File 'lib/ken/resource.rb', line 99

def data_fetched?
  @data_fetched
end

#guidObject

resource guid



22
23
24
# File 'lib/ken/resource.rb', line 22

def guid
  @data['guid'] || ""
end

#idObject

resource id



16
17
18
# File 'lib/ken/resource.rb', line 16

def id
  @data["id"] || ""
end

#inspectObject



38
39
40
# File 'lib/ken/resource.rb', line 38

def inspect
  result = "#<Resource id=\"#{id}\" name=\"#{name || "nil"}\">"
end

#nameObject

resource name



28
29
30
# File 'lib/ken/resource.rb', line 28

def name
  @data["name"] || ""
end

#propertiesObject

returns all the properties from all assigned types



64
65
66
67
68
69
70
# File 'lib/ken/resource.rb', line 64

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



88
89
90
# File 'lib/ken/resource.rb', line 88

def schema_loaded?
  @schema_loaded
end

#to_sObject



33
34
35
# File 'lib/ken/resource.rb', line 33

def to_s
  name || id || ""
end

#typesObject

returns all assigned types



44
45
46
47
# File 'lib/ken/resource.rb', line 44

def types
  load_schema! unless schema_loaded?
  @types
end

#view(type) ⇒ Object

returns individual view based on the requested type



57
58
59
60
# File 'lib/ken/resource.rb', line 57

def view(type)
  types.each { |t| return Ken::View.new(self, t) if t.id =~ /^#{Regexp.escape(type)}$/ }
  nil
end

#viewsObject

returns all available views based on the assigned types



51
52
53
# File 'lib/ken/resource.rb', line 51

def views
  @views ||= Ken::Collection.new(types.map { |type| Ken::View.new(self, type) })
end