Class: Metro::Model::Property
- Inherits:
-
Object
- Object
- Metro::Model::Property
- Includes:
- Units
- Defined in:
- lib/metro/models/properties/property.rb
Overview
Property is intented to be subclassed to provide filtering of particular types.
A property is a value filter that allows you to specify filtering options both on the setting and the getting of a value. The property itself does not maintain any of these values but keeps in mind the particular model and options that it is created when applying the filtering.
Direct Known Subclasses
AnimationProperty, ArrayProperty, BooleanProperty, ColorProperty, DimensionsProperty, FontProperty, ImageProperty, ModelProperty, NumericProperty, OptionsProperty, PositionProperty, SampleProperty, ScaleProperty, SongProperty, TextProperty
Constant Summary
Constants included from Units
Instance Attribute Summary collapse
-
#block ⇒ Object
readonly
Returns the value of attribute block.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.define_property(name, options = {}) ⇒ Object
Allow a property to define a sub-property.
-
.defined_properties ⇒ Object
An array of all the defined properties.
-
.get(*types, &block) ⇒ Object
Define a filter block for getting a value of that type.
-
.get_or_set(type = NilClass, &block) ⇒ Object
Define a filter block that applies to both the setting and getting of a property.
-
.gets ⇒ Object
All get filter blocks defined.
-
.hash_with_default_to_nil ⇒ Object
Generate a hash that will default all missing keys to the NilClass key and the NilClass key will return a cource of action that will raise an exception.
-
.inherited(subclass) ⇒ Object
Capture all the subclassed properties and add them to the availble list of properties.
- .properties ⇒ Object
-
.properties_hash ⇒ Object
Generate a properties hash.
-
.property(name) ⇒ Object
A Property Class that matches the specified name.
-
.set(*types, &block) ⇒ Object
Define a filter block for setting a value of the type.
-
.sets ⇒ Object
All set filter blocks defined.
Instance Method Summary collapse
-
#get(value) ⇒ Object
Perform a get of the value, running it through the get filter block that matches the type.
-
#initialize(model, options = {}, &block) ⇒ Property
constructor
A new instance of Property.
-
#set(value) ⇒ Object
Perform a set of the value, running it through the set filter block that matches the type.
Constructor Details
#initialize(model, options = {}, &block) ⇒ Property
Returns a new instance of Property.
23 24 25 26 27 |
# File 'lib/metro/models/properties/property.rb', line 23 def initialize(model,={},&block) @model = model @options = @block = block end |
Instance Attribute Details
#block ⇒ Object (readonly)
Returns the value of attribute block.
15 16 17 |
# File 'lib/metro/models/properties/property.rb', line 15 def block @block end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
15 16 17 |
# File 'lib/metro/models/properties/property.rb', line 15 def model @model end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
15 16 17 |
# File 'lib/metro/models/properties/property.rb', line 15 def @options end |
Class Method Details
.define_property(name, options = {}) ⇒ Object
Allow a property to define a sub-property.
A sub-property that is defined can be any type of exiting properties. Which may also define more sub-properties. These sub-properties will be available as properties through the names provided.
The sub-properties can also be prefixed with the parent property name:
If you define a property with the non-default name it will automatically add the prefix to all the sub-properties. This is to prevent any getter/setter method name collisions.
134 135 136 |
# File 'lib/metro/models/properties/property.rb', line 134 def self.define_property(name, = {}) defined_properties.push PropertyDefinition.new name, end |
.defined_properties ⇒ Object
Returns an array of all the defined properties.
141 142 143 |
# File 'lib/metro/models/properties/property.rb', line 141 def self.defined_properties @defined_properties ||= [] end |
.get(*types, &block) ⇒ Object
Define a filter block for getting a value of that type.
30 31 32 33 34 35 |
# File 'lib/metro/models/properties/property.rb', line 30 def self.get(*types,&block) types = [ NilClass ] if types.empty? types.each do |type| gets[type.to_s] = block end end |
.get_or_set(type = NilClass, &block) ⇒ Object
Define a filter block that applies to both the setting and getting of a property.
70 71 72 73 |
# File 'lib/metro/models/properties/property.rb', line 70 def self.get_or_set(type=NilClass,&block) gets[type.to_s] = block sets[type.to_s] = block end |
.gets ⇒ Object
All get filter blocks defined
38 39 40 |
# File 'lib/metro/models/properties/property.rb', line 38 def self.gets @gets ||= hash_with_default_to_nil end |
.hash_with_default_to_nil ⇒ Object
Generate a hash that will default all missing keys to the NilClass key and the NilClass key will return a cource of action that will raise an exception. This means by default that if it is not overridden an error is generated.
179 180 181 182 183 |
# File 'lib/metro/models/properties/property.rb', line 179 def self.hash_with_default_to_nil hash = HashWithIndifferentAccess.new { |hash,key| hash["NilClass"] } hash["NilClass"] = lambda { |value| raise "#{self} is not able to translate the #{value} (#{value.class})" } hash end |
.inherited(subclass) ⇒ Object
Capture all the subclassed properties and add them to the availble list of properties.
149 150 151 152 |
# File 'lib/metro/models/properties/property.rb', line 149 def self.inherited(subclass) property_name = subclass.to_s.gsub(/Property$/,'').split("::").last.underscore properties_hash[property_name] = subclass.to_s end |
.properties ⇒ Object
154 155 156 |
# File 'lib/metro/models/properties/property.rb', line 154 def self.properties @properties ||= [] end |
.properties_hash ⇒ Object
Generate a properties hash. This properties will default to the numeric key when the property type cannot be found.
170 171 172 |
# File 'lib/metro/models/properties/property.rb', line 170 def self.properties_hash @properties_hash ||= HashWithIndifferentAccess.new { |hash,key| hash[:numeric] } end |
.property(name) ⇒ Object
Returns a Property Class that matches the specified name.
161 162 163 164 |
# File 'lib/metro/models/properties/property.rb', line 161 def self.property(name) property_classname = properties_hash[name] property_classname.constantize end |
.set(*types, &block) ⇒ Object
Define a filter block for setting a value of the type.
50 51 52 53 54 55 |
# File 'lib/metro/models/properties/property.rb', line 50 def self.set(*types,&block) types = [ NilClass ] if types.empty? types.each do |type| sets[type.to_s] = block end end |
.sets ⇒ Object
All set filter blocks defined
58 59 60 |
# File 'lib/metro/models/properties/property.rb', line 58 def self.sets @sets ||= hash_with_default_to_nil end |
Instance Method Details
#get(value) ⇒ Object
Perform a get of the value, running it through the get filter block that matches the type.
44 45 46 47 |
# File 'lib/metro/models/properties/property.rb', line 44 def get(value) get_block = self.class.gets[value.class.to_s] instance_exec(value,&get_block) end |
#set(value) ⇒ Object
Perform a set of the value, running it through the set filter block that matches the type.
64 65 66 67 |
# File 'lib/metro/models/properties/property.rb', line 64 def set(value) set_block = self.class.sets[value.class.to_s] instance_exec(value,&set_block) end |