Module: Ehon::ClassMethods

Defined in:
lib/ehon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contentsObject

Returns the value of attribute contents.



32
33
34
# File 'lib/ehon.rb', line 32

def contents
  @contents
end

#default_optionsObject

Returns the value of attribute default_options.



32
33
34
# File 'lib/ehon.rb', line 32

def default_options
  @default_options
end

Instance Method Details

#allObject



46
47
48
# File 'lib/ehon.rb', line 46

def all
  self.contents.values
end

#default(options = {}) ⇒ Object



42
43
44
# File 'lib/ehon.rb', line 42

def default(options = {})
  self.default_options.merge!(options)
end

#find(*queries) ⇒ Object Also known as: []



50
51
52
53
54
55
56
57
58
59
# File 'lib/ehon.rb', line 50

def find(*queries)
  queries.flatten!
  findeds = queries.map {|query|
    next self.contents[query] unless query.is_a?(Hash)
    self.contents.values.find {|instance|
      query.all? {|key, value| instance.options[key] == value }
    }
  }.compact
  queries.size == 1 ? findeds.first : findeds
end

#page(id, options = {}, &block) ⇒ Object Also known as: enum



34
35
36
37
38
39
# File 'lib/ehon.rb', line 34

def page(id, options = {}, &block)
  instance = new(id, options)
  instance.instance_eval(&block) if block_given?
  self.contents[id] = instance
  instance
end