Class: Ziya::Components::Base

Inherits:
Object
  • Object
show all
Includes:
Utils::Text
Defined in:
lib/ziya/components/base.rb

Overview

:nodoc:

Class Method Summary collapse

Instance Method Summary collapse

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

.attributesObject


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.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



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…

Returns:

  • (Boolean)


94
95
96
# File 'lib/ziya/components/base.rb', line 94

def configured?
  !options.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}( #{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



100
101
102
103
104
105
# File 'lib/ziya/components/base.rb', line 100

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



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

#optionsObject


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 options
  options = {}
  attributes_for(self).each do |p|
    option = self.send(p.to_sym)   
    options[p] = option unless option.nil?
  end
  options
end

#options_as_stringObject


Turns options hash into string representation



120
121
122
123
124
125
126
127
128
# File 'lib/ziya/components/base.rb', line 120

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.nil? and !value.is_a? YAML::Omap
  end      
  buff.join( "," )
end