Class: BooticClient::Entity
- Inherits:
-
Object
- Object
- BooticClient::Entity
show all
- Defined in:
- lib/bootic_client/entity.rb
Constant Summary
collapse
- CURIE_EXP =
/(.+):(.+)/.freeze
- CURIES_REL =
'curies'.freeze
- SPECIAL_PROP_EXP =
/^_.+/.freeze
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(attrs, client, top: self) ⇒ Entity
Returns a new instance of Entity.
34
35
36
37
38
39
|
# File 'lib/bootic_client/entity.rb', line 34
def initialize(attrs, client, top: self)
@attrs = attrs.kind_of?(Hash) ? attrs : {}
@client, @top = client, top
build!
self.extend EnumerableEntity if iterable?
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/bootic_client/entity.rb', line 83
def method_missing(name, *args, &block)
if !block_given?
if has_property?(name)
self[name]
elsif has_entity?(name)
entities[name]
elsif has_rel?(name)
rels[name].run(*args)
else
super
end
else
super
end
end
|
Instance Attribute Details
#curies ⇒ Object
Returns the value of attribute curies.
32
33
34
|
# File 'lib/bootic_client/entity.rb', line 32
def curies
@curies
end
|
#entities ⇒ Object
Returns the value of attribute entities.
32
33
34
|
# File 'lib/bootic_client/entity.rb', line 32
def entities
@entities
end
|
Class Method Details
.wrap(obj, client: nil, top: nil) ⇒ Object
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/bootic_client/entity.rb', line 72
def self.wrap(obj, client: nil, top: nil)
case obj
when Hash
new(obj, client, top: top)
when Array
obj.map{|e| wrap(e, client: client, top: top)}
else
obj
end
end
|
Instance Method Details
#[](key) ⇒ Object
45
46
47
48
|
# File 'lib/bootic_client/entity.rb', line 45
def [](key)
key = key.to_sym
has_property?(key) ? properties[key] : entities[key]
end
|
#can?(rel_name) ⇒ Boolean
54
55
56
|
# File 'lib/bootic_client/entity.rb', line 54
def can?(rel_name)
has_rel? rel_name
end
|
#has?(prop_name) ⇒ Boolean
50
51
52
|
# File 'lib/bootic_client/entity.rb', line 50
def has?(prop_name)
has_property?(prop_name) || has_entity?(prop_name) || has_rel?(prop_name)
end
|
#has_entity?(prop_name) ⇒ Boolean
107
108
109
|
# File 'lib/bootic_client/entity.rb', line 107
def has_entity?(prop_name)
entities.has_key? prop_name.to_sym
end
|
#has_property?(prop_name) ⇒ Boolean
103
104
105
|
# File 'lib/bootic_client/entity.rb', line 103
def has_property?(prop_name)
properties.has_key? prop_name.to_sym
end
|
#has_rel?(prop_name) ⇒ Boolean
111
112
113
|
# File 'lib/bootic_client/entity.rb', line 111
def has_rel?(prop_name)
rels.has_key? prop_name.to_sym
end
|
#inspect ⇒ Object
58
59
60
|
# File 'lib/bootic_client/entity.rb', line 58
def inspect
%(#<#{self.class.name} props: [#{properties.keys.join(', ')}] rels: [#{rels.keys.join(', ')}] entities: [#{entities.keys.join(', ')}]>)
end
|
#links ⇒ Object
68
69
70
|
# File 'lib/bootic_client/entity.rb', line 68
def links
@links ||= attrs.fetch('_links', {})
end
|
#properties ⇒ Object
62
63
64
65
66
|
# File 'lib/bootic_client/entity.rb', line 62
def properties
@properties ||= attrs.select{|k,v| !(k =~ SPECIAL_PROP_EXP)}.each_with_object({}) do |(k,v),memo|
memo[k.to_sym] = Entity.wrap(v, client: client, top: top)
end
end
|
#rels ⇒ Object
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
|
# File 'lib/bootic_client/entity.rb', line 115
def rels
@rels ||= (
links = attrs.fetch('_links', {})
links.each_with_object({}) do |(rel,rel_attrs),memo|
if rel =~ CURIE_EXP
_, curie_namespace, rel = rel.split(CURIE_EXP)
if curie = curies.find{|c| c['name'] == curie_namespace}
rel_attrs['docs'] = Relation.expand(curie['href'], rel: rel)
end
end
if rel != CURIES_REL
rel_attrs['name'] = rel
memo[rel.to_sym] = Relation.new(rel_attrs, client)
end
end
)
end
|
#respond_to_missing?(method_name, include_private = false) ⇒ Boolean
99
100
101
|
# File 'lib/bootic_client/entity.rb', line 99
def respond_to_missing?(method_name, include_private = false)
has?(method_name)
end
|
#to_hash ⇒ Object
41
42
43
|
# File 'lib/bootic_client/entity.rb', line 41
def to_hash
@attrs
end
|