Class: Unimatrix::Resource
- Inherits:
-
Object
- Object
- Unimatrix::Resource
show all
- Defined in:
- lib/unimatrix/resource.rb
Direct Known Subclasses
Alchemist::ActivityProxy, Alchemist::Rendition, Alchemist::RenditionProfile, Alchemist::Text, Alchemist::Video, Alchemist::VideoEncoder, Analyst::Rule, Archivist::Artifact, Archivist::ArtifactLocator, Archivist::ArtifactRelationship, Archivist::Blueprint, Archivist::BlueprintAttribute, Archivist::Component, Authorization::Error, Authorization::Policy, Authorization::Resource, Authorization::ResourceOwner, Authorization::ResourceServer, Distributor::ActivityReference, DynamicResource, Error, Historian::History, Iris::Stream, Iris::StreamConverter, Iris::StreamEncoder, Iris::StreamInput, Iris::StreamOutput, Iris::StreamRecorder, Iris::StreamTranscriber, Iris::StreamTransformer, Iris::StreamTransmutator, Organizer::Filter, Organizer::FilterCriteria, Player::Media, Player::MediaSource, Player::MediaText, Quartermaster::Binary, Quartermaster::BinaryIngressor, Unimatrix::Regent::Realm, Zephyrus::Input, Zephyrus::Output, Zephyrus::Rendition
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attributes = {}, associations = {}) {|_self| ... } ⇒ Resource
Returns a new instance of Resource.
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/unimatrix/resource.rb', line 84
def initialize( attributes={}, associations={} )
self.nested_fields.each do | key, value |
self.send( "#{ key }=", Struct.new( *value ).new )
end
self.type_name = self.class.name.split( '::' ).last.underscore rescue nil
attributes.each do | key, value |
send( "#{ key }=", value ) if self.respond_to?( "#{ key }=" )
end
associations.each do | key, value |
self.instance_variable_set( "@_#{ key }", value )
end
yield self if block_given?
end
|
Class Method Details
.build(attributes = {}, associations = {}) ⇒ Object
30
31
32
|
# File 'lib/unimatrix/resource.rb', line 30
def build( attributes = {}, associations = {} )
new( attributes.transform_keys( &:to_s ), associations )
end
|
.field(name, options = {}) ⇒ Object
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
# File 'lib/unimatrix/resource.rb', line 34
def field( name, options = {} )
if name.is_a?( Hash )
nested_field_key = name.keys.first
self.nested_fields[ nested_field_key ] = name[ nested_field_key ]
name = nested_field_key
end
self.fields[ name.to_sym ] = options.merge( name: name )
class_eval(
"def #{ name }(); " +
"@#{ name }.is_a?( FalseClass ) ? @#{ name } : (" +
"@#{ name } || " +
( options[ :default ].nil? ?
"nil" :
( options[ :default ].is_a?( String ) ?
"'#{ options[ :default ] }'" :
"#{ options[ :default ] }" ) ) + ");" +
"end;" +
" " +
"attr_writer :#{ name };",
__FILE__,
__LINE__
)
end
|
.find_by_type_name(type_name) ⇒ Object
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/unimatrix/resource.rb', line 17
def find_by_type_name( type_name )
@@descendants_by_type_name ||= begin
result = {}
descendants.each do | descendant |
descendant_type_name = descendant.type_name
result[ descendant_type_name ] = descendant \
unless descendant_type_name.blank?
end
result
end
@@descendants_by_type_name[ type_name ]
end
|
.has_many(name, options = {}) ⇒ Object
66
67
68
69
70
71
|
# File 'lib/unimatrix/resource.rb', line 66
def has_many( name, options = {} )
define_method name do
self.instance_variable_get( "@_#{ name }" ) ||
options[ :default ] || []
end
end
|
.has_one(name, options = {}) ⇒ Object
59
60
61
62
63
64
|
# File 'lib/unimatrix/resource.rb', line 59
def has_one( name, options = {} )
define_method name do
associations = self.instance_variable_get( "@_#{ name.to_s.pluralize }" )
associations.present? ? associations.first : options[ :default ]
end
end
|
.inherited(subclass) ⇒ Object
7
8
9
10
11
|
# File 'lib/unimatrix/resource.rb', line 7
def inherited( subclass )
@@descendants_by_type_name = nil
subclass.nested_fields = {}.merge( self.nested_fields )
subclass.fields = {}.merge( self.fields )
end
|
.type_name ⇒ Object
13
14
15
|
# File 'lib/unimatrix/resource.rb', line 13
def type_name
name.present? ? name.split( '::' ).last.underscore : nil
end
|