Class: Ovto::State
- Inherits:
-
Object
- Object
- Ovto::State
- Defined in:
- lib/ovto/state.rb
Defined Under Namespace
Classes: MissingValue, UnknownKey
Instance Attribute Summary collapse
-
#values ⇒ Object
readonly
Returns the value of attribute values.
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
(internal) initialize subclass.
-
.item(name, options = {}) ⇒ Object
Declare state item.
-
.item_specs ⇒ Object
Return list of item specs (Array of ‘[name, options]`).
Instance Method Summary collapse
-
#==(other) ⇒ Object
Return true if a State object ‘other` has same key-value paris as `self`.
-
#[](key) ⇒ Object
Return the value corresponds to ‘key`.
-
#initialize(hash = {}) ⇒ State
constructor
A new instance of State.
- #inspect ⇒ Object
-
#merge(hash) ⇒ Object
Create new state object from ‘self` and `hash`.
- #to_h ⇒ Object
Constructor Details
#initialize(hash = {}) ⇒ State
Returns a new instance of State.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ovto/state.rb', line 28 def initialize(hash = {}) unknown_keys = hash.keys - self.class.item_specs.map(&:first) if unknown_keys.any? raise UnknownKey, "unknown key(s): #{unknown_keys.inspect}" end @values = self.class.item_specs.map{|name, | if !hash.key?(name) && !.key?(:default) raise MissingValue, ":#{name} is mandatory for #{self.class.name}.new" end # Note that `hash[key]` may be false or nil value = hash.key?(name) ? hash[name] : [:default] [name, value] }.to_h end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
43 44 45 |
# File 'lib/ovto/state.rb', line 43 def values @values end |
Class Method Details
.inherited(subclass) ⇒ Object
(internal) initialize subclass
9 10 11 |
# File 'lib/ovto/state.rb', line 9 def self.inherited(subclass) subclass.instance_variable_set('@item_specs', []) end |
.item(name, options = {}) ⇒ Object
Declare state item
14 15 16 17 18 19 20 21 |
# File 'lib/ovto/state.rb', line 14 def self.item(name, ={}) unless .is_a?(Hash) raise ArgumentError, "options must be a Hash: item :#{name}, #{options.inspect}" end @item_specs << [name, ] # Define accessor define_method(name){ @values[name] } end |
.item_specs ⇒ Object
Return list of item specs (Array of ‘[name, options]`)
24 25 26 |
# File 'lib/ovto/state.rb', line 24 def self.item_specs @item_specs end |
Instance Method Details
#==(other) ⇒ Object
Return true if a State object ‘other` has same key-value paris as `self`
60 61 62 |
# File 'lib/ovto/state.rb', line 60 def ==(other) other.is_a?(State) && self.values == other.values end |
#[](key) ⇒ Object
Return the value corresponds to ‘key`
55 56 57 |
# File 'lib/ovto/state.rb', line 55 def [](key) @values[key] end |
#inspect ⇒ Object
68 69 70 |
# File 'lib/ovto/state.rb', line 68 def inspect "#<#{self.class.name}:#{object_id} #{@values.inspect}>" end |
#merge(hash) ⇒ Object
Create new state object from ‘self` and `hash`
46 47 48 49 50 51 52 |
# File 'lib/ovto/state.rb', line 46 def merge(hash) unknown_keys = hash.keys - self.class.item_specs.map(&:first) if unknown_keys.any? raise UnknownKey, "unknown key(s): #{unknown_keys.inspect}" end self.class.new(@values.merge(hash)) end |
#to_h ⇒ Object
64 65 66 |
# File 'lib/ovto/state.rb', line 64 def to_h @values end |