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.
32
33
34
35
36
37
|
# File 'lib/bootic_client/entity.rb', line 32
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
# File 'lib/bootic_client/entity.rb', line 81
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.
30
31
32
|
# File 'lib/bootic_client/entity.rb', line 30
def curies
@curies
end
|
#entities ⇒ Object
Returns the value of attribute entities.
30
31
32
|
# File 'lib/bootic_client/entity.rb', line 30
def entities
@entities
end
|
Class Method Details
.wrap(obj, client: nil, top: nil) ⇒ Object
70
71
72
73
74
75
76
77
78
79
|
# File 'lib/bootic_client/entity.rb', line 70
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
43
44
45
46
|
# File 'lib/bootic_client/entity.rb', line 43
def [](key)
key = key.to_sym
has_property?(key) ? properties[key] : entities[key]
end
|
#can?(rel_name) ⇒ Boolean
52
53
54
|
# File 'lib/bootic_client/entity.rb', line 52
def can?(rel_name)
has_rel? rel_name
end
|
#has?(prop_name) ⇒ Boolean
48
49
50
|
# File 'lib/bootic_client/entity.rb', line 48
def has?(prop_name)
has_property?(prop_name) || has_entity?(prop_name) || has_rel?(prop_name)
end
|
#has_entity?(prop_name) ⇒ Boolean
105
106
107
|
# File 'lib/bootic_client/entity.rb', line 105
def has_entity?(prop_name)
entities.has_key? prop_name.to_sym
end
|
#has_property?(prop_name) ⇒ Boolean
101
102
103
|
# File 'lib/bootic_client/entity.rb', line 101
def has_property?(prop_name)
properties.has_key? prop_name.to_sym
end
|
#has_rel?(prop_name) ⇒ Boolean
109
110
111
|
# File 'lib/bootic_client/entity.rb', line 109
def has_rel?(prop_name)
rels.has_key? prop_name.to_sym
end
|
#inspect ⇒ Object
56
57
58
|
# File 'lib/bootic_client/entity.rb', line 56
def inspect
%(#<#{self.class.name} props: [#{properties.keys.join(', ')}] rels: [#{rels.keys.join(', ')}] entities: [#{entities.keys.join(', ')}]>)
end
|
#links ⇒ Object
66
67
68
|
# File 'lib/bootic_client/entity.rb', line 66
def links
@links ||= attrs.fetch('_links', {})
end
|
#properties ⇒ Object
60
61
62
63
64
|
# File 'lib/bootic_client/entity.rb', line 60
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
# File 'lib/bootic_client/entity.rb', line 113
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
97
98
99
|
# File 'lib/bootic_client/entity.rb', line 97
def respond_to_missing?(method_name, include_private = false)
has?(method_name)
end
|
#to_hash ⇒ Object
39
40
41
|
# File 'lib/bootic_client/entity.rb', line 39
def to_hash
@attrs
end
|