Class: Ken::Resource

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

Instance Method Summary collapse

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

#attributesObject

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

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


90
91
92
# File 'lib/ken/resource.rb', line 90

def data_fetched?
  @data_fetched
end

#guidObject

resource guid



20
21
22
# File 'lib/ken/resource.rb', line 20

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

#idObject

resource id



14
15
16
# File 'lib/ken/resource.rb', line 14

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

#inspectObject



36
37
38
# File 'lib/ken/resource.rb', line 36

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

#nameObject

resource name



26
27
28
# File 'lib/ken/resource.rb', line 26

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

#propertiesObject

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

Returns:

  • (Boolean)


79
80
81
# File 'lib/ken/resource.rb', line 79

def schema_loaded?
  @schema_loaded
end

#to_sObject



31
32
33
# File 'lib/ken/resource.rb', line 31

def to_s
  name || id || ""
end

#typesObject

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

Raises:



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

#viewsObject

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