Class: Gerd::Model::GithubState

Inherits:
Object
  • Object
show all
Defined in:
lib/gerd/model/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state_content) ⇒ GithubState

Returns a new instance of GithubState.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/gerd/model/model.rb', line 21

def initialize(state_content)
  
  validators = []
  validators << Gerd::Model::SchemaValidator.new( Proc.new { | data | data['organisation'] != nil }, "Should have an organisation present")
  validators << Gerd::Model::SchemaValidator.new( Proc.new { | data | data['teams'].class == Hash }, "Should have a teams element present")
  validators << Gerd::Model::SchemaValidator.new( Proc.new { | data | data['repositories'].class == Hash }, "Should have a repositories element present")
  validators << Gerd::Model::SchemaValidator.new( Proc.new { | data | data['members'].class == Hash }, "Should have a members element present")
  failures = []
  validators.each do | validator |
    result = validator.evaluate(state_content)
    failures << result unless result.valid?
  end

  @failures = failures
  if failures.length != 0 
     failures.each do | failure |
        puts failure.message
      end
      puts state_content
     raise Gerd::Exceptions::ValidationException.new("Failed to validate #{failures}") 
  end

  @organisation = state_content['organisation']
  @teams = state_content['teams']
  @members = state_content['members']
  @repositories = state_content['repositories']

end

Instance Attribute Details

#failuresObject

Returns the value of attribute failures.



9
10
11
# File 'lib/gerd/model/model.rb', line 9

def failures
  @failures
end

#membersObject

Returns the value of attribute members.



9
10
11
# File 'lib/gerd/model/model.rb', line 9

def members
  @members
end

#organisationObject

Returns the value of attribute organisation.



9
10
11
# File 'lib/gerd/model/model.rb', line 9

def organisation
  @organisation
end

#repositoriesObject

Returns the value of attribute repositories.



9
10
11
# File 'lib/gerd/model/model.rb', line 9

def repositories
  @repositories
end

#teamsObject

Returns the value of attribute teams.



9
10
11
# File 'lib/gerd/model/model.rb', line 9

def teams
  @teams
end

Class Method Details

.from_json(json) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/gerd/model/model.rb', line 11

def self.from_json(json)
   begin
    return Gerd::Model::GithubState.new(JSON.parse(json))
  rescue => e
    puts json
    raise Gerd::Exceptions::ValidationException.new("Couldn't parse JSON")
  end
  
end

Instance Method Details

#serializeObject



50
51
52
53
54
55
56
57
# File 'lib/gerd/model/model.rb', line 50

def serialize
  json = { 'organisation' => @organisation,
    'teams' => @teams,
    'members' => @members,
    'repositories' => @repositories
  }
  json
end