Class: Pione::Global::Item
- Inherits:
-
Object
- Object
- Pione::Global::Item
- Defined in:
- lib/pione/global/item.rb
Instance Attribute Summary collapse
-
#dependencies ⇒ Object
readonly
item names that the item depends.
-
#desc ⇒ Object
item description.
-
#init ⇒ Object
initial value.
-
#name ⇒ Object
readonly
item name.
-
#orig ⇒ Object
readonly
original value.
-
#record ⇒ Object
record target.
-
#type ⇒ Object
type of item value.
-
#updater ⇒ Object
readonly
update process object.
-
#value ⇒ Object
readonly
item value.
Instance Method Summary collapse
-
#configurable? ⇒ Boolean
Return true if the item is configurable.
-
#define_updater(&b) ⇒ Object
Define updater process of the item.
-
#initialize(name, configurable, option = {}) ⇒ Item
constructor
A new instance of Item.
- #post(&b) ⇒ Object
-
#register ⇒ Object
Register the item to global system.
-
#unregister ⇒ Object
Unregister the item from global system.
-
#update(val) ⇒ Object
Update the item with the value.
Constructor Details
#initialize(name, configurable, option = {}) ⇒ Item
Returns a new instance of Item.
97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/pione/global/item.rb', line 97 def initialize(name, configurable, option={}) @name = name @value = nil @configurable = configurable @dependencies = option[:dependencies] || [] @desc = nil @init = nil @type = nil @updater = Proc.new {|val| val} @post_action = nil @orig = nil @record = false end |
Instance Attribute Details
#dependencies ⇒ Object (readonly)
item names that the item depends
77 78 79 |
# File 'lib/pione/global/item.rb', line 77 def dependencies @dependencies end |
#desc ⇒ Object
item description
80 81 82 |
# File 'lib/pione/global/item.rb', line 80 def desc @desc end |
#init ⇒ Object
initial value
83 84 85 |
# File 'lib/pione/global/item.rb', line 83 def init @init end |
#name ⇒ Object (readonly)
item name
71 72 73 |
# File 'lib/pione/global/item.rb', line 71 def name @name end |
#orig ⇒ Object (readonly)
original value
92 93 94 |
# File 'lib/pione/global/item.rb', line 92 def orig @orig end |
#record ⇒ Object
record target
95 96 97 |
# File 'lib/pione/global/item.rb', line 95 def record @record end |
#type ⇒ Object
type of item value
86 87 88 |
# File 'lib/pione/global/item.rb', line 86 def type @type end |
#updater ⇒ Object (readonly)
update process object
89 90 91 |
# File 'lib/pione/global/item.rb', line 89 def updater @updater end |
#value ⇒ Object (readonly)
item value
74 75 76 |
# File 'lib/pione/global/item.rb', line 74 def value @value end |
Instance Method Details
#configurable? ⇒ Boolean
Return true if the item is configurable.
112 113 114 |
# File 'lib/pione/global/item.rb', line 112 def configurable? @configurable end |
#define_updater(&b) ⇒ Object
Define updater process of the item.
158 159 160 |
# File 'lib/pione/global/item.rb', line 158 def define_updater(&b) @updater = b end |
#post(&b) ⇒ Object
162 163 164 |
# File 'lib/pione/global/item.rb', line 162 def post(&b) @post_action = b end |
#register ⇒ Object
Register the item to global system.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'lib/pione/global/item.rb', line 117 def register # register this item Global.item[@name] = self # build dependency table for computed items if @dependencies @dependencies.each do |dependency| Global.__dependencies__[dependency] << self end end # set reader and writer name = @name Global.singleton_class.module_eval do |mod| define_method(name) {get(name)} define_method("set_%s" % name) {|val| set(name, val)} define_method("%s=" % name) {|val| set(name, val)} end end |
#unregister ⇒ Object
Unregister the item from global system.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/pione/global/item.rb', line 138 def unregister Global.item.delete(@name) # remove dependency if @dependencies @dependencies.each do |dependency| Global.__dependencies__.delete(dependency) end end # remove accessors name = @name Global.singleton_class.module_eval do |mod| remove_method(name) remove_method("set_%s" % name) remove_method("%s=" % name) end end |
#update(val) ⇒ Object
Update the item with the value.
167 168 169 170 171 172 173 |
# File 'lib/pione/global/item.rb', line 167 def update(val) @orig = val @value = @updater.call(ValueConverter.convert(@type, val)) if @post_action @post_action.call(@value) end end |