Class: Ziya::Charts::Support::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/ziya/charts/support/base.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base


initializes component from hash



41
42
43
44
45
# File 'lib/ziya/charts/support/base.rb', line 41

def initialize( opts={} )
  opts.each_pair do |k,v|
    self.send( "#{k}=", v )
  end
end

Class Method Details

.attributesObject


Class accessor. Retrieve class level preferences



34
35
36
# File 'lib/ziya/charts/support/base.rb', line 34

def attributes # :nodoc:
  @attributes ||= {}
end

.has_attribute(*args) ⇒ Object


defines attribute accessors for a given component



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ziya/charts/support/base.rb', line 19

def has_attribute(*args) # :nodoc:
  class_name = self.to_s
  args.each do |attribute|          
    # add the attribute to the collection making sure to create a new array if one doesn't exist
    attributes[class_name] = [] if attributes[class_name].nil?
    attributes[class_name] << attribute
    # create the accessor methods for the attribute
    unless self.instance_methods.include?(attribute.to_s) && self.instance_methods.include?("#{attribute.to_s}=")
      self.module_eval "attr_accessor :#{attribute}"
    end
  end
end

Instance Method Details

#==(other) ⇒ Object



47
48
49
50
51
# File 'lib/ziya/charts/support/base.rb', line 47

def ==( other )
  self.options.each_pair do |k,v|
    return false unless other.send( k ) == v
  end
end

#attributes_for(an_instance) ⇒ Object


fetch attributes for a give component



120
121
122
123
124
# File 'lib/ziya/charts/support/base.rb', line 120

def attributes_for( an_instance )
  attrs = self.class.attributes[an_instance.class.name] 
  raise "Unable to get attributes for #{an_instance}" unless attrs
  attrs
end

#configured?Boolean


checks if a give component properties have been set. return true if one or more props have been set. False otherwise…

Returns:

  • (Boolean)


82
83
84
# File 'lib/ziya/charts/support/base.rb', line 82

def configured?
  !options.empty?
end

#flatten(xml) ⇒ Object


Flattens out this component to xml



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ziya/charts/support/base.rb', line 65

def flatten( xml )
  hash = has_sub_components
  pref = self.class.name.ziya_demodulize.ziya_underscore
  if hash and ! hash.empty?     
    self.class.module_eval <<-XML
     xml.#{pref}( #{options_as_string} ) do
      hash.each{ |k,v| v.flatten( xml ) }
     end
    XML
  else
    self.class.module_eval "xml.#{pref}( #{options_as_string} )"
  end
end

#has_sub_componentsObject


Checks if one of the options is an array



88
89
90
91
92
93
# File 'lib/ziya/charts/support/base.rb', line 88

def has_sub_components
  options.each_pair do |k, v|
    return v if v.is_a? YAML::Omap
  end
  nil
end

#merge(parent_attributes, force = false) ⇒ Object


merge attributes with overriden component



55
56
57
58
59
60
61
# File 'lib/ziya/charts/support/base.rb', line 55

def merge( parent_attributes, force=false )
  attributes_for(self).each do |attr|
    unless parent_attributes.send(attr).nil?
      send("#{attr}=", parent_attributes.send(attr)) 
    end
  end
end

#optionsObject


calls all attribute methods and gather the various props into a hash



97
98
99
100
101
102
103
104
# File 'lib/ziya/charts/support/base.rb', line 97

def options
  options = {}
  attributes_for(self).each do |p|
    option = self.send(p.to_sym)   
    options[p] = option if option
  end
  options
end

#options_as_stringObject


Turns options hash into string representation



108
109
110
111
112
113
114
115
116
# File 'lib/ziya/charts/support/base.rb', line 108

def options_as_string
  buff = []
  opts = options
  opts.keys.sort{ |a,b| a.to_s <=> b.to_s }.each do |k|
    value = opts[k]
    buff << sprintf( ":%s => '%s'", k, value.to_s ) if value and !value.is_a? YAML::Omap
  end      
  buff.join( "," )
end