Module: HasAttributes

Included in:
IChing::Hexagram, IChing::Line, IChing::Trigram
Defined in:
lib/i_ching/has_attributes.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



2
3
4
# File 'lib/i_ching/has_attributes.rb', line 2

def attributes
  @attributes
end

Class Method Details

.included(base) ⇒ Object



9
10
11
# File 'lib/i_ching/has_attributes.rb', line 9

def self.included(base)
  base.extend(self)
end

Instance Method Details

#has_attributes(*args) ⇒ Object



4
5
6
7
# File 'lib/i_ching/has_attributes.rb', line 4

def has_attributes(*args)
  @attributes = args
  instance_eval { attr_reader *@attributes }
end

#initialize(args) ⇒ Object



13
14
15
16
17
18
# File 'lib/i_ching/has_attributes.rb', line 13

def initialize(args)
  args.each do |k, v|
    next unless self.class.attributes.include?(k)
    instance_variable_set("@#{k}", v)
  end
end

#to_hObject



20
21
22
23
24
# File 'lib/i_ching/has_attributes.rb', line 20

def to_h
  Hash[self.class.attributes.map do |key|
    [key, self.send(key)]
  end]
end

#to_json(*args) ⇒ Object



26
27
28
# File 'lib/i_ching/has_attributes.rb', line 26

def to_json(*args)
  JSON.generate(self.to_h)
end