Class: Taza::Entity

Inherits:
Object show all
Defined in:
lib/taza/entity.rb

Instance Method Summary collapse

Constructor Details

#initialize(hash, fixture) ⇒ Entity

Creates a entity, pass in a hash to be methodized and the fixture to look up other fixtures (not entirely happy with this abstraction)



5
6
7
8
9
# File 'lib/taza/entity.rb', line 5

def initialize(hash,fixture)
  @hash = hash
  @fixture = fixture
  define_methods_for_hash_keys
end

Instance Method Details

#create_entity_if_value_is_hash(key) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/taza/entity.rb', line 34

def create_entity_if_value_is_hash(key)
  if (@fixture.nil? && @hash[key].is_a?(Hash))
      Entity.new(@hash[key], nil)
  elsif (!@fixture.nil? && @hash[key].is_a?(Hash))
      Entity.new(@hash[key], @fixture)
  else
      @hash[key]
  end
end

#define_methods_for_hash_keysObject

This method converts hash keys into methods onto the entity



12
13
14
15
16
17
18
# File 'lib/taza/entity.rb', line 12

def define_methods_for_hash_keys
  @hash.keys.each do |key|
    create_method(key) do
      get_value_for_entry(key)
    end
  end
end

#get_value_for_entry(key) ⇒ Object Also known as: []

This method will lookup another fixture if a pluralized fixture exists otherwise return the value in the hash



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/taza/entity.rb', line 21

def get_value_for_entry(key) # :nodoc:
  if @fixture.nil?
    create_entity_if_value_is_hash(key)
  elsif @fixture.fixture_exists?(key)
    @fixture.specific_fixture_entities(key.to_sym, @hash[key])
  elsif @fixture.pluralized_fixture_exists?(key)
    @fixture.get_fixture_entity(key.pluralize.to_sym,@hash[key])
  else
    create_entity_if_value_is_hash(key)
  end
end

#to_hashObject



44
45
46
47
# File 'lib/taza/entity.rb', line 44

def to_hash
  cloned_hash = @hash.clone
  cloned_hash.key_strings_to_symbols!
end