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.
39
40
41
42
43
44
|
# File 'lib/bootic_client/entity.rb', line 39
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/bootic_client/entity.rb', line 88
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.
37
38
39
|
# File 'lib/bootic_client/entity.rb', line 37
def curies
@curies
end
|
#entities ⇒ Object
Returns the value of attribute entities.
37
38
39
|
# File 'lib/bootic_client/entity.rb', line 37
def entities
@entities
end
|
Class Method Details
.wrap(obj, client: nil, top: nil) ⇒ Object
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/bootic_client/entity.rb', line 77
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
50
51
52
53
|
# File 'lib/bootic_client/entity.rb', line 50
def [](key)
key = key.to_sym
has_property?(key) ? properties[key] : entities[key]
end
|
#can?(rel_name) ⇒ Boolean
59
60
61
|
# File 'lib/bootic_client/entity.rb', line 59
def can?(rel_name)
has_rel? rel_name
end
|
#has?(prop_name) ⇒ Boolean
55
56
57
|
# File 'lib/bootic_client/entity.rb', line 55
def has?(prop_name)
has_property?(prop_name) || has_entity?(prop_name) || has_rel?(prop_name)
end
|
#has_entity?(prop_name) ⇒ Boolean
112
113
114
|
# File 'lib/bootic_client/entity.rb', line 112
def has_entity?(prop_name)
entities.has_key? prop_name.to_sym
end
|
#has_property?(prop_name) ⇒ Boolean
108
109
110
|
# File 'lib/bootic_client/entity.rb', line 108
def has_property?(prop_name)
properties.has_key? prop_name.to_sym
end
|
#has_rel?(prop_name) ⇒ Boolean
116
117
118
|
# File 'lib/bootic_client/entity.rb', line 116
def has_rel?(prop_name)
rels.has_key? prop_name.to_sym
end
|
#inspect ⇒ Object
63
64
65
|
# File 'lib/bootic_client/entity.rb', line 63
def inspect
%(#<#{self.class.name} props: [#{properties.keys.join(', ')}] rels: [#{rels.keys.join(', ')}] entities: [#{entities.keys.join(', ')}]>)
end
|
#links ⇒ Object
73
74
75
|
# File 'lib/bootic_client/entity.rb', line 73
def links
@links ||= attrs.fetch('_links', {})
end
|
#properties ⇒ Object
67
68
69
70
71
|
# File 'lib/bootic_client/entity.rb', line 67
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
# File 'lib/bootic_client/entity.rb', line 120
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
104
105
106
|
# File 'lib/bootic_client/entity.rb', line 104
def respond_to_missing?(method_name, include_private = false)
has?(method_name)
end
|
#to_hash ⇒ Object
46
47
48
|
# File 'lib/bootic_client/entity.rb', line 46
def to_hash
@attrs
end
|