Class: Bookshop::Commands::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/bookshop/commands/yaml/book.rb

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Book

Returns a new instance of Book.



14
15
16
17
# File 'lib/bookshop/commands/yaml/book.rb', line 14

def initialize(data={})
  @data = {}
  update!(data)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/bookshop/commands/yaml/book.rb', line 37

def method_missing(sym, *args)
  if sym.to_s =~ /(.+)=$/
    self[$1] = args.first
  else
    self[sym]
  end
end

Instance Method Details

#[](key) ⇒ Object



25
26
27
# File 'lib/bookshop/commands/yaml/book.rb', line 25

def [](key)
  @data[key.to_sym]
end

#[]=(key, value) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/bookshop/commands/yaml/book.rb', line 29

def []=(key, value)
  if value.class == Hash
    @data[key.to_sym] = Book.new(value)
  else
    @data[key.to_sym] = value
  end
end

#update!(data) ⇒ Object



19
20
21
22
23
# File 'lib/bookshop/commands/yaml/book.rb', line 19

def update!(data)
  data.each do |key, value|
    self[key] = value
  end
end