Class: Yacl::Properties
- Inherits:
-
Object
- Object
- Yacl::Properties
- Extended by:
- Forwardable
- Defined in:
- lib/yacl/properties.rb
Overview
Properties is a wrapper around keys and values that are part of a Configuration. This is just the keys and the values, it does not tie it to where the data comes from.
The properties interface is a limited subset of the Hash interface.
Properties may also be accessed via a dotted method call location
Example:
p = Properties.new( 'my.a' => 'foo', 'my.b' => 'bar' )
p.my.a #=> 'foo'
p.my.b #=> 'bar'
p.my #=> Properties
Instance Attribute Summary collapse
-
#map ⇒ Object
readonly
Internal: used only for merging.
Class Method Summary collapse
-
.delegatable_methods ⇒ Object
Internal: Returns the list of methods that may be delegated to a Properties instance.
-
.delegating_to_map ⇒ Object
Internal: returns the list of methods to delegate to the internal Map instance.
Instance Method Summary collapse
-
#has_scope?(scope) ⇒ Boolean
Public: Return true or false if the Properites instance has the given scope.
-
#initialize(initial = {}) ⇒ Properties
constructor
Create a new Properites.
-
#merge!(other) ⇒ Object
Public: Mereg another Properties instance on top of this one, overwriting any commaon key/values.
-
#scoped_by(*scope) ⇒ Object
Public: Return a new Properties that is a subset of the properties with the first prefix removed.
-
#scopes ⇒ Object
Public: List the knownn scops, i.e.
Constructor Details
#initialize(initial = {}) ⇒ Properties
Create a new Properites
initial - A Hash or Map that is used as the initial values of the
Properites
54 55 56 57 58 59 60 |
# File 'lib/yacl/properties.rb', line 54 def initialize( initial = {} ) @map = Map.new ( initial ) do |k,v| k << v @map.set( *k ) end end |
Instance Attribute Details
#map ⇒ Object (readonly)
Internal: used only for merging
Returns the internal Map instances
47 48 49 |
# File 'lib/yacl/properties.rb', line 47 def map @map end |
Class Method Details
.delegatable_methods ⇒ Object
Internal: Returns the list of methods that may be delegated to a Properties instance.
Returns an Array of method names
37 38 39 |
# File 'lib/yacl/properties.rb', line 37 def self.delegatable_methods delegating_to_map + [ :scoped_by, :scopes, :has_scope? ] end |
.delegating_to_map ⇒ Object
Internal: returns the list of methods to delegate to the internal Map instance
Returns an Array of method names
29 30 31 |
# File 'lib/yacl/properties.rb', line 29 def self.delegating_to_map [ :set, :get, :fetch, :store, :delete, :clear, :[], :[]=, :has?, :has_key?, :each, :length, :keys, :method_missing ] end |
Instance Method Details
#has_scope?(scope) ⇒ Boolean
Public: Return true or false if the Properites instance has the given scope.
Returns true or false.
98 99 100 |
# File 'lib/yacl/properties.rb', line 98 def has_scope?( scope ) scope_names.include?( scope ) end |
#merge!(other) ⇒ Object
Public: Mereg another Properties instance on top of this one, overwriting any commaon key/values.
This is a destructive operation on this Properties instance.
other - The Properties instance to lay on top of this one.
Returns nothing
70 71 72 |
# File 'lib/yacl/properties.rb', line 70 def merge!( other ) @map.merge!( other.map ) end |
#scoped_by(*scope) ⇒ Object
Public: Return a new Properties that is a subset of the properties with the first prefix removed.
scope - the scope under which to use
Returns a new Properties object
80 81 82 83 84 |
# File 'lib/yacl/properties.rb', line 80 def scoped_by( *scope ) scope = scope.length == 1 ? scope.first : scope sub_map = @map.get( ( scope ) ) || {} Properties.new( sub_map ) end |
#scopes ⇒ Object
Public: List the knownn scops, i.e. top level keys, of the current Properitess.
Returns an Array
90 91 92 |
# File 'lib/yacl/properties.rb', line 90 def scopes scope_names.to_a.sort end |