Sha256: ddb1300d9350660162bfc69f39392deac8edde2d4295d4e9d697372fcb295c61

Contents?: true

Size: 1.21 KB

Versions: 27

Compression:

Stored size: 1.21 KB

Contents

class RestModel
  class Key
    attr_accessor :name, :model, :options, :summarize

    def initialize(name, options = {})
      @name    = name
      @options = options
    end

    def convert_input_keys
      options.fetch(:convert_input_keys, model.try(:convert_input_keys) ||
                    RestModel::Configuration.convert_input_keys)
    end

    def source_path
      return @source_path.clone if @source_path

      @source_path = Source::Path.resolve(options, convert_input_keys)

      if @source_path.empty? and !root_path?
        @source_path = convert_input_keys.call([name])
      end

      @source_path.clone
    end

    def root_path?
      path_definition = options.slice(:start_key, :field)
      path_definition.any? and path_definition.first[1].empty?
    end

    def present?(resource)
      !options[:if] ? true : resource.instance_eval(&options[:if])
    end

    def visible?(resource)
      !present?(resource) ? false : case visible = options[:visible]
                                    when nil  then true
                                    when Proc then resource.instance_eval(&visible)
                                    else visible
                                    end
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
rest_model-0.2.1 lib/rest_model/key.rb
rest_model-0.2.0 lib/rest_model/key.rb
rest_model-0.1.24 lib/rest_model/key.rb
rest_model-0.1.23 lib/rest_model/key.rb
rest_model-0.1.22 lib/rest_model/key.rb
rest_model-0.1.21 lib/rest_model/key.rb
rest_model-0.1.20 lib/rest_model/key.rb
rest_model-0.1.19 lib/rest_model/key.rb
rest_model-0.1.18 lib/rest_model/key.rb
rest_model-0.1.17 lib/rest_model/key.rb
rest_model-0.1.16 lib/rest_model/key.rb
rest_model-0.1.15 lib/rest_model/key.rb
rest_model-0.1.14 lib/rest_model/key.rb
rest_model-0.1.13 lib/rest_model/key.rb
rest_model-0.1.12 lib/rest_model/key.rb
rest_model-0.1.11 lib/rest_model/key.rb
rest_model-0.1.10 lib/rest_model/key.rb
rest_model-0.1.9 lib/rest_model/key.rb
rest_model-0.1.8 lib/rest_model/key.rb
rest_model-0.1.7 lib/rest_model/key.rb