Class: ActiveShard::ShardDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/active_shard/shard_definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, options = {}) ⇒ ShardDefinition

Returns a new ShardDefinition

Parameters:

  • name (String)

    name of the shard

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :schema (String)

    name of the schema

  • :group (String)

    group name of shard

  • all (Object)

    other options passed as connection spec



67
68
69
70
71
72
73
# File 'lib/active_shard/shard_definition.rb', line 67

def initialize( name, options={} )
  opts = options.dup

  @name                 = name.to_sym
  @schema               = ( ( sch = opts.delete( :schema ) ).nil? ? nil : sch.to_sym )
  self.connection_spec  = opts
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/active_shard/shard_definition.rb', line 7

def name
  @name
end

#schemaObject

Returns the value of attribute schema.



6
7
8
# File 'lib/active_shard/shard_definition.rb', line 6

def schema
  @schema
end

Class Method Details

.from_hash(hash) ⇒ Hash

Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values

Parameters:

  • hash (Hash)

    raw hash in YAML-format

Returns:

  • (Hash)

    hash of environments and lists of Definitions



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/active_shard/shard_definition.rb', line 42

def from_hash( hash )
  definitions = {}

  hash.each_pair do |environment, schemas|
    schemas.each_pair do |schema, shards|
      shards.each_pair do |shard, spec|
        ( definitions[ environment.to_sym ] ||= [] ) << self.new( shard.to_sym,
                                                                  spec.merge( :schema => schema.to_sym ) )
      end unless shards.nil?
    end unless schemas.nil?
  end unless hash.nil?

  definitions
end

.from_yaml(yaml) ⇒ Hash

Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values

Parameters:

  • yaml (String)

    YAML string to parse

Returns:

  • (Hash)

    hash of environments and lists of Definitions



29
30
31
32
33
# File 'lib/active_shard/shard_definition.rb', line 29

def from_yaml( yaml )
  hash = YAML.load( ERB.new( yaml ).result )

  from_hash( hash )
end

.from_yaml_file(file_name) ⇒ Hash

Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values

Parameters:

  • file_name (String)

    path to Yaml file

Returns:

  • (Hash)

    hash of environments and lists of Definitions



18
19
20
# File 'lib/active_shard/shard_definition.rb', line 18

def from_yaml_file( file_name )
  from_yaml( File.open( file_name ).read() )
end

Instance Method Details

#==(other) ⇒ Object



104
105
106
# File 'lib/active_shard/shard_definition.rb', line 104

def ==( other )
  eql?( other )
end

#belongs_to_schema?(schema_name) ⇒ bool

Returns true if our schema == schema_name and neither self#schema nor schema_name is nil. Returns false otherwise.

Parameters:

  • schema_name (Symbol)

Returns:

  • (bool)


98
99
100
101
102
# File 'lib/active_shard/shard_definition.rb', line 98

def belongs_to_schema?( schema_name )
  return false if ( schema.nil? || schema_name.nil? )

  ( schema.to_sym == schema_name.to_sym )
end

#connection_adapterObject



75
76
77
# File 'lib/active_shard/shard_definition.rb', line 75

def connection_adapter
  @connection_adapter ||= connection_spec[:adapter]
end

#connection_databaseObject



79
80
81
# File 'lib/active_shard/shard_definition.rb', line 79

def connection_database
  @connection_database ||= connection_spec[:database]
end

#connection_specObject



87
88
89
# File 'lib/active_shard/shard_definition.rb', line 87

def connection_spec
  @connection_spec ||= {}
end

#connection_spec=(val) ⇒ Object



83
84
85
# File 'lib/active_shard/shard_definition.rb', line 83

def connection_spec=(val)
  @connection_spec = val.nil? ? nil : HashWithIndifferentAccess.new( val )
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


108
109
110
111
112
# File 'lib/active_shard/shard_definition.rb', line 108

def eql?( other )
  (self.name == other.name) &&
    (self.schema == other.schema) &&
    (self.connection_spec == other.connection_spec)
end