Sha256: 1f140f028d5805c815ccc49227ed4f6cba1edc043f32525562a905297686506c

Contents?: true

Size: 1.82 KB

Versions: 11

Compression:

Stored size: 1.82 KB

Contents

class LucidProps
  def initialize(props_hash)
    props_hash = {} unless props_hash
    @props_hash = props_hash
  end

  def any?
    @props_hash.keys.size > 0
  end

  def to_h
    @props_hash
  end

  def to_transport
    {}.merge(@props_hash)
  end

  if RUBY_ENGINE == 'opal'
    def [](prop_name)
      @props_hash[prop_name]
    end

    def []=(prop_name, value)
      @props_hash[prop_name] = value
    end

    def key?(prop_name)
      @props_hash.key?(prop_name)
    end

    def keys
      @props_hash.keys
    end

    def method_missing(prop_name, *args, &block)
      return @props_hash[prop_name] if @props_hash.key?(prop_name)
      super(prop_name, *args, &block)
    end

    def set(prop_name, value)
      @props_hash[prop_name] = value
    end

    def to_json
      JSON.dump(to_transport)
    end

    def to_n
      @props_hash.to_n
    end
  else # RUBY_ENGINE
    def [](prop_name)
      name = prop_name.to_sym
      return @props_hash[name] if @props_hash.key?(name)
      name = prop_name.to_s
      return @props_hash[name] if @props_hash.key?(name)
      nil
    end

    def []=(prop_name, value)
      @props_hash[prop_name.to_sym] = value
    end

    def key?(prop_name)
      @props_hash.key?(prop_name.to_sym) || @props_hash.key?(prop_name.to_s)
    end

    def keys
      @props_hash.keys.map(&:to_sym)
    end

    def method_missing(prop_name, *args, &block)
      name = prop_name.to_sym
      return @props_hash[name] if @props_hash.key?(name)
      name = prop_name.to_s
      return @props_hash[name] if @props_hash.key?(name)
      super(prop_name, *args, &block)
    end

    def set(prop_name, value)
      @props_hash[prop_name.to_sym] = value
    end

    def to_json
      Oj.dump(to_transport, mode: :strict)
    end

    def to_n
      @props_hash
    end
  end # RUBY_ENGINE

  alias_method :has_key?, :key?
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
isomorfeus-policy-1.0.0.zeta25 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta24 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta23 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta22 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta21 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta20 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta19 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta18 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta17 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta16 lib/lucid_props.rb
isomorfeus-policy-1.0.0.zeta15 lib/lucid_props.rb