Class: CTioga2::Graphics::Styles::BasicStyle

Inherits:
Object
  • Object
show all
Defined in:
lib/ctioga2/graphics/styles/base.rb

Overview

This style is the base class of a series of style objects that share one common feature: all their attributes can be set using the set_from_hash function.

Constant Summary collapse

OldAttrAccessor =
method(:attr_accessor)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.attr_accessor(symbol) ⇒ Object

This redefinition of attr_accessor allows to track for the names of the attributes, while still showing them up properly documented in rdoc.



36
37
38
39
40
41
42
43
44
# File 'lib/ctioga2/graphics/styles/base.rb', line 36

def self.attr_accessor(symbol)
  @attributes ||= []
  @attributes << symbol
  OldAttrAccessor.call(symbol)
  # cl = caller()
  # if not cl[0] =~ /typed_attribute/
  #   puts "old-style attribute: #{cl[0]}"
  # end
end

.attributesObject

Returns the list of attributes.



47
48
49
50
51
52
53
54
# File 'lib/ctioga2/graphics/styles/base.rb', line 47

def self.attributes
  return ( @attributes || [] ) + 
    if superclass.respond_to?(:attributes)
      superclass.attributes
    else
      []
    end
end

.from_hash(hash, name = "%s") ⇒ Object

Creates a new object from a hash specification, just as in #set_from_hash.



169
170
171
172
173
# File 'lib/ctioga2/graphics/styles/base.rb', line 169

def self.from_hash(hash, name = "%s")
  obj = self.new
  obj.set_from_hash(hash, name)
  return obj
end

.options_hash(key = "%s") ⇒ Object

Returns a hash suitable for using as an options hash.

key provides tuning of the key names.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/ctioga2/graphics/styles/base.rb', line 95

def self.options_hash(key = "%s")
  ret = if superclass.respond_to?(:options_hash)
          superclass.options_hash(key)
        else
          {}
        end

  if @attribute_types   # Not always present
    for k, v in @attribute_types
      ret[key % k] = v
    end
  end
    
  if @sub_styles        # Not always present too
    for sub in @sub_styles
      sym, cls, fmt = *sub
      fmt = key % fmt
      ret.merge!(cls.options_hash(fmt))
    end
  end

  return ret
end

.sub_style(symbol, cls, fmt = nil) ⇒ Object

Defines an accessor for an attribute which is a BasicStyle subclass in itself.

format is the thing fed to the subclass for the from_hash function.



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/ctioga2/graphics/styles/base.rb', line 80

def self.sub_style(symbol, cls, fmt = nil)
  @sub_styles ||= []    # A list of [symbol, cls, fmt]
  
  if ! fmt
    fmt = "#{symbol.to_s}_%s"
  end
  
  @sub_styles << [symbol, cls, fmt]
  # Define the accessor
  OldAttrAccessor.call(symbol)
end

.sub_stylesObject



119
120
121
# File 'lib/ctioga2/graphics/styles/base.rb', line 119

def self.sub_styles
  return @sub_styles
end

.typed_attribute(symbol, type) ⇒ Object

TODO:

There may be a reason to make some of the attributes

TODO:

Provide a function to make attributes “aliases” of

This function should be the main way now of declaring attributes, as it allows one to automatically generate an options hash for Command

private to some extent ?

others (but just on the hash side of the things), in order for instance to have halign and valign as aliases for the less intuitive alignment and justification.



67
68
69
70
71
72
73
# File 'lib/ctioga2/graphics/styles/base.rb', line 67

def self.typed_attribute(symbol, type)
  sym = symbol.to_sym
  self.attr_accessor(sym)
  type = CmdArg.new(type) unless type.respond_to? :string_to_type
  @attribute_types ||= {}
  @attribute_types[sym] = type
end

Instance Method Details

#instance_variable_defined?(iv) ⇒ Boolean

Returns:

  • (Boolean)


179
180
181
182
183
184
185
186
# File 'lib/ctioga2/graphics/styles/base.rb', line 179

def instance_variable_defined?(iv)
  a = instance_variables.index(iv)
  if a && a >= 0 
    return true
  else
    return false
  end
end

#set_from_hash(hash, name = "%s") ⇒ Object

TODO:

Maybe there should be a way to detect extra attributes ?

Sets the values of the attributes from the given hash. Keys are looked under the form of

name % key_name

where key_name takes all the values of the attributes.

Unspecified attributes are not removed from the object. Extra keys are silently ignored.

This function returns the number of properties that were effectively set (including those set in sub-styles)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ctioga2/graphics/styles/base.rb', line 137

def set_from_hash(hash, name = "%s")
  nb_set = 0
  for key_name in self.class.attributes
    hash_key = name % key_name
    if hash.key? hash_key 
      self.send("#{key_name}=", hash[hash_key])
      nb_set += 1
    end
  end

  if self.class.sub_styles
    for sub in self.class.sub_styles
      sym, cls, fmt = *sub
      cur_var = self.send(sym)
      if ! cur_var        # Create if not present
        cur_var = cls.new
        set_after = true
      end
      fmt = name % fmt
      nb = cur_var.set_from_hash(hash, fmt)
      if nb > 0 and set_after
        self.send("#{sym}=", cur_var)
      end
      nb_set += nb
    end
  end
  return nb_set
    
end

#to_hash(name = "%s") ⇒ Object

Converts to a hash. Does the reverse of #set_from_hash.



190
191
192
193
194
195
196
197
198
# File 'lib/ctioga2/graphics/styles/base.rb', line 190

def to_hash(name = "%s")
  retval = {}
  for attr in self.class.attributes
    if instance_variable_defined?("@#{attr}")
      retval[name % attr] = instance_variable_get("@#{attr}")
    end
  end
  return retval
end

#update_from_other(other_object) ⇒ Object

Updates information from another object.



201
202
203
# File 'lib/ctioga2/graphics/styles/base.rb', line 201

def update_from_other(other_object)
  set_from_hash(other_object.to_hash)
end