Module: Welo::Resource::ClassMethods
- Defined in:
- lib/welo/core/resource.rb
Instance Method Summary collapse
-
#base_path(val = nil) ⇒ Object
If there is one argument, sets the base_path, otherwise returns the base_path, if not set, will fallback to the default_base_path_name.
-
#default_base_path_name ⇒ Object
Returns the downcased last part of the ruby name.
- #epithet(sym, vals = nil) ⇒ Object
-
#epithets(label, prefix = '') ⇒ Object
Returns array of formatted strings from the epithets fields.
- #epithets_hash ⇒ Object
-
#identifiers(ident, prefix = '') ⇒ Object
Returns array of formatted strings from the identification fields.
-
#identifiers_hash ⇒ Object
An hash to log the identifiers, by default, uses [:uuid].
-
#identify(sym, vals = nil) ⇒ Object
If there is no argument, returns the identification fields (defaults to [:uuid]).
-
#inherited(klass) ⇒ Object
A hook to duplicates * relationships * perspectives * identification * base_path Such that the subclass looks like the parent one.
-
#nesting(resource_sym, identifier_sym = nil) ⇒ Object
If one argument, returns the nesting for the given resource_sym If two arguments: creates a nesting of a resource in this resource.
-
#nestings ⇒ Object
The hash of nestings of other resources.
-
#path_model(ident, prefix = '') ⇒ Object
Returns the path model for items of this resource, including an optional prefix e.g.
-
#perspective(name, syms = nil) ⇒ Object
If one argument, returns the perspective for the given name, or the :default one if it exists If more than one argument, registers a new perspective.
-
#perspectives ⇒ Object
The hash of perspectives for this resource.
-
#relationship(sym, klass = nil, kinds = []) ⇒ Object
If one argument, returns the relationship with the given name If more than one argument, registers a new relationship, possibly overwriting it.
-
#relationships ⇒ Object
The hash of relationships with other resources.
-
#structure(name) ⇒ Object
Returns the structure associated to the selected perspective the structure replace the perspective’s fields by relationships whenever it’s possible.
Instance Method Details
#base_path(val = nil) ⇒ Object
If there is one argument, sets the base_path, otherwise returns the base_path, if not set, will fallback to the default_base_path_name
96 97 98 99 100 101 102 |
# File 'lib/welo/core/resource.rb', line 96 def base_path(val=nil) if val @base_path = val else @base_path || default_base_path_name end end |
#default_base_path_name ⇒ Object
Returns the downcased last part of the ruby name
89 90 91 |
# File 'lib/welo/core/resource.rb', line 89 def default_base_path_name self.name.split('::').last.downcase end |
#epithet(sym, vals = nil) ⇒ Object
137 138 139 140 141 142 143 |
# File 'lib/welo/core/resource.rb', line 137 def epithet(sym, vals=nil) if vals.nil? epithets_hash[sym] else epithets_hash[sym] = vals end end |
#epithets(label, prefix = '') ⇒ Object
Returns array of formatted strings from the epithets fields. a prefix may be appended before the epithets field e.g. ‘foo’ (no prefix) => ‘foo’ ‘foo’ (prefix:‘bla.’) => ‘bla.foo’
150 151 152 |
# File 'lib/welo/core/resource.rb', line 150 def epithets(label, prefix='') epithet(label).map{|sym| "#{prefix}#{sym}"} end |
#epithets_hash ⇒ Object
133 134 135 |
# File 'lib/welo/core/resource.rb', line 133 def epithets_hash @epithets_hash ||= {} end |
#identifiers(ident, prefix = '') ⇒ Object
Returns array of formatted strings from the identification fields. a prefix may be appended before the identification field e.g. ‘foo’ (no prefix) => ‘foo’ ‘foo’ (prefix:‘bla.’) => ‘bla.foo’
129 130 131 |
# File 'lib/welo/core/resource.rb', line 129 def identifiers(ident, prefix='') identify(ident).map{|sym| "#{prefix}#{sym}"} end |
#identifiers_hash ⇒ Object
An hash to log the identifiers, by default, uses [:uuid]
105 106 107 |
# File 'lib/welo/core/resource.rb', line 105 def identifiers_hash @identifiers_hash ||= {:default => [:uuid]} end |
#identify(sym, vals = nil) ⇒ Object
If there is no argument, returns the identification fields (defaults to [:uuid]). Otherwise, sets the identification fields. Fields are symbols of methods that will be sent to the instances. Since they may be used to build an URL, identifying fields must respond to :to_s. See Resource#identifying_path_part
116 117 118 119 120 121 122 |
# File 'lib/welo/core/resource.rb', line 116 def identify(sym, vals=nil) if vals.nil? identifiers_hash[sym] else identifiers_hash[sym] = vals end end |
#inherited(klass) ⇒ Object
A hook to duplicates
-
relationships
-
perspectives
-
identification
-
base_path
Such that the subclass looks like the parent one
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/welo/core/resource.rb', line 25 def inherited(klass) relationships.each_pair do |k,v| klass.relationships[k] = v end nestings.each_pair do |k,v| klass.nestings[k] = v end perspectives.each_pair do |k,v| klass.perspectives[k] = v end identifiers_hash.each_pair do |k,v| klass.identifiers_hash[k] = v end klass.base_path base_path end |
#nesting(resource_sym, identifier_sym = nil) ⇒ Object
If one argument, returns the nesting for the given resource_sym If two arguments: creates a nesting of a resource in this resource
80 81 82 83 84 85 86 |
# File 'lib/welo/core/resource.rb', line 80 def nesting(resource_sym, identifier_sym=nil) if identifier_sym.nil? nestings[resource_sym] else nestings[resource_sym] = Nesting.new(resource_sym, identifier_sym) end end |
#nestings ⇒ Object
The hash of nestings of other resources
47 48 49 |
# File 'lib/welo/core/resource.rb', line 47 def nestings @nestings ||= {} end |
#path_model(ident, prefix = '') ⇒ Object
Returns the path model for items of this resource, including an optional prefix e.g. class Foo
base_path 'foo'
identify :default, :bar
end
Foo.path_model(ident, 'pfx.')
#=> 'foo/:pfx.bar'
163 164 165 |
# File 'lib/welo/core/resource.rb', line 163 def path_model(ident, prefix='') File.join(base_path, identifiers(ident, ':' + prefix)) end |
#perspective(name, syms = nil) ⇒ Object
If one argument, returns the perspective for the given name, or the :default one if it exists If more than one argument, registers a new perspective
70 71 72 73 74 75 76 |
# File 'lib/welo/core/resource.rb', line 70 def perspective(name, syms=nil) if syms.nil? perspectives[name] || perspectives[:default] else perspectives[name] = Perspective.new(name, syms) end end |
#perspectives ⇒ Object
The hash of perspectives for this resource.
63 64 65 |
# File 'lib/welo/core/resource.rb', line 63 def perspectives @perspectives ||= {} end |
#relationship(sym, klass = nil, kinds = []) ⇒ Object
If one argument, returns the relationship with the given name If more than one argument, registers a new relationship, possibly overwriting it.
54 55 56 57 58 59 60 |
# File 'lib/welo/core/resource.rb', line 54 def relationship(sym, klass=nil, kinds=[]) if klass relationships[sym] = Relationship.new(sym, klass, kinds) else relationships[sym] end end |
#relationships ⇒ Object
The hash of relationships with other resources
42 43 44 |
# File 'lib/welo/core/resource.rb', line 42 def relationships @relationships ||= {} end |
#structure(name) ⇒ Object
Returns the structure associated to the selected perspective the structure replace the perspective’s fields by relationships whenever it’s possible
170 171 172 173 174 175 176 177 178 |
# File 'lib/welo/core/resource.rb', line 170 def structure(name) persp = perspective(name) raise ArgumentError.new("no such perspective: #{name} for #{self}") unless persp fields = persp.fields fields.map do |field| rel = relationship(field) (rel || field) end end |