Class: Ovto::State

Inherits:
Object
  • Object
show all
Defined in:
lib/ovto/state.rb

Defined Under Namespace

Classes: MissingValue, UnknownStateKey

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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, options|
    if !hash.key?(name) && !options.key?(:default) && !options.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 options.key?(:default)
              options[:default]
            elsif options.key?(:default_proc)
              options[:default_proc].call
            else
              raise "must not happen"
            end
    [name, value]
  }.to_h
end

Instance Attribute Details

#valuesObject (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, options={})
  unless options.is_a?(Hash)
    raise ArgumentError, "options must be a Hash: item :#{name}, #{options.inspect}"
  end
  @item_specs << [name, options]
  # Define accessor
  define_method(name){ @values[name] }
end

.item_specsObject

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

#inspectObject



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_hObject



72
73
74
# File 'lib/ovto/state.rb', line 72

def to_h
  @values
end