Class: Ziya::Components::Base
- Inherits:
-
Object
- Object
- Ziya::Components::Base
- Includes:
- Utils::Text
- Defined in:
- lib/ziya/components/base.rb
Overview
:nodoc:
Direct Known Subclasses
Area, AxisCategory, AxisTicks, AxisValue, Bevel, Blur, ChartBorder, ChartGridH, ChartGridV, ChartGuide, ChartLabel, ChartPref, ChartRect, ChartTransition, Circle, ContextMenu, Draw, DrawBase, Filter, Glow, Image, Legend, Line, Link, LinkData, Rect, Scroll, Series, SeriesColor, SeriesExplode, Shadow, Text, Update, Gauges::Support::Base
Class Method Summary collapse
-
.attributes ⇒ Object
———————————————————————– Class accessor.
-
.has_attribute(*args) ⇒ Object
———————————————————————– defines attribute accessors for a given component.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#attributes_for(an_instance) ⇒ Object
————————————————————————- fetch attributes for a give component.
-
#configured? ⇒ Boolean
————————————————————————- checks if a give component properties have been set.
- #flatten(xml) ⇒ Object
-
#has_sub_components ⇒ Object
————————————————————————- Checks if one of the options is an array.
-
#initialize(opts = {}) ⇒ Base
constructor
————————————————————————- initializes component from hash.
-
#merge(parent_attributes, force = false) ⇒ Object
————————————————————————- merge attributes with overriden component.
-
#options ⇒ Object
————————————————————————- calls all attribute methods and gather the various props into a hash.
-
#options_as_string ⇒ Object
————————————————————————- Turns options hash into string representation.
Methods included from Utils::Text
#camelize, #classify, #demodulize, #underscore
Constructor Details
#initialize(opts = {}) ⇒ Base
initializes component from hash
40 41 42 43 44 |
# File 'lib/ziya/components/base.rb', line 40 def initialize( opts={} ) opts.each_pair do |k,v| self.send( "#{k}=", v ) end end |
Class Method Details
.attributes ⇒ Object
Class accessor. Retrieve class level preferences
33 34 35 |
# File 'lib/ziya/components/base.rb', line 33 def attributes # :nodoc: @attributes ||= {} end |
.has_attribute(*args) ⇒ Object
defines attribute accessors for a given component
18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ziya/components/base.rb', line 18 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
46 47 48 49 50 |
# File 'lib/ziya/components/base.rb', line 46 def ==( other ) self..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
132 133 134 135 136 |
# File 'lib/ziya/components/base.rb', line 132 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…
94 95 96 |
# File 'lib/ziya/components/base.rb', line 94 def configured? !.empty? end |
#flatten(xml) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/ziya/components/base.rb', line 62 def flatten( xml ) hash = has_sub_components clazz = demodulize( self.class.name ) pref = underscore( clazz ) if hash and ! hash.empty? self.class.module_eval <<-XML xml.#{pref}( #{} ) do hash.each{ |k,v| v.flatten( xml ) } end XML else self.class.module_eval "xml.#{pref}( #{} )" end end |
#has_sub_components ⇒ Object
Checks if one of the options is an array
100 101 102 103 104 105 |
# File 'lib/ziya/components/base.rb', line 100 def has_sub_components .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
54 55 56 57 58 59 60 |
# File 'lib/ziya/components/base.rb', line 54 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 |
#options ⇒ Object
calls all attribute methods and gather the various props into a hash
109 110 111 112 113 114 115 116 |
# File 'lib/ziya/components/base.rb', line 109 def = {} attributes_for(self).each do |p| option = self.send(p.to_sym) [p] = option unless option.nil? end end |
#options_as_string ⇒ Object
Turns options hash into string representation
120 121 122 123 124 125 126 127 128 |
# File 'lib/ziya/components/base.rb', line 120 def buff = [] opts = 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.nil? and !value.is_a? YAML::Omap end buff.join( "," ) end |