module Plotrb::Base
Some internal methods for mixin
Public Class Methods
included(base)
click to toggle source
# File lib/plotrb/base.rb, line 6 def self.included(base) base.extend(ClassMethods) end
Public Instance Methods
add_attributes(*args)
click to toggle source
add new attributes to the instance @param args [Array<Symbol>] the attributes to add to the instance
# File lib/plotrb/base.rb, line 49 def add_attributes(*args) self.singleton_class.add_attributes(*args) end
attribute_post_processing()
click to toggle source
to be implemented in each Plotrb
class
# File lib/plotrb/base.rb, line 59 def attribute_post_processing raise NotImplementedError end
attributes()
click to toggle source
@return [Array<Symbol>] attributes of the particular instance combined
with attributes of the class
# File lib/plotrb/base.rb, line 31 def attributes singleton_attr = self.singleton_class.attributes || [] class_attr = self.class.attributes || [] singleton_attr.concat(class_attr).uniq end
classify(name, format=nil)
click to toggle source
# File lib/plotrb/base.rb, line 143 def classify(name, format=nil) klass = name.to_s.split('_').collect(&:capitalize).join if format == :json klass[0].downcase + klass[1..-1] else klass end end
collect_attributes()
click to toggle source
@return [Hash] recursively construct a massive hash
# File lib/plotrb/base.rb, line 64 def collect_attributes attribute_post_processing collected = {} defined_attributes.each do |attr| value = self.instance_variable_get("@#{attr}") # change snake_case attributes to camelCase used in Vega's JSON spec json_attr = classify(attr, :json) if value.respond_to?(:collect_attributes) collected[json_attr] = value.collect_attributes elsif value.is_a?(Array) collected[json_attr] = [].concat(value.collect{ |v| v.respond_to?(:collect_attributes) ? v.collect_attributes : v }) else collected[json_attr] = value end end collected end
define_boolean_attribute(method)
click to toggle source
# File lib/plotrb/base.rb, line 84 def define_boolean_attribute(method) # when setting boolean values, eg. foo.bar sets bar attribute to true, # foo.bar? returns state of bar attribute define_singleton_method(method) do |&block| self.instance_variable_set("@#{method}", true) self.instance_eval(&block) if block self end define_singleton_method("#{method}?") do self.instance_variable_get("@#{method}") end end
define_boolean_attributes(*methods)
click to toggle source
# File lib/plotrb/base.rb, line 97 def define_boolean_attributes(*methods) methods.each { |m| define_boolean_attribute(m) } end
define_multi_val_attribute(method, proc=nil)
click to toggle source
# File lib/plotrb/base.rb, line 123 def define_multi_val_attribute(method, proc=nil) # when multiple values are allowed, eg. foo.bar(1,2) or foo.bar([1,2]) # proc is passed in to process values before assigning to attributes define_singleton_method(method) do |*args, &block| case args.size when 0 self.instance_variable_get("@#{method}") else vals = proc.is_a?(Proc) ? proc.call(*args) : [args].flatten self.instance_variable_set("@#{method}", vals) self.instance_eval(&block) if block self end end end
define_multi_val_attributes(*methods)
click to toggle source
# File lib/plotrb/base.rb, line 139 def define_multi_val_attributes(*methods) methods.each { |m| define_multi_val_attribute(m) } end
define_single_val_attribute(method, proc=nil)
click to toggle source
# File lib/plotrb/base.rb, line 101 def define_single_val_attribute(method, proc=nil) # when only single value is allowed, eg. foo.bar(1) # proc is passed in to process value before assigning to attributes define_singleton_method(method) do |*args, &block| case args.size when 0 self.instance_variable_get("@#{method}") when 1 val = proc.is_a?(Proc) ? proc.call(args[0]) : args[0] self.instance_variable_set("@#{method}", val) self.instance_eval(&block) if block self else raise ArgumentError end end end
define_single_val_attributes(*methods)
click to toggle source
# File lib/plotrb/base.rb, line 119 def define_single_val_attributes(*methods) methods.each { |m| define_single_val_attribute(m) } end
defined_attributes()
click to toggle source
@return [Array<Symbol>] attributes that have values
# File lib/plotrb/base.rb, line 54 def defined_attributes attributes.reject { |attr| self.instance_variable_get("@#{attr}").nil? } end
set_attributes(args)
click to toggle source
add and set new attributes and values to the instance @param args [Hash] attributes in the form of a Hash
# File lib/plotrb/base.rb, line 39 def set_attributes(args) args.each do |k, v| # use singleton_class as attributes are instance-specific self.singleton_class.add_attributes(k) self.instance_variable_set("@#{k}", v) unless v.nil? end end