Module: Plotrb::Base

Overview

Some internal methods for mixin

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


6
7
8
# File 'lib/plotrb/base.rb', line 6

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#add_attributes(*args) ⇒ Object

add new attributes to the instance

Parameters:

  • args (Array<Symbol>)

    the attributes to add to the instance


49
50
51
# File 'lib/plotrb/base.rb', line 49

def add_attributes(*args)
  self.singleton_class.add_attributes(*args)
end

#attribute_post_processingObject

to be implemented in each Plotrb class

Raises:

  • (NotImplementedError)

59
60
61
# File 'lib/plotrb/base.rb', line 59

def attribute_post_processing
  raise NotImplementedError
end

#attributesArray<Symbol>

Returns attributes of the particular instance combined with attributes of the class.

Returns:

  • (Array<Symbol>)

    attributes of the particular instance combined with attributes of the class


31
32
33
34
35
# 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) ⇒ Object


143
144
145
146
147
148
149
150
# 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_attributesHash

Returns recursively construct a massive hash.

Returns:

  • (Hash)

    recursively construct a massive hash


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# 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) ⇒ Object


84
85
86
87
88
89
90
91
92
93
94
95
# 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) ⇒ Object


97
98
99
# 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) ⇒ Object


123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# 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) ⇒ Object


139
140
141
# 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) ⇒ Object


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# 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) ⇒ Object


119
120
121
# File 'lib/plotrb/base.rb', line 119

def define_single_val_attributes(*methods)
  methods.each { |m| define_single_val_attribute(m) }
end

#defined_attributesArray<Symbol>

Returns attributes that have values.

Returns:

  • (Array<Symbol>)

    attributes that have values


54
55
56
# File 'lib/plotrb/base.rb', line 54

def defined_attributes
  attributes.reject { |attr| self.instance_variable_get("@#{attr}").nil? }
end

#set_attributes(args) ⇒ Object

add and set new attributes and values to the instance

Parameters:

  • args (Hash)

    attributes in the form of a Hash


39
40
41
42
43
44
45
# 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