Class: ActiveShard::ShardDefinition
- Inherits:
-
Object
- Object
- ActiveShard::ShardDefinition
- Defined in:
- lib/active_shard/shard_definition.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
Returns the value of attribute name.
-
#schema ⇒ Object
Returns the value of attribute schema.
Class Method Summary collapse
-
.from_hash(hash) ⇒ Hash
Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values.
-
.from_yaml(yaml) ⇒ Hash
Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values.
-
.from_yaml_file(file_name) ⇒ Hash
Returns a hash with environments as the hash keys and a list of ShardDefinitions as the hash values.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#belongs_to_schema?(schema_name) ⇒ bool
Returns true if our schema == schema_name and neither self#schema nor schema_name is nil.
- #connection_adapter ⇒ Object
- #connection_database ⇒ Object
- #connection_spec ⇒ Object
- #connection_spec=(val) ⇒ Object
- #eql?(other) ⇒ Boolean
-
#initialize(name, options = {}) ⇒ ShardDefinition
constructor
Returns a new ShardDefinition.
Constructor Details
#initialize(name, options = {}) ⇒ ShardDefinition
Returns a new ShardDefinition
67 68 69 70 71 72 73 |
# File 'lib/active_shard/shard_definition.rb', line 67 def initialize( name, ={} ) opts = .dup @name = name.to_sym @schema = ( ( sch = opts.delete( :schema ) ).nil? ? nil : sch.to_sym ) self.connection_spec = opts end |
Instance Attribute Details
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/active_shard/shard_definition.rb', line 7 def name @name end |
#schema ⇒ Object
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
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
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
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.
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_adapter ⇒ Object
75 76 77 |
# File 'lib/active_shard/shard_definition.rb', line 75 def connection_adapter @connection_adapter ||= connection_spec[:adapter] end |
#connection_database ⇒ Object
79 80 81 |
# File 'lib/active_shard/shard_definition.rb', line 79 def connection_database @connection_database ||= connection_spec[:database] end |
#connection_spec ⇒ Object
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
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 |