Class: Ovto::State
- Inherits:
-
Object
- Object
- Ovto::State
- Defined in:
- lib/ovto/state.rb
Defined Under Namespace
Classes: MissingValue, UnknownStateKey
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 43 44 45 46 47 48 49 50 |
# 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 UnknownStateKey, "unknown key(s): #{unknown_keys.inspect}" end @values = self.class.item_specs.map{|name, | if !hash.key?(name) && !.key?(:default) && !.key?(:default_proc) raise MissingValue, ":#{name} is mandatory for #{self.class.name}.new" end # Note that `hash[key]` may be false or nil value = if hash.key?(name) hash[name] elsif .key?(:default) [:default] elsif .key?(:default_proc) [:default_proc].call else raise "must not happen" end [name, value] }.to_h end |
Instance Attribute Details
#values ⇒ Object (readonly)
Returns the value of attribute values.
51 52 53 |
# File 'lib/ovto/state.rb', line 51 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}, #{.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`
68 69 70 |
# File 'lib/ovto/state.rb', line 68 def ==(other) other.is_a?(State) && self.values == other.values end |
#[](key) ⇒ Object
Return the value corresponds to `key`
63 64 65 |
# File 'lib/ovto/state.rb', line 63 def [](key) @values[key] end |
#inspect ⇒ Object
76 77 78 |
# File 'lib/ovto/state.rb', line 76 def inspect "#<#{self.class.name}:#{object_id} #{@values.inspect}>" end |
#merge(hash) ⇒ Object
Create new state object from `self` and `hash`
54 55 56 57 58 59 60 |
# File 'lib/ovto/state.rb', line 54 def merge(hash) unknown_keys = hash.keys - self.class.item_specs.map(&:first) if unknown_keys.any? raise UnknownStateKey, "unknown key(s): #{unknown_keys.inspect}" end self.class.new(@values.merge(hash)) end |
#to_h ⇒ Object
72 73 74 |
# File 'lib/ovto/state.rb', line 72 def to_h @values end |