Class: Mingle::MingleSymbolMap

Inherits:
MingleValue
  • Object
show all
Extended by:
BitGirder::Core::BitGirderMethods, Forwardable
Includes:
Enumerable
Defined in:
lib/mingle.rb

Defined Under Namespace

Classes: NoSuchKeyError

Constant Summary

Constants included from BitGirder::Core::BitGirderMethods

BitGirder::Core::BitGirderMethods::PARAM_TYPE_ARG, BitGirder::Core::BitGirderMethods::PARAM_TYPE_ENVVAR, BitGirder::Core::BitGirderMethods::PARAM_TYPE_KEY

Class Method Summary collapse

Instance Method Summary collapse

Methods included from BitGirder::Core::BitGirderMethods

argv_to_argh, check_fail_prefix, class_name_to_sym, code, compares_to, console, ext_to_class_name, ext_to_sym, has_env, has_key, has_keys, nonnegative, not_nil, positive, raisef, set_from_key, set_var, split_argv, sym_to_cli_switch, sym_to_ext_id, to_bool, unpack_argv_array, unpack_argv_hash, warn

Constructor Details

#initialize(map) ⇒ MingleSymbolMap

Returns a new instance of MingleSymbolMap.



2142
2143
2144
# File 'lib/mingle.rb', line 2142

def initialize( map )
    @map = map.freeze
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object (private)



2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
# File 'lib/mingle.rb', line 2214

def method_missing( meth, *args )
    
    case meth.to_s

        when /^(expect|get)_(mingle_[a-z][a-z\d]*(?:_[a-z][a-z\d]*)*)$/
            
            case val = send( $1.to_sym, expect_one_arg( args ) )
                when nil then nil
                else MingleModels.as_mingle_instance( val, $2.to_sym )
            end

        when /^(expect|get)_string$/
            s = send( :"#{$1}_mingle_string", *args ) and s.to_s

        when /^(expect|get)_int$/
            s = send( :"#{$1}_mingle_int64", *args ) and s.to_i

        when /^(expect|get)_timestamp$/
            s = send( :"#{$1}_mingle_timestamp", *args )

        when /^(expect|get)_boolean$/
            s = send( :"#{$1}_mingle_boolean", *args ) and s.to_bool

        else super
    end
end

Class Method Details

.create(map = {}) ⇒ Object



2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
# File 'lib/mingle.rb', line 2127

def self.create( map = {} )
    
    res = {}

    not_nil( map, "map" ).each_pair do |k, v| 
        
        mv = MingleModels.as_mingle_value( v )
        res[ MingleIdentifier::get( k ) ] = mv unless mv.is_a?( MingleNull )
    end

    new( res )
end

Instance Method Details

#==(other) ⇒ Object



2196
2197
2198
2199
2200
# File 'lib/mingle.rb', line 2196

def ==( other )
 
    other.is_a?( MingleSymbolMap ) &&
        other.instance_variable_get( :@map ) == @map
end

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



2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
# File 'lib/mingle.rb', line 2156

def []( key )

    case not_nil( key, :key )

        when MingleIdentifier then @map[ key ]

        when String then self[ key.to_sym ]

        when Symbol
            if res = ( @vals_by_sym ||= {} )[ key ]
                res
            else
                res = self[ MingleIdentifier.get( key ) ]
                @vals_by_sym[ key ] = res if res

                res
            end
        
        else raise TypeError, "Unexpected key type: #{key.class}"
    end
end

#expect(key) ⇒ Object



2181
2182
2183
2184
2185
2186
2187
2188
# File 'lib/mingle.rb', line 2181

def expect( key )

    if ( res = self[ key ] ) == nil
        raise NoSuchKeyError, "Map has no value for key: #{key}"
    else
        res
    end
end

#fieldsObject



2151
2152
2153
# File 'lib/mingle.rb', line 2151

def fields
    self
end

#get_mapObject



2146
2147
2148
# File 'lib/mingle.rb', line 2146

def get_map
    {}.merge( @map )
end

#values_at(*arg) ⇒ Object



2191
2192
2193
# File 'lib/mingle.rb', line 2191

def values_at( *arg )
    not_nil( arg, :arg ).map { |k| get( k ) }
end