class Battlerite::Model
Attributes
blob[R]
id[R]
relationships[R]
Public Class Methods
alias_attribute(new, old)
click to toggle source
# File lib/battlerite/model.rb, line 11 def self.alias_attribute new, old @aliases ||= {} @aliases[new] = old end
aliases()
click to toggle source
# File lib/battlerite/model.rb, line 16 def self.aliases @aliases ||= {} end
enable_dynamic_dig(enabled)
click to toggle source
# File lib/battlerite/model.rb, line 7 def self.enable_dynamic_dig enabled @@dig = enabled end
new(params)
click to toggle source
# File lib/battlerite/model.rb, line 22 def initialize params @relationships = params.delete :relationships @blob = params.delete :blob params.each { |k, v| self.instance_variable_set :"@#{k}", v } @relationships.each { |k, v| self.define_singleton_method(k.to_sym) { v } } self.class.aliases.each { |new, old| self.define_singleton_method(new.to_sym) { |*args, &block| self.public_send(old, *args, &block) } } end
Public Instance Methods
dig(method, *args, &block)
click to toggle source
# File lib/battlerite/model.rb, line 57 def dig(method, *args, &block) dig_path(method).map { |v| v.public_send(method, *args, &block) }.flatten end
dig_path(method)
click to toggle source
# File lib/battlerite/model.rb, line 51 def dig_path(method) return [self] if @relationships.keys.include?(method) return [self] if self.class.aliases.keys.include?(method) return @relationships.values.map { |v| v.map { |i| i.dig_path(method) } }.compact.flatten end
method(method)
click to toggle source
Calls superclass method
# File lib/battlerite/model.rb, line 43 def method(method) if !dig_path(method).empty? && @@dig super(:dig) else super(method) end end
method_missing(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/battlerite/model.rb, line 33 def method_missing method, *args, &block return dig(method, *args, &block) unless dig_path(method).empty? || @@dig == false super(method, *args, &block) end
respond_to?(method, *args, &block)
click to toggle source
Calls superclass method
# File lib/battlerite/model.rb, line 38 def respond_to? method, *args, &block return !dig_path(method).empty? || super(method, *args, &block) if @@dig return super(method, *args, &block) end
to_s()
click to toggle source
# File lib/battlerite/model.rb, line 61 def to_s "#{self.class.name.split("::").last}: #{id[0,5]}..." end
to_s_tree()
click to toggle source
# File lib/battlerite/model.rb, line 65 def to_s_tree str = "#{to_s}" relationships.each { |k, v| v.each { |d| str += "\n├────#{d.to_s_tree.gsub("\n", "\n│ ")}" } } if respond_to? :relationships str end