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.



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

def contents
  @contents
end

#default_optionsObject

Returns the value of attribute default_options.



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

def default_options
  @default_options
end

Instance Method Details

#allObject



62
63
64
# File 'lib/ehon.rb', line 62

def all
  self.contents.values
end

#create_accessors!Object



57
58
59
60
# File 'lib/ehon.rb', line 57

def create_accessors!
  create_readers!
  create_writers!
end

#create_readers!Object



49
50
51
# File 'lib/ehon.rb', line 49

def create_readers!
  options.each {|option| define_reader option }
end

#create_writers!Object



53
54
55
# File 'lib/ehon.rb', line 53

def create_writers!
  options.each {|option| define_writer option }
end

#default(options = {}) ⇒ Object



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

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

#enum(id, options = {}, &block) ⇒ Object



38
39
40
41
42
43
# File 'lib/ehon.rb', line 38

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

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ehon.rb', line 66

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